Search code examples
c++qtwindow

Qt passing a Value from one window to antother


I am quite new to programming, so hopefully someone can help me out. After calculating a value with a function and storing it into the variable with a setter, I want to call it in my viswindow.cpp to visualize it in a Graph. In the mainwindow the right value is calculated and stored by pressing a button, which also opens the viswindow. I tried to pass it with a pointer or with signals and slots, but it wouldn't work.

mainwindow.cpp:

double MainWindow::berechneFussabdruck(double strecke, int anzahl, double sfcWert)
{
    if(strecke <= 1500 && strecke > 0){
        double aequivalente_Co2 = (((sfcWert*3.116*50*(((0.0011235955)*(strecke-2*48.42))+0.233))*0.001)/anzahl);
        setAequivalente(aequivalente_Co2);
    }
    else{
        double aequivalente_Co2 = (((sfcWert*3.116*75*(((0.0011235955)*(strecke-2*208))+0.833))*0.001)/anzahl);
        setAequivalente(aequivalente_Co2);
    }
   return aequivalente_Co2;
}

Button to open viswindow:

void MainWindow::on_pushButton_clicked()
{
    MainWindow::berechneFussabdruck(strecke, anzahl, sfcWert);
    visWindow = new VisWindow(this);
    visWindow->show();   
}

viswindow.cpp:

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

    ...

    *set0 << aequivalente_Co2 << aequivalente_Co2;
    *set1 << 11.07 << 0;
    *set2 << 0 << 0.49;
    
    ...
}

Thanks for helping!


Solution

  • Here are examples of:

    1. Single time value transfer from MainWindow to VisWindow in constructor parameter.
    2. Value update from MainWindow to VisWindow by slot-signal chain.
    3. Value update from MainWindow to VisWindow by direct call of VisWindow->updateValue() slot as regular function.

    enter image description here

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "viswindow.h"
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private slots:
        void on_pushButton_clicked();
        void on_pushButton_2_clicked();
        void on_pushButton_3_clicked();
    signals:
        void updatevalue(const double value);
    private:
        Ui::MainWindow *ui;
        VisWindow * m_viswindow;
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow) 
    {
        ui->setupUi(this);
    }
    MainWindow::~MainWindow() {
        delete ui;
    }
    void MainWindow::on_pushButton_clicked() {
        m_viswindow = new VisWindow(12.34, nullptr);
        connect(this, &MainWindow::updatevalue,
                m_viswindow, &VisWindow::updateValue);
        m_viswindow->show();
    }
    void MainWindow::on_pushButton_2_clicked() {
        emit updatevalue(23.45);
    }
    void MainWindow::on_pushButton_3_clicked() {
        m_viswindow->updateValue(45.67);
    }
    

    viswindow.h

    #ifndef VISWINDOW_H
    #define VISWINDOW_H
    #include <QWidget>
    namespace Ui {
    class VisWindow;
    }
    class VisWindow : public QWidget
    {
        Q_OBJECT
    public:
        explicit VisWindow(const double value, QWidget *parent = nullptr);
        ~VisWindow();
    public slots:
        void updateValue(const double value);
    private:
        Ui::VisWindow *ui;
    };
    #endif // VISWINDOW_H
    

    viswindow.cpp

    #include "viswindow.h"
    #include "ui_viswindow.h"
    VisWindow::VisWindow(const double value, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::VisWindow)
    {
        ui->setupUi(this);
        ui->lblValue->setText(QString::number(value,'f',2));
        this->setWindowTitle("VisWindow");
    }
    VisWindow::~VisWindow() {
        delete ui;
    }
    void VisWindow::updateValue(const double value) {
        ui->lblValue->setText(QString::number(value,'f',2));
    }