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:
// myClass.h
// ...
public slots:
void receiveFromMain( const std::vector< myStruct > & info );
//...
// myClass.cpp
void myClass::receiveFromMain( const std::vector< mSystruct > & info )
{
// do stuff with mystruct contents
}
// MainWindow.h
#include <vector>
// ...
signals:
void sendToClass( const std::vector< myStruct > & info );
private:
std::vector< myStruct > data;
myclass * mcObj;
};
// MainWindow.cpp
{
// setup ui, etc.
mcObj = new myclass( this ); // Create on the heap, not the stack
data.resize( 5 );
connect(this, &MainWindow::sendToClass, mcObj, &myclass::receiveFromMain); // do this only once.
}
void MainWindow::on_pushButton_clicked()
{
startProgram();
}
// myClass.h
// ...
public slots:
void receiveFromMain( const std::vector< myStruct > & info );
//...
// myClass.cpp
void myClass::receiveFromMain( const std::vector< mSystruct > & info )
{
// do stuff with mystruct contents
}
// MainWindow.h
#include <vector>
// ...
signals:
void sendToClass( const std::vector< myStruct > & info );
private:
std::vector< myStruct > data;
myclass * mcObj;
};
// MainWindow.cpp
MainWindow::MainWindow( QWidget * parent ) : QMainWindow( parent )
{
// setup ui, etc.
mcObj = new myclass( this ); // Create on the heap, not the stack
data.resize( 5 );
connect(this, &MainWindow::sendToClass, mcObj, &myclass::receiveFromMain); // do this only once.
}
void MainWindow::on_pushButton_clicked()
{
startProgram();
}
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.
Bookmarks