自定义子窗口与主窗口通信

自定义子窗口与主窗口通信

1、自定义QDialog子类

构造函数声明:

ColorPickDialog(QWidget* parent=NULL);//构造函数

构造函数定义:

ColorPickDialog::ColorPickDialog(QWidget* parent):QDialog(parent,Qt::WindowCloseButtonHint)//窗口没有最大最小化,只有关闭

{

//控件初始化

}
2、设置用户操作的槽函数,将用户输入存在本子类的变量中

3、设置用户关闭时的返回值

如果点击关闭按钮:

this->done(0);//done函数会关闭此dialog并且退出exec循环,返回done的参数

如果点击确定按钮:

this->done(1);

4、设置公共get函数,返回值类型就是用户的输入

5、在主窗口任意地方,声明此类的变量,注意参数加上this,才是模态框

ColorPickDialog colorDialog(this);

if(1==colorDialog.exec())

{

//get.....

}