Unable to pass variable value from QMainWindow to QDialog in Qt5.4

170 Views Asked by At

I would like to pass the values of variable or in other words, i would like initialize the variables of Dialog class in MainWindow and execute Dialog window. I can successfully initialize the variables of Dialog class using constructor. But when I try to initialize the private or public variables using the defined public function in Dialog class, it doesn't work. Please see below code:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:


    explicit Dialog(const QString &d, QWidget *parent = 0);
    ~Dialog();

    void setDat(const QString &k);



private:
    Ui::Dialog *ui;

    QString msg;

    QString msg2;

};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(const QString &d, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    msg = d;

    ui->label->setText(msg);

    ui->label_2->setText(msg2);
}

void Dialog::setDat(const QString &k)
{
    msg2 = k;
}

Dialog::~Dialog()
{
    delete ui;
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "dialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString temp = "Hello....";
    Dialog dg(temp);
    dg.setDat(temp);
    dg.exec();

}
0

There are 0 best solutions below