Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Edit variables from other widget of QStackedWidget
QtWS25 Last Chance

Edit variables from other widget of QStackedWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetqstackedwidget
14 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on 9 Dec 2019, 11:38 last edited by
    #1

    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.

    J 1 Reply Last reply 9 Dec 2019, 11:46
    0
    • H hobbyProgrammer
      9 Dec 2019, 11:38

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Dec 2019, 11:46 last edited by jsulm 12 Sept 2019, 11:47
      #2

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply 9 Dec 2019, 11:53
      2
      • J jsulm
        9 Dec 2019, 11:46

        @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).

        H Offline
        H Offline
        hobbyProgrammer
        wrote on 9 Dec 2019, 11:53 last edited by
        #3

        @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.

        J 1 Reply Last reply 9 Dec 2019, 12:06
        0
        • H hobbyProgrammer
          9 Dec 2019, 11:53

          @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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 9 Dec 2019, 12:06 last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          H 2 Replies Last reply 9 Dec 2019, 12:13
          0
          • J jsulm
            9 Dec 2019, 12:06

            @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.

            H Offline
            H Offline
            hobbyProgrammer
            wrote on 9 Dec 2019, 12:13 last edited by
            #5

            @jsulm Thank you! It works.

            1 Reply Last reply
            0
            • J jsulm
              9 Dec 2019, 12:06

              @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.

              H Offline
              H Offline
              hobbyProgrammer
              wrote on 9 Dec 2019, 13:53 last edited by
              #6

              @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;
              }
              
              J 1 Reply Last reply 9 Dec 2019, 14:17
              0
              • H hobbyProgrammer
                9 Dec 2019, 13:53

                @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;
                }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 9 Dec 2019, 14:17 last edited by
                #7

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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                H 1 Reply Last reply 9 Dec 2019, 14:20
                0
                • J jsulm
                  9 Dec 2019, 14:17

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

                  H Offline
                  H Offline
                  hobbyProgrammer
                  wrote on 9 Dec 2019, 14:20 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    hobbyProgrammer
                    wrote on 9 Dec 2019, 14:26 last edited by
                    #9
                    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;
                    }
                    
                    J 1 Reply Last reply 9 Dec 2019, 14:29
                    0
                    • H hobbyProgrammer
                      9 Dec 2019, 14:26
                      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;
                      }
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 9 Dec 2019, 14:29 last edited by
                      #10

                      @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);
                      

                      ?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply 9 Dec 2019, 14:39
                      0
                      • J jsulm
                        9 Dec 2019, 14:29

                        @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);
                        

                        ?

                        H Offline
                        H Offline
                        hobbyProgrammer
                        wrote on 9 Dec 2019, 14:39 last edited by
                        #11

                        @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.

                        J 1 Reply Last reply 10 Dec 2019, 06:42
                        0
                        • H hobbyProgrammer
                          9 Dec 2019, 14:39

                          @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.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 10 Dec 2019, 06:42 last edited by
                          #12

                          @hobbyProgrammer Check what connect() returns.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          H 1 Reply Last reply 10 Dec 2019, 07:57
                          0
                          • J jsulm
                            10 Dec 2019, 06:42

                            @hobbyProgrammer Check what connect() returns.

                            H Offline
                            H Offline
                            hobbyProgrammer
                            wrote on 10 Dec 2019, 07:57 last edited by
                            #13

                            @jsulm it returns true

                            J 1 Reply Last reply 10 Dec 2019, 15:22
                            0
                            • H hobbyProgrammer
                              10 Dec 2019, 07:57

                              @jsulm it returns true

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 10 Dec 2019, 15:22 last edited by
                              #14

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

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              5/14

                              9 Dec 2019, 12:13

                              topic:navigator.unread, 9
                              • Login

                              • Login or register to search.
                              5 out of 14
                              • First post
                                5/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved