Results 1 to 4 of 4

Thread: struct type object with signal/slot method

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,323
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: struct type object with signal/slot method

    Passing a struct is no different from passing any other type of variable. Defining a metatype is only needed if you want to serialize it using Qt's stream serialization methods. And signals and slots are nothing except ordinary C++ methods with some fancy Qt macros to keep moc and the preprocessor busy when they compile your code. So you writ something like this:

    Qt Code:
    1. // myClass.h
    2.  
    3. // ...
    4. public slots:
    5. void receiveFromMain( const std::vector< myStruct > & info );
    6. //...
    7.  
    8.  
    9. // myClass.cpp
    10.  
    11. void myClass::receiveFromMain( const std::vector< mSystruct > & info )
    12. {
    13. // do stuff with mystruct contents
    14. }
    15.  
    16. // MainWindow.h
    17. #include <vector>
    18.  
    19. // ...
    20.  
    21. signals:
    22. void sendToClass( const std::vector< myStruct > & info );
    23.  
    24. private:
    25. std::vector< myStruct > data;
    26. myclass * mcObj;
    27. };
    28.  
    29.  
    30. // MainWindow.cpp
    31.  
    32. MainWindow::MainWindow( QWidget * parent ) : QMainWindow( parent )
    33. {
    34. // setup ui, etc.
    35. mcObj = new myclass( this ); // Create on the heap, not the stack
    36. data.resize( 5 );
    37.  
    38. connect(this, &MainWindow::sendToClass, mcObj, &myclass::receiveFromMain); // do this only once.
    39. }
    40.  
    41. void MainWindow::on_pushButton_clicked()
    42. {
    43. startProgram();
    44. }
    To copy to clipboard, switch view to plain text mode 

    I changed from a fixed sized structure for data to a variable-sized one that uses std::vector. You could use QVector instead. Your mcObj instance should be allocated on the heap using new() instead of on the stack as you had it. Giving it the instance of MainWindow ("this") as a parent ensures it will be destroyed when the MainWindow instance is. Otherwise, you were almost there.
    <=== 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. The following user says thank you to d_stranz for this useful post:

    eliosm (20th April 2017)

Similar Threads

  1. How Signal/slot is type-safe????
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 20th January 2025, 23:27
  2. Replies: 1
    Last Post: 14th August 2014, 17:08
  3. Replies: 4
    Last Post: 26th June 2014, 18:27
  4. Signal/slot or direct method call within a class
    By mike_the_tv in forum Newbie
    Replies: 6
    Last Post: 11th March 2010, 18:49
  5. Replies: 3
    Last Post: 15th April 2007, 19:16

Tags for this Thread

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.