【发布时间】:2015-05-12 08:16:45
【问题描述】:
我遇到的问题有点棘手,与网上的其他问题/解决方案不同。为了简要介绍我的应用程序,工作流程如下所示;
我已经完成了在 Dialog2 中获取数据并将数据保存在 QString 变量中。我想把它传回给 Dialog1 它已经打开了。使用这些值,我将使用setText 将值设置为 Dialog1 中的 QLabel 和 QLineEdit 小部件。
我使用的技术没有反映 Dialog1 上的更改。也许是因为它已经 OPEN 并且没有更新。
相关代码sn-ps如下图-
Dialog1.h
private slots:
void on_pushButton_2_clicked(); //this slot for pushbutton open Dialog2
public:
void setLabelText(QString str); //for setting the text of label
Dialog1.cpp
void Dialog1::on_pushButton_2_clicked()
{
Dialog2 dialog2;
dialog2.setModal(true);
dialog2.setWindowFlags(Qt::FramelessWindowHint);
dialog2.exec();
}
void Dialog1::setLabelText(QString str)
{
ui->lineEdit->setText(str);
qDebug()<<"Value Arrived "<<str;
}
Dialog2.h
public slots:
void saveSettings(); //slot for button press at dialog2, this will set the values at dialog1 and close the dialog2
Dialog2.cpp
void Dialog2::saveSettings()
{
Dialog1 dialog1;
dialog1.setLabelText(Vehicle_Name); //Vehicle_Name is QString variable
qDebug()<<"Sent Value "<<Vehicle_Name;
accept();
}
QString 在 QDialog 表单类之间正确传递。我已经使用 qDebug() 消息来验证这一点。
如何确保变量的值反映在 Dialog1 上???谁能指导我参考我的代码???
【问题讨论】:
-
我认为只有在 dialog2 关闭时才应该更新 dialog1。您可以通过将 dialog2 的
accepted()信号连接到插槽来做到这一点。还有你为什么在Dialog2::saveSettings()又打电话给dialog1.exec()? -
oopsie .. 复制粘贴时出现错误.. 只需一秒钟......关于“如何在对话框 2 关闭后更新对话框 1”,这就是我遇到的问题,有什么想法吗?? ?
-
我好像还不够清楚。不要调用
dialog1.setLabelText(Vehicle_Name),而是将Vehicle_Name存储在一个变量中。也许在Dialog2的公共成员中。然后将Dialog2::accepted()信号连接到dialog1 的一个插槽,名为on_dialog2_accepted()。在on_dialog2_accepted()拨打这个电话;dialog1.setLabelText(dialog2.Vehicle_Name);。on_dialog2_accepted()应该在 dialog2 关闭时调用,并且 dialog1 会自行更新。希望这对你有用。