qt show widget_Qt中show()与exec()
1.?show()默認顯示的是非模態對話框,即此對話框出現后你還可以對其他窗口進行操作,可以用setModal函數進行設置窗口為模態,即無法操作其他窗口,即被阻塞. 而exec()出現的只能是模態對話框.
2. show()顯示的窗口無論是否模態,都立刻將操作權返回, 運行下面代碼;而exec()則是得等待exec出的窗口關閉后再運行下面代碼.
qt幫助文檔那個中寫的是:Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result., 返回的DialogCode 有QDialog::Accepted和QDialog::Rejected,可以根據返回值來進行判斷等操作.
下面是一個小程序:
#ifndef DIALOG1_H
#define DIALOG1_H
#include
#include "ui_dialog1.h"
class dialog1 : public QDialog
{
Q_OBJECT
public:
dialog1(QWidget *parent = 0);
protected slots:
void buttonClicked();
private:
Ui::dialog1 ui;
};
#endif // DIALOG1_H
/*multiwindow.h*/#ifndef MULTIWINDOW_H#define MULTIWINDOW_H
#include "dialog1.h"
#include
#include "ui_multiwindow.h"
class multiWindow : public QMainWindow
{
Q_OBJECT
public:
multiWindow(QWidget *parent = 0);
public slots:
void button1Clicked();
private:
Ui::multiWindowClass ui;
dialog1 *dialog;
};
#endif // MULTIWINDOW_H
/*dialog1.cpp*/#include "dialog1.h"
dialog1::dialog1(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
}
void dialog1::buttonClicked()
{
this->close();
}
/*multiwindow.cpp*/#include "multiwindow.h"
multiWindow::multiWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(button1Clicked()));
connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(close()));
dialog = new dialog1(this);
}
void multiWindow::button1Clicked()
{
this->hide();
//dialog->show();
dialog->exec();
this->show();
}/*main.cpp*/#include "multiwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
multiWindow w;
w.show();
return a.exec();
}
重點是multiwindow.cpp中的this->hide();
dialog->exec();
this->show();當按下按鈕時,先將主窗口隱藏,然后顯示exec的模態對話框,不繼續執行下面代碼,等待對話框的操作, 按下對話框的按鈕后,對話框關閉,繼續執行主對話框代碼,將主對話框顯示,即實現對話框的調用.
若將exec替換為show,則會出現兩個對話框并存的現象.
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的qt show widget_Qt中show()与exec()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 共享内存变量_浅谈pyth
- 下一篇: java订单超时取消设计_quartz框