Results 1 to 4 of 4

Thread: pass ui to subclass

  1. #1
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default pass ui to subclass

    I would like to split up the code of my application into multiple classes, each class containing a dockwidget with its underlying functionality.

    For that I would like to have access to the original "ui" from my mainclass, since the dockwidgets need to change functionality in the main window. How can I connect this?

    I have managed to pass the "ui" to a function, but i cant figure out how to store it inside the new object.
    Any other approaches to solve this problem are also welcome.


    Qt Code:
    1. #ifndef SUBCLASS_H
    2. #define SUBCLASS_H
    3. #include <ui_mainclass.h>
    4.  
    5. namespace Ui {
    6. class MainClass;
    7. }
    8.  
    9. class SubClass
    10. {
    11. private:
    12. Ui::Ui_MainClass *ui;
    13. public:
    14. DataGeneration(Ui_MainClass *myUi);
    15. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. NewSubclass::NewSubclass( Ui_MainClass *myUi)
    2. {
    3. qDebug() << myUi->tableWidget->rowCount(); // works
    4.  
    5. ui = myUi // does not work
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: pass ui to subclass

    Quote Originally Posted by phil333 View Post
    For that I would like to have access to the original "ui" from my mainclass
    You actually don't want to do that, you want to treat the "ui" pointer as an internal to the window it belongs to.

    Quote Originally Posted by phil333 View Post
    since the dockwidgets need to change functionality in the main window. How can I connect this?
    They should ask the main window to manipulate its UI.

    Quote Originally Posted by phil333 View Post
    Qt Code:
    1. NewSubclass::NewSubclass( Ui_MainClass *myUi)
    2. {
    3. qDebug() << myUi->tableWidget->rowCount(); // works
    4.  
    5. ui = myUi // does not work
    6. }
    To copy to clipboard, switch view to plain text mode 
    No error so the guess is: because ui and myUI have different types?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: pass ui to subclass

    Thank you for your answer.

    I think I will try to instantiate the dockwidget inside the main window and then just pass a pointer. But that doesn't solve the issue that I have to access differentiates inside the main ui from the feedback i get inside the dockwidget. I like the idea of not letting subclasses touch the main ui directly, but i ll have see how easy this is in practice.


    The code above compiles, but it crashes when it gets to the "ui = myUi" line.

  4. #4
    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: pass ui to subclass

    solve the issue that I have to access differentiates inside the main ui from the feedback i get inside the dockwidget.
    A fundamental building block of all Qt programs is the use of signals and slots. Every Qt class that derives from QObject supports them. Many of the built-in Qt classes have signals and slots that they use to 1) communicate changes in their state or notify about the availability of data they hold (signals) or 2) react to changes signaled by some other Qt object (slots). The real power behind signals and slots is that you aren't limited to the one Qt provides as built-ins; if you derive your own custom class from some other QObject-based class, you can add your own signals and slots to do whatever you want them to do.

    As anda_skoa said, your dock widgets absolutely do not need pointers to your main window's "ui" instance. Your dock widgets don't even need to know that there is a main window. What they need to know is that if (for example) the user can do some action inside them that the outside world should know about, they should emit a signal that says "Hey, the user did X". Note that it doesn't matter how the user managed to do "X" - she could have clicked a button, turned a dial, slid a slider - that's a detail the dock widget knows about and no other class (including the main window) needs to know.

    So, now you have a signal that your customized dock widget emits when the user does "X". Your main window wants to know about that, so you write a slot for your main window that handles what should happen when the user does "X". Now, at the point where you construct your dock widget (maybe in the MainWindow constructor), you make a connect() call to connect the dock widget's "heyGotAnX()" signal to the main window's "gimmeeAnX()" slot.

    This is a one-to-one connection: if no other widget emits a heyGotAnX() signal -or- you don't connect the same slot to it, then the only time that slot will be executed is when that particular dock widget emits the signal connected to it. Your main window knows that the only place that signal could have come from is the dock widget it connected to so there is no confusion, and your main window can do whatever it need to "differentiate" the action that has just occurred.

    Nobody needs to know anything about the internals of anyone else's user interface in order for this to work. The main window needs to know that the dock widget can emit some custom signal, but the dock widget doesn't need to know anything about the main window. The dock widget doesn't even need to know if -anyone- is listening for its signals - it emits them when it is supposed to and that's it as far as it is concerned. It is up to the class whose slot is conected to that signal to do something with it.
    <=== 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. Replies: 2
    Last Post: 15th April 2013, 06:33
  2. Replies: 8
    Last Post: 23rd December 2012, 17:50
  3. How can i pass a value from qt to qml
    By vinayaka in forum Newbie
    Replies: 3
    Last Post: 13th October 2011, 13:07
  4. Replies: 8
    Last Post: 12th February 2010, 02:41
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34

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.