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



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


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

    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 ?



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



  • 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 :)



  • 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();
    ....
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.