Results 1 to 4 of 4

Thread: pass ui to subclass

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 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
  •  
Qt is a trademark of The Qt Company.