Search code examples
c++qtdialogqmainwindow

updating value from mainwindow to dialog


I have a mainwindow that generates random int numbers every second in "numbers" and I have a button that opens a new dialog. In that dialog is a button "Get Number" and a qlabel. If I press "Get Number" I want the current generated number from the mainwindow set to the dialog label text. I tried to write a member function in the mainwindow class that returns the actual value but I cant call it from the dialog and I think that's the wrong way. How can I get this current generated int "numbers" into the dialog? My plain code:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void generateNumbers();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()), this, SLOT(generateNumbers()));
    timer->start(1000);

}

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

void MainWindow::on_pushButton_clicked()
{
    Dialog dialog;
    dialog.setModal(true);
    dialog.exec();
}

void MainWindow::generateNumbers()
{
    int numbers = qrand() % 100;
    qDebug() << numbers;
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

dialog.cpp

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

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

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

Solution

  • On mainwindow.h, add a signal:

    signals:
        void update_number(int);
    

    On mainwindow.cpp, emit that signal everytime you generate a new number:

    void MainWindow::generateNumbers()
    {
        int numbers = qrand() % 100;
        emit update_number(numbers); // signals that a new number has been generated
        qDebug() << numbers;
    }
    

    and your on_pushButton_clicked() should look like this:

    void MainWindow::on_pushButton_clicked()
    {
       Dialog dialog(this); // main window as the parent of dialog
       dialog.setModal(true);
       dialog.exec();
    }
    

    On dialog.h, add an attribute called number_ and add the following slot:

    public slots:
        void on_update_number(int number); // updates the number_
    
    private:
        int number_; // it will be updated with the most recent number
        Ui::Dialog *ui;
    

    On dialog.cpp, connect the signal to the slot:

    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        connect(parent, SIGNAL(update_number(int)), this, SLOT(on_update_number(int)));
    }
    

    add the slot code:

    void Dialog::on_update_number(int number)
    {
        number_ = number;
    }
    

    Finally, when the user clicks the button "Get Number" in the dialog, set the label text:

    void Dialog::on_pushButton_clicked()
    {
        ui->label->setText(QString::number(number_));
    }
    

    For more information on Signals & Slots go here.