Edit variables from other widget of QStackedWidget



  • Hi,
    I have 1 MainWindow with a stacked widget and 2 widgets.
    The second widget needs to read a variable from the first widget, but I don't know how to do that.
    I'm starting out with a quite simple version of this principle. 1 widget is a calculator that stores it's latest result. The other widget is a widget to show the result.
    Both widgets are called via menu bar actions.

    My MainWindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QAction>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connectActions();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::goToCalculator()
    {
        ui->stackedWidget->setCurrentIndex(0);
    }
    
    void MainWindow::goToResult()
    {
        ui->stackedWidget->setCurrentIndex(1);
    }
    
    void MainWindow::connectActions()
    {
        connect(ui->actioncalculator, &QAction::triggered,this, &MainWindow::goToCalculator);
        connect(ui->actionshow_result, &QAction::triggered, this, &MainWindow::goToResult);
    }
    

    my Calculator.cpp:

    #include "calculator.h"
    #include "ui_calculator.h"
    
    Calculator::Calculator(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Calculator)
    {
        ui->setupUi(this);
    }
    
    Calculator::~Calculator()
    {
        delete ui;
    }
    
    void Calculator::on_pushButton_equals_clicked()
    {
        result = 1+1;
    }
    

    my showResult.cpp:

    #include "showresult.h"
    #include "ui_showresult.h"
    #include "calculator.h"
    
    showResult::showResult(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::showResult)
    {
        ui->setupUi(this);
        setUp();
    }
    
    showResult::~showResult()
    {
        delete ui;
    }
    
    void showResult::setUp()
    {
        //insert code here to retreive result from calculator class
        Calculator calc;
        int result = calc.result;
        QString setText = QString::number(result);
        ui->label->setText(setText);
    }
    

    I know that currently, I am not using the Calculator instance that is used by MainWindow, but how can I use that one?

    Also: in Calculator.h int result is public.


  • Qt Champions 2019

    @hobbyProgrammer said in Edit variables from other widget of QStackedWidget:

    from the first widget, but I don't know how to do that

    Using signals/slots.
    Calculator simply emits signal whenever a new result is available and whoever needs that information connects to the signal.
    This way both classes do not even need to know anything about each other (if you do the connection in main window).


  • Qt Champions 2019

    @hobbyProgrammer said in Edit variables from other widget of QStackedWidget:

    from the first widget, but I don't know how to do that

    Using signals/slots.
    Calculator simply emits signal whenever a new result is available and whoever needs that information connects to the signal.
    This way both classes do not even need to know anything about each other (if you do the connection in main window).



  • @jsulm alright, thank you. But won't that instantly trigger the showResult?
    I would like to show the result only when someone chooses it in the menubar.


  • Qt Champions 2019

    @hobbyProgrammer said in Edit variables from other widget of QStackedWidget:

    But won't that instantly trigger the showResult?

    No, why should it? connect() does not call anything. It just connects the signal to the slot. You have to emit the signal in order the slot being called. This is how signal/slot works.



  • @jsulm Thank you! It works.



  • @jsulm on my advanced construction it doesn't seem to work.
    I have a mainWindow and a QWidget. The widget contains a promoted QGraphicsView. I'd like to retreive information from the QGraphicsView to the mainWindow.
    I already tried by emitting a signal in QGraphicsView and connect it in the QWidget to a slot that sets a public integer to that value and send a signal for MainWindow. Then I go to the mainWindow and connect the signal from QWidget to a slot that handles the rest.

    It doesn't go to the signal in the QWidget.

    this is the code in the QWidget:

    #include "overview.h"
    #include "ui_overview.h"
    
    Overview::Overview(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Overview)
    {
        ui->setupUi(this);
        overviewView *ovView = qobject_cast<overviewView*>(ui->graphicsView);
        connect(ovView, &overviewView::valAdded, this, &Overview::giveSignalToMainWindow);
    }
    
    Overview::~Overview()
    {
        delete ui;
    }
    
    void Overview::giveSignalToMainWindow()
    {
        qDebug() << "overview";
        emit editFinished();
        val = ui->graphicsView->value;
        qDebug() << "Overview ui->graphicsView->value: "<< ui->graphicsView->value;
    }
    

  • Qt Champions 2019

    @hobbyProgrammer I'm not sure I understand: do you mean slot connected to editFinished() is not called?



  • @jsulm No I mean that Overview::giveSignalToMainWindow is not even called.



  • void overviewView::loadFromCSV()
    {
        QString fileName = "C://users//user1//downloads//data.csv";
        QFile csvFile(fileName);
        QStringList dataList;
        if(csvFile.open(QIODevice::ReadOnly))
        {
            QString data;
            data = csvFile.readAll();
            dataList = data.split('\n');
            csvFile.close();
        }
    
        for(int i = 0; i < dataList.size(); i++)
        {
            QString data2 = dataList.at(i);
            QStringList dataList = data2.split(",");
            for(int j = 0; j < dataList.size(); j++)
            {
                val = dataList.at(j);
            }
        }
    
        emit valAdded();
        qDebug() << "overviewview: " << val;
    }
    

  • Qt Champions 2019

    @hobbyProgrammer You made sure overviewView::valAdded was called?
    Do you really have only one overviewView instance (I'm asking because this is a mistake people often do :-))?

    Is ovView not nullptr here:

    overviewView *ovView = qobject_cast<overviewView*>(ui->graphicsView);
    

    ?



  • @jsulm I added qDebug() << ovView
    and it gave me this:

    overviewView(0x1fef8160, name="graphicsView")
    

    so it doesn't seem to be a nullptr.
    Also I have only 1 ovView.


  • Qt Champions 2019

    @hobbyProgrammer Check what connect() returns.



  • @jsulm it returns true


  • Qt Champions 2019

    @hobbyProgrammer Do you actually have event loop running when you emit the signal?