Results 1 to 3 of 3

Thread: How to access UI elements from subclass.

  1. #1
    Join Date
    Dec 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default How to access UI elements from subclass.

    Hello!

    Trying to learn QT.

    My current project has one GUI and I need to be able to access elements of the UI from subclasses. None of the subclasses have a UI.

    How do I pass Ui::MainWindow to subclasses? Basically they need to be able to update labels or text fields.

    I've tried to pass it through the constructor like below:

    Qt Code:
    1. SubClass::SubClass(Ui::MainWindow mw) {
    2. this->mw = mw;
    3. }
    To copy to clipboard, switch view to plain text mode 
    With it defined in the header file:
    Qt Code:
    1. class SubClass {
    2. public:
    3. SubClass(Ui::MainWindow mw);
    4. Ui::MainWindow mw;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Which of course doesn't work. I'm guessing there's a proper way to do this but I haven't been able to find it in any of the tutorials or documentation.

    Any help would be appreciated.
    Thanks

  2. #2
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access UI elements from subclass.

    Do not do it this way. Try to check the signal-slot mechanism.
    Last edited by ado130; 1st December 2017 at 18:11.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access UI elements from subclass.

    I'm guessing there's a proper way to do this but I haven't been able to find it in any of the tutorials or documentation.
    That's because none of the tutorials or documentation teach such bad programming practice.

    You never want to expose the details of the UI to any class other than the one that implements it. If your UI needs to communicate changes to other parts of your program, the way to do it in Qt is to implement signals and slots.

    A QLineEdit tells the world that the user has changed its contents by sending signals, like QLineEdit::editingFinished(). If the widget that contains the line edit needs to know this, you implement a slot and connect it to that signal.

    If some class outside of the one that owns the line edit needs to know, you don't expose the line edit instance (or its signal) to that class. Instead, you can implement a signal for the class that owns the line edit, and in your slot that handles the line edit's signal, you can emit your own signal. The classes that want to know can then make a connection to your signal instead.

    By doing it this way, you can hide the implementation of your UI from the outside world; all the world needs to know about is that your class has a signal that will be emitted every time that important piece of information changes. You are then free to change your UI in any way you want, as long as you keep your signal the same.

    Read the Qt documentation on signals and slots.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Access to UI elements
    By ivanotero76 in forum Newbie
    Replies: 1
    Last Post: 6th February 2017, 13:11
  2. Access to UI elements from another class c++
    By nasil122002 in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2013, 15:43
  3. Access elements of the xml-file
    By 8Observer8 in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2013, 18:27
  4. SVG: access coordinates of elements
    By bmpix in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2012, 23:29
  5. Replies: 4
    Last Post: 6th August 2009, 06:12

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.