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

                          12/14

                          10 Dec 2019, 06:42

                          • Login

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