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. How can I have access to values of my Mainwindow in another window?
QtWS25 Last Chance

How can I have access to values of my Mainwindow in another window?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.0k 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.
  • A Offline
    A Offline
    AmirKh
    wrote on last edited by
    #1

    Hi Guys,

    My question might seem simple for you but I'm stuck in it. I've searched around but since I'm new to QT and OOP I'm a bit confused and can't quiet understand whats going on!

    Well, I have written a program in QT Creator that has two windows. In one window the user enters some numbers in a QTablewidget (lets say some Xs and Ys) and when the user pushes the button "depict graph", I want to open a new window and depict the graph of Xs and Ys in the second window. I have managed to open the second window in QT and also to depict a random plot in that window. However, I want to get the values of what user entered in my Mainwindow and depict them in the second window. I know it has to do someting with Signals and SLots but I don't know how!

    Any help is much appreciated!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      There are several ways you can use to do it. Since you are plotting data from a table you should consider passing the model from your QTableWidget to your second window using e.g. a setter so that you'll read the data directly from it to populate your graph. Another option will be to extract the data from your table to e.g. a QVector<QVector> and pass that to your new window.

      Signals and slots are not really needed in this case.

      Hope it helps

      On a side note, it's Qt , QT stands for Apple QuickTime

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AmirKh
        wrote on last edited by
        #3

        HI SGaist and thanks a bunch for your quick and insightful response.

        Could you show me a sample code on how to do this? (I really appreciate to see some code on this!).

        To clarify again, my MainWindow has a tablewidget. In mainwindow.cpp, I can simply edit the the values of this Tablewidget using ui->tablewidget->item(i,j).

        However, in the second window that I have (which I have added plotdialog.cpp and plotdialog.h) I can't. I tried to create a pointer to the MainWindow class and read the table in this way but I can't.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And you shouldn't. Your PlotDialog doesn't need to know anything about your MainWindow.

          Should your graph be update dynamically ? i.e. If a user changes a value in the table should it be reflected automatically on the graph ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AmirKh
            wrote on last edited by
            #5

            No, its not that complex, just everytime the user clicks a push button in the mainwindow, a graph is depicted in the second window based on the values of a tablewidget in the mainwindow.

            Thanks again SGaist.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AmirKh
              wrote on last edited by
              #6

              I figured it out how to do it. I have created two functions in my second window class (plotDialog.cpp) named "setmydataset" and "plotgraph". I also considered two private variables in my second window class for xdata and ydata. In my Mainwindow I create an object of plotDialog type and set the variables xdata and ydata and then plot the graph based on them. The code is as follow:

              plotdialog.h
              ....
              public slots:

              //--- Sets the data vectors
              void setmydata(QVector<double>, QVector<double>);
              
              //--- Plot the data vectors
              void plotData();
              

              private:
              Ui::plotDialog *ui;
              QVector<double> xdata;
              QVector<double> ydata;
              ....

              and in the mainwindow.cpp

              plotDialog dialog(this);
              dialog.setmydata(x, y);
              dialog.plotData();
              dialog.exec&#40;&#41;;
              

              I have found this thread to be useful as well. (http://qt-project.org/forums/viewthread/2333).

              Now this works just fine. But since I am new to C++ as well. I am not quite sure what does the following line mean:

              plotDialog dialog(this);
              

              I assume we should create an object of type plotDialog. I know this would do it, I just dont get the meaning of syntax.

              Thanks SGaist :)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by
                #7

                plotDialog dialog(this);

                Above line mostly likely will result in a big baraboom when dialog is going out of scope.
                It will result in deleting dialog twice. If it works I guess there is something wrong with platform or compiler.
                You either do not provide parent or use operator new when creating QObject subclasses.
                Counting that exec was called modal behavior is desired, so

                @
                /* Instantiate object of plotDialog class named dialog with no parent */
                plotDialog dialog();
                ....
                @

                1 Reply Last reply
                0

                • Login

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