Results 1 to 4 of 4

Thread: struct type object with signal/slot method

  1. #1
    Join Date
    Apr 2017
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question struct type object with signal/slot method

    Hi, i'm new to Qt (started on: C- 5 years ago; C++ June last year; Qt - since this January) and i got some questions... i'm trying stuff for the past week but unsuccessful:
    1. How can i make a struct visible in another class so i can create instances of it in diferent headers? (Like in mainwindow.h above) (This is a c++ question, i know..)
    2. After i got it working, i saw in this forum that i got to add my struct to qt metatype so i can use signal/slot. I got to do it inside the header or the cpp file where i declared it?

    I just made a clean project:
    main.cpp (default code)
    |-mainwindow.ui (just a start button on it)
    |-mainwindow.h
    |-mainwindow.cpp
    |-myclass.h
    |-myclass.cpp



    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QString>
    6. #include <QDebug>
    7. #include "myclass.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12. struct myStruct{
    13. int id;
    14. QString name;
    15. float height;
    16. };
    17.  
    18. class MainWindow : public QMainWindow
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit MainWindow(QWidget *parent = 0);
    24. ~MainWindow();
    25.  
    26. void startProgram();
    27.  
    28. signals:
    29. void sendToClass(myStruct data[5]);
    30.  
    31. private slots:
    32. void on_pushButton_clicked();
    33.  
    34. private:
    35. void send();
    36. Ui::MainWindow *ui;
    37. myclass mcObj;
    38. myStruct data[5];
    39. };
    40.  
    41. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    mainwindow.cpp:
    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. }
    15.  
    16. void MainWindow::startProgram()
    17. {
    18. data[0].id = 1001;
    19. data[0].name = "Mark";
    20. data[0].height = 1.78f;
    21.  
    22. emit sendToClass(data);
    23. }
    24.  
    25. void MainWindow::on_pushButton_clicked()
    26. {
    27. connect(this, &MainWindow::sendToClass, &mcObj, &myclass::receiveFromMain);
    28. startProgram();
    29. }
    To copy to clipboard, switch view to plain text mode 


    myclass.h:
    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. #include <QObject>
    5.  
    6. class myclass : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit myclass(QObject *parent = 0);
    11.  
    12. void startStuff();
    13.  
    14. public slots:
    15. void receiveFromMain();
    16. };
    17.  
    18. #endif // MYCLASS_H
    To copy to clipboard, switch view to plain text mode 


    myclass.cpp:
    Qt Code:
    1. #include "myclass.h"
    2.  
    3. myclass::myclass(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. void myclass::startStuff()
    9. {
    10.  
    11. }
    12.  
    13. void myclass::receiveFromMain()
    14. {
    15. //Want to receive the struct here!
    16. }
    To copy to clipboard, switch view to plain text mode 


    I left it in blank because i just don't know how to make it!

    Basically, in my real project i want to be able to receive position parameters from a socket class running with QtConcurrent, and pass it to several classes.. i could just pass: int id, QString Name, float x, float y, float z.... but it is much more clean with a struct or class!

    that struct can be anywhere (or even transform it to a class, better for me..).. i just want it to be seen in almost every project files.. although i don't want to create global variables.. I just tried several ways and i can't do it.




    Thank you in advance!

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

  3. The following user says thank you to d_stranz for this useful post:

    eliosm (20th April 2017)

  4. #3
    Join Date
    Apr 2017
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: struct type object with signal/slot method

    Hi d_stranz, i tried to delete this post next morning but without any luck. I was just too tired! 4 days programming all the time and sleeping 5/6 hours a day.. not good! I knew I had to rest! After some beers, girlfriend, and 9h of sleep i tried again and i just solved it in minutes! I did it with struct, with class, and even completed the threading stuff i wanted to do...

    Anyway, you created a standard vector... why not QVector?

    Thank you for reply!

  5. #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: struct type object with signal/slot method

    Anyway, you created a standard vector... why not QVector?
    No reason except that I use the C++ standard template library (STL) in preference to the Qt library when equivalent classes exist in both. Much of the non-GUI code I write has to be portable as libraries used in non-Qt applications, so I have just adopted the habit of using the STL everywhere possible. If I were to write something using Qt container classes that I later decided should be shared in a library, then I have a bunch of rewriting and testing to convert it to STL when I could have done it that way in the first place.

    That said, QVector has the same semantics as std::vector and can be used in any of the STL or boost algorithms that operate on containers that implement random iterator access.
    <=== 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.

  6. The following user says thank you to d_stranz for this useful post:

    eliosm (21st April 2017)

Similar Threads

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

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.