PDA

View Full Version : How to get MainWindow of Application?



rajeshs
20th December 2007, 11:51
Hi all,


I am using Qt 4.3 , I want to get MainWindow of the particular Application?

How to do this?

marcel
20th December 2007, 11:53
Simple. Make a QMainWindow singleton subclass.

rajeshs
20th December 2007, 11:56
sorry , I want to get instance of mainwindow of the application from some other classes in the same application.

marcel
20th December 2007, 12:26
Yes, I know. And you should do it by implementing your QMainWindow subclass as a singleton.

rajeshs
20th December 2007, 12:34
we can make qMainWindow as singleton , how to get instance.:)

marcel
20th December 2007, 12:41
class MyWindow : public QMainWindow
{
private:
static MyWindow* instance;
protected:
MyWindow(QWidget* = NULL);
public:
static MyWindow* getInstance()
{
if(!instance)
{
instance = new MyWindow();
}
return instance;
}
};

and in the cpp don't forget to initialize the static member to NULL.

Next, you can use this in other classes just by including the MyWindow header:


#include "MyWindow.h"
...
MyWindow* mainWindow = MyWindow::getInstance();
...