View Full Version : C++ easy question Classes
arturs
30th April 2015, 20:34
I have a MainWindow class and in the class I defined in public part QString app_language
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QString app_language;
I would like to access to QString app_language; from another Class
I tried to use the following code:
MainWindow.app_language;
but It does not work
How to do it ?
Regards
Artur
Zlatomir
30th April 2015, 21:09
You can't access a non-static member like that, you need an instance of the MainWindow class (or a reference or a pointer to an instance).
For example in main you'll access it like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow instanceOfMainWindowClass;
instanceOfMainWindowClass.app_language = "romanian";
instanceOfMainWindowClass.show();
return a.exec();
}
In another class you'll need to pass a pointer or reference to the instance of MainWindow to the constructor (or create a method to set the pointer) of your second class (constructor approach is similar to how you pass the parent pointer with Qt), i'll let you try that for an exercise ;)
And i really recommend you read a C++ book (like Thinking in C++ - it can be found for free in electronic format) or else Qt will seem very hard or read Introduction to design patterns in C++ with Qt (it's available for free from ICS, you'll need to register on their site: http://www.ics.com/design-patterns ) this one starts with C++ and introduces Qt right after.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.