Qt 5 多窗口传值问题
需求: 在主窗体下点击按钮弹出信息收集框,然后获取Dialog中的值。
实现方式: 自定义信号和槽、 函数指针
在 Dialog.h 方面
//自定义信号用于向主窗体发送信号
signals:
void sendMessage(QString);
在Dialog.cpp中
QString a = "HelloWorld";
emit sendMessage(a);
在MainWindow.cpp 中:
//由于传递带参数的信号,需要定义一个参数表与信号相同的槽函数 void recive(QString a);
定义函数指针
函数指针定义格式
返回类型 (作用域::*给函数指针取个名字)(参数表)= &作用域::函数名;
绑定信号与槽
void (importVideo:: *signal)(QString) = &importVideo::sendMessage;
connect(&dl,signal, this, &MainWindow::on_addVideoInfo);
在 MainWindow.cpp定义子窗口时候
使用 Dialog dl 使用QDialog *dl = new Dialog(this);容易报错