Qt的三大窗口

Qt的三大窗口dialog、widget、mainwindow

一、dialog、widget、mainwindow的区别

1)、dialog有exec函数,如果是dialog窗口,后边的窗口时不可选的;
2)、widget和dialog都有show函数,如果通过这个函数显示这两种类型的窗口,则两个窗口都是可选的;
3)、widget主要是在上面放置布局和控件;
4)、mainwindow可以显示菜单,工具栏,状态栏、托盘等功能。

二、dialog窗口

这个dialog窗口只是为了给人们提供更好的可视化操作,但是对于程序员而言,这个操作并不是立刻执行的;而是当在窗口选择关闭后,才将选择的结果返回给后台,后台才可以根据选择的结果进行相应的操作。



#include "mydialog.h"

mydialog::mydialog(QDialog *parent):QDialog(parent) { 

    QPushButton *button = new QPushButton( "button" , this ) ;
    connect( button , SIGNAL(clicked()) , this , SLOT(slotButtonClick()) ) ;
}


void mydialog::slotButtonClick() {
	#if 0
    /*dialog和widget的区别,exec和show的区别而已*/
    QDialog *dlg = new QDialog ;
    QPushButton *button = new QPushButton("close" , dlg ) ;
    connect( button , SIGNAL(clicked()) , dlg , SLOT(reject()) ) ;
    int ret = dlg->exec( ) ;
    //通过exec显示出来的窗口称为,模块对话框
    // 在模块对话框中,exec有自己的消息循环,并且把app的消息循环接管了
    // 如果Dialog是通过exec来显示,那么可以通过accept或者reject来关闭窗口
    // 如果Dialog是通过show来显示,那么可以通过close来关闭窗口,这个和QWidget一样的

    // 有许多特殊的dailog:文件选择,MessageBox,颜色选择,字体选择,打印预览,打印
    if( ret == QDialog::Accepted ) 
        qDebug()<<"accepted" ;
    else 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 open", _strDir,"pic file (*.png *.jpg)");
#endif
#if 0
    QString strFilename = QFileDialog::getExistingDirectory();

    if(strFilename.isEmpty())
    {
        qDebug() << "select none";
        return;
    }

    qDebug() << strFilename;
    QFileInfo fileInfo(strFilename);
    _strDir = fileInfo.filePath();
    qDebug() << _strDir;
#endif
    //do something for io ... ...
    //上面的选择将绝对路径名都给拿下来了,如果要进行保存,不是很容易吗!
    QPixmap pixmap(this->size()) ;
    QPainter painter(&pixmap) ;
    this->render( &painter ) ;
    pixmap.save(strFilename) ;
#if 0
/*颜色选择对话框:可以选择相应的颜色;然后将选择的颜色返回,这样我们就可以操作了*/
    QColorDialog colorDia ;
    colorDia.exec() ;
    QColor c = colorDia.selectedColor() ;
#endif
#if 0
/*字体选择对话框:可以选择字体;然后将选择的字体信息返回,我们同样可以用这些信息来设置相应的值*/
    QFontDialog fontDia ;
    fontDia.exec() ;
    QFont font = fontDia.selectedFont() ;
#endif
#if 0
/*这个也是弹窗对话框,不过只是简单的选择以下枚举中的值,可以尝试下效果*/
     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";
    }
    #endif
}


void mydialog::paintEvent( QPaintEvent *ev ) {
}


mydialog::~mydialog(void) { }

三、widget窗口

前面已经介绍过很多继承这个窗口的控件了,这里就不再累述。

四、mainWindow窗口

这个也是给人们提供更好的可视化操作;
一个正常window软件呈现给客户的可视化界面;
包括:menu菜单、tool工具栏、status状态栏、电脑显示屏右下脚的托盘等。

#include "MyWindow.h"


MyWindow::MyWindow(QMainWindow *parent):QMainWindow( parent ) { 

    /*menuBar菜单栏,菜单menu*/
    QMenuBar  *menuBar = this->menuBar() ;
    _menu = menuBar->addMenu( "&File" ) ;
    QMenu *edit = menuBar->addMenu( "&Edit" ) ;

    /*menu菜单中的选项action*/
    QAction *openAction = _menu->addAction( "&Open"
        , this , SLOT(slotOpen()) , QKeySequence::Open  ) ;
    QAction *saveAction = _menu->addAction( "&Save"
        , this , SLOT(slotOpen()) , QKeySequence::Save  ) ;
    _menu->addSeparator() ;//添加分界线
    QAction *closeAction = _menu->addAction( "&Exit"
        , this , SLOT(close()) , QKeySequence::Close  ) ;

    /*toolBar工具栏*/
    QToolBar *toolBar = this->addToolBar( "mimi" ) ;
    toolBar->addAction( openAction ) ;
    toolBar->addAction( saveAction ) ;
    toolBar->addAction( closeAction ) ;
    /*statusBar状态栏*/
    QStatusBar *statusBar = this->statusBar() ;
    QLabel *label ;
    statusBar->addWidget( label = new QLabel("Ok") ) ;
    label->setText( "<font color=blue>XXXXX... ...</font>" ) ;
    /*上面的三种栏介绍完之后,剩下的窗口区域就是CentralWidget
    **如果将widget直接add到mainwindow这个窗口的话,
    **toolbar是会跟添加进来的widget重叠的*/
    MyView *view = new MyView ;
    this->setCentralWidget( view ) ;
    /*最后就是window系统右下脚的托盘:system tray icon*/
    QSystemTrayIcon *icon = new QSystemTrayIcon ;
    icon->setIcon( QIcon("./1.png") ) ;//图标
    icon->setToolTip( "luck dog" ) ;//鼠标滑过提示文字
    icon->show() ;//展示在右下角
    icon->setContextMenu( _menu ) ;//右击出现的菜单
    this->connect( icon , SIGNAL( slotActivated(QSystemTrayIcon::ActivationReason) ) 
        , this , SLOT(slotActivated(QSystemTrayIcon::QSystemTrayIcon::ActivationReason)) ) ;
    this->installEventFilter(this);
}


void MyWindow::slotActivated(QSystemTrayIcon::ActivationReason reason){

    /*这个没成功*/
    if( reason==QSystemTrayIcon::Trigger ) {
        if( this->isHidden() ) 
            this->show() ;
        else
            this->hide() ;
    }
}


bool MyWindow::eventFilter(QObject *o, QEvent *e)
{
    /*实现什么功能呢?*/
    if(o == (QObject*)this && e->type() == QEvent::Close)
    {
        return true;
    }

    return QMainWindow::eventFilter(o, e);
}


void MyWindow::slotOpen() {

    QString fileName = QFileDialog::getOpenFileName() ;
    qDebug()<<fileName ;
    /*将打开的文件中的内容显示在窗口上... ...*/
}
bool MyWindow::event(QEvent *ev)
{
    qDebug() << ev;
    if(ev->type() == QEvent::Close)  {
        return false;
    }
    /*怎么弄才能实现窗口关闭,托盘还在?*/
    return QMainWindow::event(ev);
}


void MyWindow::paintEvent( QPaintEvent * ) {

    QPainter painter(this) ;
    painter.drawPixmap( QPoint(0,0) , QPixmap("./1.png") ) ;
}


void MyWindow::mousePressEvent( QMouseEvent *ev ) {

    if( ev->button() == Qt::RightButton ) {
        _menu->exec( QCursor::pos() ) ;
    }
}


MyWindow::~MyWindow(void) { }

猜你喜欢

转载自blog.csdn.net/weixin_45050225/article/details/107160342