Results 1 to 11 of 11

Thread: Passing data between MainWindow and my class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2016
    Posts
    57
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Question Passing data between MainWindow and my class

    So I have a simple class with one variable called param1, constructor, and two functions to set/get that parameter:

    So here are the sources of ctester.h and ctester.cpp
    Qt Code:
    1. #ifndef CTESTER_H
    2. #define CTESTER_H
    3.  
    4.  
    5. class CTester {
    6.  
    7. public:
    8. CTester(); // constructor
    9. void SetParam1(int value);
    10. int GetParam1(void);
    11.  
    12. private:
    13. int param1;
    14.  
    15. };
    16.  
    17. #endif // CTESTER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. CTester::CTester(void)
    2. {
    3. // initializing all the parameters
    4. param1 = 0;
    5. }
    6.  
    7. void CTester::SetParam1(int value)
    8. {
    9. param1 = value;
    10. }
    11.  
    12. int CTester::GetParam1(void)
    13. {
    14. return param1;
    15. }
    To copy to clipboard, switch view to plain text mode 

    My main.cpp and mainwindow.cpp files are rather simple, since I just started a QMainWindow application in QtCreator:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    What I am not quite sure is, what is the most proper way of getting/setting that param1 variable in my class through that MainWindow form?
    The search on QtCentre gives tons of similar threads BUT about sharing data between dialogs or dialogs and main window, but I did not see any description in docs or the forum threads about reading/writing to own class.

    In other words, I have my Qt GUI interface, but what I want to achieve now is create now personal c++ class which is "tied" to that GUI and updated by it. I do have two buttons on the MainWindow form, Set and get, and the editbox for setting the value and label for showing the value of param1 when get button is clicked.

    Keep in mind that I want to keep ctester.h and ctester.cpp as c++ source files only, i.e. no Qt related macros/keywords there.

    Now, does it mean that I need another "intermediate" wrapper class for this mechanism to work? Or there is a better, more elegant approach to achieve what I just described?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing data between MainWindow and my class

    So where does your instance of the CTester class live? Have you even declared an instance of it anywhere? If you haven't declared an instance of it, then how do you expect to call its Get and Set methods from any other class at all?

    There is no magic to mixing pure C++ and Qt classes; the Qt libraries -are- C++ and the macros (like Q_OBJECT) are just aids to paste a lot of boilerplate code into header files and elsewhere. Signals and slots are just ordinary C++ methods and are called from within the Qt framework just like any other C++ method.

    Of course, you have to have an instance of a class first before you can call any of its non-static members. Where do you want your instance of CTester to live?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2016
    Posts
    57
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing data between MainWindow and my class

    I declared instance of CTester inside private section of QMainWindow:
    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. CTester aa;
    To copy to clipboard, switch view to plain text mode 

    Then on the form designer I right click on the Get button and choose "GoTo SLot->clicked()" and enter this code:
    Qt Code:
    1. void MainWindow::on_param1Get_clicked()
    2. {
    3. aa.GetParam1();
    4. QString s = QString::number(aa.GetParam1());
    5. ui->param1Label->setText(s);
    6. }
    To copy to clipboard, switch view to plain text mode 

    It does give me a LNK2019 error on compile.
    And also, I have a feeling that I'm trying to achieve this in an ugly way and there gotta be an elegant solution.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing data between MainWindow and my class

    LNK2019
    And what else does it say besides "LNK2019"? Probably something about can't find a CTester constructor or a CTester:: GetParam() method, right? So did you add "Tester.cpp" or whatever you called the source file to the project? Did you add the paths to and names of your Qt library ".lib" files to the link properties? (Qt5Core, Qt5Gui, and Qt5Widgets, at a minimum for a GUI project).

    By the way, you -do- realize that the code in your line 3 does nothing, right? Gets the parameter value, doesn't assign it to anything, then throws it away once the statment is done.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing data between MainWindow and my class

    Quote Originally Posted by r2com View Post
    And also, I have a feeling that I'm trying to achieve this in an ugly way and there gotta be an elegant solution.
    Aside from the linker error and the unnecessary line that d_stranz mentioned this looks fine.

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 5th September 2012, 20:47
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  3. Replies: 4
    Last Post: 23rd August 2011, 21:53
  4. Replies: 6
    Last Post: 10th February 2011, 11:10
  5. Passing Pixmaps between MainWindow and Dialog
    By ramstormrage in forum Qt Programming
    Replies: 28
    Last Post: 20th April 2008, 13:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.