PDA

View Full Version : Best way to acces MainWindow instance from separate class



scarecr0w132
13th January 2014, 08:06
Hello,

What is the best way to access MainWindow instance from a separate class.

Main.cpp


...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
...


How can I do this?

Thank you have a good day!

wysota
13th January 2014, 08:09
Pass a pointer to the window instance to the other class instance or use signals and slots.

scarecr0w132
13th January 2014, 10:34
Pass a pointer to the window instance to the other class instance or use signals and slots.

Hello,

How can you use signals and slots to accomplish this?

Thank you

wysota
13th January 2014, 11:47
You connect signals from the window instance to the other object and vice-versa. All depending on what functionality you want to implement.

scarecr0w132
14th January 2014, 03:06
MainWindow class contains a QTableWidget, I am trying to access the tables data from another class. It has to be accessing the current instance of the class.

Thank you

ChrisW67
14th January 2014, 04:47
The answer hasn't changed.

You use generic C++ mechanisms to achieve this. Expose the data through a public interface and give the other class instance a pointer/reference to the source instance.
OR
When something happens in the table widget that the other class needs to hear about you emit a signal which is, in turn, connected to a slot in the other class. The signal can carry relevant data as parameters.

If the entire content of the QTableWidget needs to be shared between views then you should be using QTableView and a separate shared model.

Exactly how you do it is strongly dependent on what you need to achieve.

scarecr0w132
14th January 2014, 06:04
Thank you I understand now.