在QDialog表单之间传递QString变量

时间:2022-11-06 10:03:59

The problem I have encountered is a bit trickier and not similar to other problems / solutions over the net. To provide a brief idea of my application, the work-flow is shown below;

我遇到的问题有点棘手,与网络上的其他问题/解决方案不太相似。为了简要介绍我的应用,工作流程如下所示;

在QDialog表单之间传递QString变量

I have done till fetching the data in Dialog2 and saved the data in QString variables. And I want to pass this back to Dialog1 which is already OPEN. Using these values I will do setText to set values to QLabel and QLineEdit widgets in Dialog1.

我一直在读取Dialog2中的数据并将数据保存在QString变量中。我想把它传回给已经开放的Dialog1。使用这些值,我将使用setText将值设置为Dialog1中的QLabel和QLineEdit小部件。

The technique I have used is not reflecting the changes on the Dialog1. Maybe because it is already OPEN and has not been updated.

我使用的技术并未反映Dialog1上的更改。也许是因为它已经开放且尚未更新。

Relevant code snippets are shown below -

相关代码段如下所示 -

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();
}

The QString is getting passed properly between the QDialog form classes. I have used qDebug() messages to verify this.

QString在QDialog表单类之间正确传递。我已经使用qDebug()消息来验证这一点。

How to ensure that the values of variables are reflected on Dialog1 ??? Can anyone please guide me with reference to my code ???

如何确保变量的值反映在Dialog1上?任何人都可以参考我的代码指导我???

2 个解决方案

#1


Dialog2 should simply provide a signal containing the data you want to transfer to Dialog1 that gets emitted whenever Dialog2 is done with its action:

Dialog2应该只提供一个信号,其中包含您要传输到Dialog1的数据,只要Dialog2完成其操作就会发出该信号:

Dialog1.cpp

void Dialog1::on_pushButton_2_clicked()
{
    Dialog2 dialog2;
    dialog2.setModal(true);
    dialog2.setWindowFlags(Qt::FramelessWindowHint);
    connect(&dialog2, &Dialog2::dataFetched, this, &Dialog1::updateData);
    // or Qt4 connect syntax
    // connect(&dialog2, SIGNAL(dataFetched(const QString&)), this, SLOT(updateData(const QString&));
    // or directly connect to the label
    // connect(&dialog2, &Dialog2::dataFetched, ui->lineEdit, &QLineEdit::setText);
    dialog2.exec();
}

void Dialog1::updateData(const QString& data)
{
    ui->lineEdit->setText(data);
}

Dialog2.h

public slots:
    void saveSettings();    //slot for button press at dialog2, this will set the values at dialog1 and close the dialog2

signals:
    void dataFetched(const QString& data);

Dialog2.cpp

void Dialog2::saveSettings()
{
    // do whatever to generate the data
    emit dataFetched(Vehicle_Name);
    accept();
}

#2


Dialog2 needs to know the instance of Dialog1 where it was opened from, so it can pass the string back to this specific instance:

Dialog2需要知道它打开的Dialog1的实例,因此它可以将字符串传递回这个特定的实例:

class Dialog1;
class Dialog2 : public QDialog
{
public:
    Dialog2(Dialog1* parent) : QDialog(parent), parent(parent) {}
public slots:
    void saveSettings()
    {
          parent->setLabelText("hello")
    }
private:
    Dialog1* parent;
};

#1


Dialog2 should simply provide a signal containing the data you want to transfer to Dialog1 that gets emitted whenever Dialog2 is done with its action:

Dialog2应该只提供一个信号,其中包含您要传输到Dialog1的数据,只要Dialog2完成其操作就会发出该信号:

Dialog1.cpp

void Dialog1::on_pushButton_2_clicked()
{
    Dialog2 dialog2;
    dialog2.setModal(true);
    dialog2.setWindowFlags(Qt::FramelessWindowHint);
    connect(&dialog2, &Dialog2::dataFetched, this, &Dialog1::updateData);
    // or Qt4 connect syntax
    // connect(&dialog2, SIGNAL(dataFetched(const QString&)), this, SLOT(updateData(const QString&));
    // or directly connect to the label
    // connect(&dialog2, &Dialog2::dataFetched, ui->lineEdit, &QLineEdit::setText);
    dialog2.exec();
}

void Dialog1::updateData(const QString& data)
{
    ui->lineEdit->setText(data);
}

Dialog2.h

public slots:
    void saveSettings();    //slot for button press at dialog2, this will set the values at dialog1 and close the dialog2

signals:
    void dataFetched(const QString& data);

Dialog2.cpp

void Dialog2::saveSettings()
{
    // do whatever to generate the data
    emit dataFetched(Vehicle_Name);
    accept();
}

#2


Dialog2 needs to know the instance of Dialog1 where it was opened from, so it can pass the string back to this specific instance:

Dialog2需要知道它打开的Dialog1的实例,因此它可以将字符串传递回这个特定的实例:

class Dialog1;
class Dialog2 : public QDialog
{
public:
    Dialog2(Dialog1* parent) : QDialog(parent), parent(parent) {}
public slots:
    void saveSettings()
    {
          parent->setLabelText("hello")
    }
private:
    Dialog1* parent;
};