Two Windows
-
Sorry if this is another Q with same old A.
I have MainWindow and with one function it create one another Window. In that Window I have a button and that button should be able to access some properties of MainWindow, like move position or change Window Title. But I couldn't. I cant access it. I've include header file of MainWindow in second Window cpp and h file but it is the same problem. I can see it but compiler report error:
For example on:
MainWindow.setWindowTitle("Hi");it reports:
"error: expected unqualified-id before '.' token"So where is the problem...???
-
MainWindow probably is a class - not an object.
You can't call non-static methods from a class. You need an instance (object) of that class!
In your case the obvious solution would be as follows:
When your instance of MainWindow creates the second window, it needs to pass a pointer to itself into the constructor of the second window. The second window can remember that pointer in some member variable. To access the MainWindow instance at later time, the second window will simply use the pointer it stored before.
Actually you don't need to code that yourself, because in Qt all Widgets already store a parent pointer :-)
-
so what is the code for example in order to understand more clearly??
-
The basic idea is like this:
@class A
{
public:
A(void)
{
/constructor of A/
}void createB(void) { m_b = new B(this); } void sayHello(void) { printf("Hello, this is A talking!\n"); }
private:
B *m_b; //Here we'll store the pointer to B, once we create it
}class B
{
public:
B(A *a)
{
m_a = a; //remember the pointer to the instance of A!
}void doSomethingWithA(void) { m_a->sayHello(); }
private:
A *m_a; //Here we'll store the pointer to the A who created us
}@But remember that a Qt widget will already store a pointer to it's parent!
-
You did this on purpose... :D
OK, if I understand clearly in my case it is like this:QMainWindow *m;
m = MainWindow;
m->setWindowTitle("Hi");Or not?
Remember, I am beginner
-
Well, maybe you should read a basic C++ tutorial first, before you get deeper into Qt.
Anyway, your line "m = MainWindow" doesn't make any sense. You can't assign a pointer to a class.
But to make a long story short, in your case the solution could be:
@void TheSecondWindow::doSomethingWithMainWindow(void)
{
MainWindow m = dynamic_cast<MainWindow>(this->parent());
if(m != NULL)
{
m->setWindowTitle(“Hi”);
}
else
{
printf("Error: Parent was not set or parent is not of type MainWindow!");
}
}@...given that you set the parent properly when you create the second window!
@void MainWindow::createSecondWindow(void)
{
TheSecondWindow *m_second = new TheSecondWindow(this);
}@ -
OK, thanks... actually I am familiar with dynamic_cast when I use it years ago with Borland C++ Builder in Windows but when I left Windows and switch on Linux everything change for me and also I never use casting in this case... tnx I wil try!
-
With this:
TheSecondWindow *m_second = new TheSecondWindow(this);m_second is showing inside of first Window and that is not what I want but it is working (I have access now)
I understand why is acting like that (first is parent of the second) but how to make it not showing inside of the first one?
QT is really new to me!
TNX for helping :D
-
If you want a widget to be a "top-level" widget, i.e. a widget without a parent, you need to set its parent to NULL. Of course then you won't be able to access the MainWindow instance via parent() anymore. Bummer!
Workaround: Simply add your own setter method and a suitable member variable.
@class TheSecondWindow
{
[...]void setMainWindow(MainWindow *m); m_mainWindow *MainWindow; [...]
};
void TheSecondWindow::setMainWindow(MainWindow *m)
{
m_mainWindow = m;
}void TheSecondWindow::doSomethingWithMain(MainWindow *m)
{
m_mainWindow->setWindowTitle(“Hi”);
}@In the MainWindow you'd change your code like this:
@TheSecondWindow *m_second = new TheSecondWindow(NULL);
m_second->setMainWindow(this);@ -
Cool, look great, will try tomorrow, now is too late (or early) at my place (2:00AM)
tnx