• qt学习(六)窗口


    1.Dialog

    #ifndef MYDIALOG_H
    #define MYDIALOG_H
    
    #include <QDialog>
    
    class MyDialog : public QDialog
    {
        Q_OBJECT
    public:
        explicit MyDialog(QWidget *parent = 0);
    
        QString _strDir;
        void paintEvent(QPaintEvent *);
    
    signals:
    
    public slots:
        void slotButtonClick();
    
    };
    
    #endif // MYDIALOG_H
    #include "mydialog.h"
    
    #include "MyDialog.h"
    #include <QPushButton>
    #include <QDebug>
    #include <QFileDialog>
    #include <QFileInfo>
    
    #include <QColorDialog>
    #include <QFontDialog>
    #include <QMessageBox>
    #include <QPainter>
    MyDialog::MyDialog(QWidget *parent) :
        QDialog(parent)
    {
        QPushButton* button = new QPushButton("Click me", this);
        connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick()));
    }
    void MyDialog::slotButtonClick()
    {
        #if 0
        QDialog *dlg=new QDialog;
        int ret;
        ret=dlg->exec();//在模块对话框中,exec有自己的消息循环,并且把app的消息循环接管
        QPushButton *button=new QPushButton(dlg);
        connect(button,SIGNAL(clicked()),dlg,SLOT(accept()));
        //如果Dialog是通过exec来显示,那么可以通过accept或者reject来关闭窗口
        //如果Dialog是通过show来显示,那么可以通过close来关闭窗口,这个和QWidget一样的
    
        //有许多特殊的dialog:打印预览,打印,MessageBox,颜色选择,字体选择
        if(ret==QDialog::Accepted)
        {
            qDebug()<<"Accepted";
        }
        if(ret==QDialog::Rejected)
        {
            qDebug()<<"rejected";
        }
      #endif
      #if 0
        //第一次的时候选择目录,下次进来的时候是第一次保存的目录
        //打开文件,保存文件
        QString strFilename=QFileDialog::getSaveFileName(NULL,
                                                         "Select file for save",
                                                         _strDir,"pic file (*.png *.jpg)");
       #endif
       #if 0
        //选择文件
        QString strFilename=QFileDialog::getOpenFileName(NULL,
                                                         "Select file for save",
                                                         _strDir,"pic file (*.png *.jpg)");
        #endif
        //选择目录
        #if 0
        QString strFilename=QFileDialog::getExistingDirectory();
        if(strFilename.isEmpty())
        {
            qDebug()<<"select note";
            return ;
    
        }
    
        qDebug()<<strFilename;
        QFileInfo fileInfo(strFilename);
        _strDir=fileInfo.filePath();
        #endif
        #if 0
        //颜色对话框
        QColorDialog color;
        color.exec();
        QColor c=color.selectedColor();//选择的颜色
        #endif
        #if 0
        //字体对话框
        QFontDialog fontDialog;
        fontDialog.exec();
        QFont font=fontDialog.selectedFont();
        #endif
    
        //提示对话框
    //     QMessageBox::information(this,"Error","Eror msgs...");
    //     QMessageBox::warning(this,"Error","Eror msgs...");
    //     QMessageBox::critical(this,"Error","Eror msgs...");
         int ret=QMessageBox::question(this,"???","realy do...",QMessageBox::Yes|QMessageBox::No
                                       |QMessageBox::YesAll|QMessageBox::NoAll);//增加按钮
         if(ret==QMessageBox::Yes)
         {
             qDebug()<<"user select yes";
         }
         if(ret==QMessageBox::No)
         {
             qDebug()<<"user select no";
         }
    
    
    
    }
    void MyDialog::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        p.drawLine(QLine(0,0,200,200));
    }
    #include <QApplication>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
    
        MyDialog dlg;
        dlg.show();
    
        return app.exec();
    }

    2.mainWindow

    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include <QMainWindow>
    #include <QWidget>
    #include<QLabel>
    #include<QPainter>
    #include<QMouseEvent>
    #include"myview.h"
    #include<QMenu>
    #include<QSystemTrayIcon>//托盘
    class MyMainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MyMainWindow(QWidget *parent = nullptr);
        QLabel *_label;
        MyView *_view;//中心区域封装在view中,画图等操作在view上进行操作;
        QMenu *_menu;
        QSystemTrayIcon *_icon;
        void paintEvent(QPaintEvent *);
        void mousePressEvent(QMouseEvent *);//右键弹出菜单
        bool event(QEvent *event);
        bool eventFilter(QObject *watched, QEvent *event);
    
    signals:
    
    public slots:
        void slotOpen();
        void slotActioned(QSystemTrayIcon::ActivationReason);
    };
    
    #endif // MYMAINWINDOW_H
    #ifndef MYVIEW_H
    #define MYVIEW_H
    
    #include <QWidget>
    #include<QPainter>
    class MyView : public QWidget
    {
        Q_OBJECT
    public:
        explicit MyView(QWidget *parent = nullptr);
        void paintEvent(QPaintEvent *);
    
    signals:
    
    public slots:
    };
    
    #endif // MYVIEW_H
    #include "mymainwindow.h"
    #include<QApplication>
    #include<QMenu>
    #include<QMenuBar>//菜单栏
    #include<QAction>//菜单上的单项,可以触发
    #include<QToolBar>
    #include<QStatusBar>
    #include<QFileDialog>
    #include<QtDebug>
    #include<QPainter>
    #include<QCursor>//鼠标当前的位置
    MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent)
    {
       /*编程建议:最好先把所有的Action创建好,menu和toolbar从中选择*/
       /*加菜单*/
        QMenuBar *pMenuBar=menuBar();
        QMenu *menu=pMenuBar->addMenu("&File");
        QAction *openAction=menu->addAction("&open",this,SLOT(slotOpen()),QKeySequence::Open);//快捷键open是ctrl+o,根据平台决定
        QAction *saveAction=menu->addAction("&save",this,SLOT(slotOpen()),QKeySequence::Save);
        _menu=menu;
        //加了一条线区分
        menu->addSeparator();
        QAction *closeAction=menu->addAction("&exit",this,SLOT(slotOpen()),QKeySequence::Close);//菜单前面也可以加图标
        closeAction->setToolTip("close window");
    
        /*toolbar 工具栏*/
        QToolBar* toolBar=this->addToolBar("MyToolBar");
        toolBar->addAction(openAction);
        toolBar->addAction(saveAction);
        toolBar->addAction(closeAction);
    
        /*status bar 状态栏*/
        QStatusBar *pStatusBar=this->statusBar();
        pStatusBar->addWidget(_label=new QLabel("OK"));
    
        _label->setText("<font color=red>Processing ...</font>");
    
        /*别的控件占用之后,剩下的区域都是CentralWidget*/
        _view=new MyView();
        this->setCentralWidget(_view);
        //创建托盘
        _icon=new QSystemTrayIcon;
        _icon->setIcon(QIcon("../bing.ico"));
        _icon->setToolTip("This is tray icon test");
        _icon->show();
        _icon->setContextMenu(_menu);//点一下托盘会有菜单
        connect(_icon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                this,SLOT(slotActioned(QSystemTrayIcon::ActivationReason));
                
        this->installEventFilter(this);
    
    }
    
    void MyMainWindow::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        p.drawPixmap(QPoint(0,0),QPixmap("../map.png"));//单纯的这样画,会遮挡菜单栏
    }
    
    void MyMainWindow::mousePressEvent(QMouseEvent *ev)
    {
        if(ev->button()==Qt::RightButton)
            _menu->exec(QCursor::pos());//QCursor::pos返回的位置的窗口坐标系鼠标的位置,而ev->pos()是桌面坐标系的位置
    }
    
    bool MyMainWindow::event(QEvent *event)
    {
        qDebug()<<event;
        if(event->type()==QEvent::Close)
        {
            return false;
        }
        return QMainWindow::event(event);
    }
    
    bool MyMainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if(watched==(QObject *)this &&event->type()==QEvent::Close)
        {
            return true;
        }
        return QMainWindow::eventFilter(watched,event);
    }
    //懒的写槽函数了
    void MyMainWindow::slotOpen()
    {
        QString strFile=QFileDialog::getOpenFileName();
        qDebug()<<"open file is"<<strFile;
    }
    
    void MyMainWindow::slotActioned(QSystemTrayIcon::ActivationReason)//响应托盘事件
    {
        if(reason==QSystemTrayIcon::Trigger)
        {
            if(this->isHidden()) this->show();
            else this->hide();
        }
    }
    
    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
    
        MyMainWindow w;
        w.show();
        return app.exec();
    }
    #include "myview.h"
    
    MyView::MyView(QWidget *parent) : QWidget(parent)
    {
    
    }
    
    void MyView::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        p.fillRect(rect(),Qt::red);
    
    }
  • 相关阅读:
    2017 ICPC沈阳站L
    (POJ 1990)Mowfest(确定不再来一发树状数组么?)
    (POJ 3067) Japan (慢慢熟悉的树状数组)
    (POJ 2549)Sumsets(折半枚举)
    图的高siao存储结构——链式前向星
    (HDU1317)XYZZY(Floyd+spfa)
    (POJ1182)食物链(带权并查集-附通用模板)
    (HDU 1231)最大连续子序列
    (HDU 1598) find the most comfortable road (并查集+最小生成树)
    Problem: The World Final II(NEUOJ1175)排序+动态规划
  • 原文地址:https://www.cnblogs.com/rainbow1122/p/8179309.html
Copyright © 2020-2023  润新知