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.
-
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/ -
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.