Results 1 to 2 of 2

Thread: C++ easy question Classes

  1. #1

    Default C++ easy question Classes

    I have a MainWindow class and in the class I defined in public part QString app_language

    Qt Code:
    1. public:
    2. explicit MainWindow(QWidget *parent = 0);
    3. ~MainWindow();
    4. QString app_language;
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ easy question Classes

    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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow instanceOfMainWindowClass;
    5.  
    6. instanceOfMainWindowClass.app_language = "romanian";
    7.  
    8. instanceOfMainWindowClass.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Replies: 0
    Last Post: 23rd October 2011, 20:09
  2. Easy yes/no QSortFilterProxyModel question...
    By scott_hollen in forum Newbie
    Replies: 3
    Last Post: 14th March 2011, 17:11
  3. Question about classes in Qt
    By waynew in forum Newbie
    Replies: 18
    Last Post: 12th November 2010, 23:08
  4. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05
  5. Replies: 2
    Last Post: 14th February 2008, 20:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.