Inheritance with multiple windows.
-
I have a Qt Widgets program that has three windows. One main window (mainwindow.h, mainwindow.cpp), an editor (editor.h, editor.cpp), and an about window (about.h, about.cpp), with respective forms attached to them.
The MainWindow class has a two QList variables, "defs" and "words".
The Editor class needs to edit both of those variables. When a button is pressed, this code is used:
@MainWindow::words.append(st);@
"st" being a string.
In the "editor.cpp" file, I included the "mainwindow.h" file, the header file with the variables "defs" and "words" declared.
I am getting the errors:
"invalid use of non-static data member 'MainWindow::words'"
"from this location
@MainWindow::wordsappend(st);@"How can I access the MainWindow::words and MainWindow::defs variables without generating an error?
-
Hi, you need a copy of MainWindow's this pointer, i.e. you cannot access the words QList using "MainWindow::" prefix, instead you have to use a copy of the this pointer. for example:
@
pMainWindow->words.append(st):
@To get the this pointer into your Editor class, you can do like the code ui->setupUI(), it copies the this pointer into the Ui_MainWindow class.
-
I used this code:
@MainWindow *pMainWindow;
pMainWindow->words.append(st);@and the program unexpectedly crashed. (I am using Windows 7.)
I know that pointers need to be initialized, but I do not know what to do.
Thank you for the response.
-
Well, I think that's because you didn't allocated the space for you pointer. So you try this:
@
MainWindow *pMainWindow = new MainWindow;
pMainWindow->words.append(st);
@
You should give I look at this:
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/ -
It seems you're still struggling with the basics of c++, it might help to brush up on that first.
-
I understand that mistake, using 'new'.
Can somebody explainhow to get the 'this' pointer from the MainWindow class in my Editor class?
Inheritance will not be solution, I think, because the Editor class already inherits the QDialog class.
-
Hi, you could add a function to your Editor class, that accepts a MainWindow flavored this pointer, like this:
@
void setMainWindowPtr(MainWindow *p);
@ -
What should be in the body of the function?
Because the parameter for the function is a MainWindow, I would have to create a MainWindow variable, right?
-
A variable that is a pointer to a MainWindow.
i.e.
@
MainWindow *pMw;
@
and such...May I ask what your programming experience is?
I would respectfully suggest that if you are just learning C++ you will may find it difficult to learn both at the same time. -
I learned C++ about one year ago, then learned Java, and now working on learning Android Programming.
-
I see. Well is is good to know several useful languages. Good luck with your Qt work.