Results 1 to 11 of 11

Thread: Passing data between MainWindow and my class

  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,
    _

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

    Question Re: Passing data between MainWindow and my class

    OK, nevermind, it turns out that I had to run Build->Run qmake
    And after that include header file in ctester.cpp, now it all works.

    but my another question still holds, can you show me any other similar or good methods to achieve what I am trying to achieve? I just want to know all proper solutions available to me.

    Also, I want to know methods to achieve this without adding any Qt related syntax to ctester.cpp/ctester.h

    Because in a way how i got it to work now, its strange...there is now a "private slots" but who is a signal then? signal comes from user? from OS?
    Last edited by r2com; 26th July 2016 at 21:24.

  7. #7
    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
    but my another question still holds, can you show me any other similar or good methods to achieve what I am trying to achieve? I just want to know all proper solutions available to me.
    Well, C++ offers you two options:

    1) public members in CTester which the other class can directly access
    2) setter/getter methods

    Quote Originally Posted by r2com View Post
    Also, I want to know methods to achieve this without adding any Qt related syntax to ctester.cpp/ctester.h
    That's what you have right now, no?

    Quote Originally Posted by r2com View Post
    Because in a way how i got it to work now, its strange...there is now a "private slots" but who is a signal then? signal comes from user? from OS?
    The only slot I see in your code is the one connected to the button that you want to use to read the data.
    What other private slot are you referring to?

    Cheers,
    _

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

    Default Re: Passing data between MainWindow and my class

    Alright, so according to your response, here is what I understand is available to a programmer to achieve task:

    Flow A:

    1) public members in CTester which the other class can directly access
    2) setter/getter methods

    Flow B:
    1)Modify CTester class to be derived from QObject, insert slots there,a nd tie signal/slots to my MainWindow ui.

    Correct?

    In my case I used Flow A, method #2

  9. #9
    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

    Yes, that's about it.

    Cheers,
    _

  10. #10
    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

    There is nothing wrong with "Flow A" and public getter / setter methods. I use non-Qt C++ classes everywhere in my code.

    I need the algorithms I write to be portable, not just to a different OS, but to a different GUI platform (like Windows MFC). So I embed them in Qt GUI classes either as member variables or in some cases as stack variables in a method if I don't need to have a persistent instance in my Qt class. I use signals and slots in the Qt class so that other parts of my GUI can communicate with the Qt class that owns the C++ instance.

    If I have a GUI element (a QLineEdit for example) that the user can edit to change the temperature, the form where the edit lives listens for the "editingFinished()" signal from the line edit, and if the value is OK, emits a signal "temperatureChanged( double )". The MainWindow has a slot, "onTemperatureChanged( double )" that is connected to this signal. In that slot, it calls the C++ method "setTemperature( double )" of the CTemperatureControl instance it owns. CTemperatureControl doesn't know anything about Qt, but it does know what to do when someone sets a new temperature on it.

    As an aside, there are a few things about what I have just described that I think are part of a good design that encapsulates things locally and doesn't spread knowledge of what each part of the program is doing all over the rest of it:

    1 - The QLineEdit is owned by a form of some kind (based on QWidget, usually). The QLineEdit tells the form its value has changed. It is up to the form to relay that information to whomever is listening. And it does so in an application-specific way: a "temperatureChanged()" signal, not a "lineEdit_1_text_changed()" signal.

    2 - So a revised design suggested by the project manager says to replace QLineEdit with a QDoubleSpinBox because the stupid testers were typing negative temperatures and their dog's names into the edit box to see if they could break the system. No problem, all you do is update the form to listen for the slider's valueChanged() signal then emit "temperatureChanged()" and the rest of the program doesn't care.

    3 - The MainWindow is just listening for a "temperatureChanged()" signal from some QObject. It doesn't know it's a form with an edit or spin box. So you could replace that form with a widget that listens to a feed sent from your Nest thermostat and which emits "temperatureChanged()" every time the user presses the Up or Down button.

    4 - And because the MainWindow talks to the CTemperatureControl instance through a plain old C++ method call, you could throw that whole GUI away and replace it with something that did the same thing, and the temperature control wouldn't know anything different, either.

    (Why doesn't this forum have [SOAPBOX] and [/SOAPBOX] tags? I've been watching too much politics here in the US).
    <=== 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.

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

    Default Re: Passing data between MainWindow and my class

    thanks anda_skoa and d_stranz for explanations

Similar Threads

  1. Replies: 11
    Last Post: 5th September 2012, 21:47
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 17:15
  3. Replies: 4
    Last Post: 23rd August 2011, 22:53
  4. Replies: 6
    Last Post: 10th February 2011, 12:10
  5. Passing Pixmaps between MainWindow and Dialog
    By ramstormrage in forum Qt Programming
    Replies: 28
    Last Post: 20th April 2008, 14: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.