Qt; C++ 11; Qt父子窗体;
实验环境: Qt5.8.0 支持C++ 11 ubuntu 14.04 64bit
接下来,探究父子窗体 新建一个project,取默认的mainwindow类型,在mainwindow.cpp中引入#include <QDialog> 接着在构造函数中加入
ui(new Ui::MainWindow) { ui->setupUi(this); QDialog * d=new QDialog(this);//'this' stands for father window pointer d->show(); }弹出一大一小两个窗体
若将d->show();改为d->exec();,则小窗体会先弹出,小窗体关闭后大窗体才会执行,原因是阻塞。
若想在窗体中加入一个下拉菜单按键,并将菜单中的选项设置为出发子窗体的事件,需要在ui设计中,加入下拉菜单和 具体选项。我加入了一个名为123的下拉按键,并在123按键下新建一个名为456的按键选项。
接着在Qt Creator的ui设计页面下方的action editor中选中新生成的选项,右键转到槽, 这时在mainwindow.cpp中就自动生存了on_action456_triggered,将刚才构造函数中的语句移到这里就ok
void MainWindow::on_action456_triggered() { QDialog * d=new QDialog(this);//'this' stands for father window pointer d->show(); //d->exec(); }此时show和exec无区别。 如果不想要Qdialog,觉得太小,换成MainWindow也可以
void MainWindow::on_action456_triggered() { MainWindow * d=new MainWindow(this);//'this' stands for father window pointer d->show(); //d->exec(); }注意到,关闭父窗体也会导致子窗体关闭,看起来是一起被析构了。
若我想自定义弹出窗口的内部样式,如ui的设计,加入文字,按钮等
只需在点击project -> 右键 -> 选择添加新文件 -> QT -> QT设计师界面类,一路默认。
现在在mainwindow.cpp中加入新生成的文件中的#include "dialog.h" 然后将事件on_action456_triggered()改为dialog
void MainWindow::on_action456_triggered() { Dialog * d=new Dialog(this);//'this' stands for father window pointer d->show(); //d->exec(); }这时,点击下拉菜单中选项弹出的就是新定义的窗口。(ubuntu下,下拉菜单不在生成的窗口中,而是在ubuntu desktop的顶栏上,这是系统的问题,you know all about that~)
如果我想在父子窗体间传递信息?请看我的下一篇学习记录 link
visitor tracker