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
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 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.

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

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 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.

  4. #4
    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
  •  
Qt is a trademark of The Qt Company.