Results 1 to 14 of 14

Thread: Difficult connect of signals and slots

  1. #1
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Difficult connect of signals and slots

    Hi,

    I am working on a project and have some serious problems concerning the connections and slots through my project. Let me explain this to you:
    I have a MainForm that contains some widgets that are promoted to other forms (to split my gui into several classes).
    In addition I have a thread that shall update the gui. The thread is started in the constructor of the MainForm. Therefor I think I have to do the connect in the constructor, too. But how can I connect the thread signal through the promoted widgets to the wanted slot?

    Thanks and best regards
    Ozzy

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Difficult connect of signals and slots

    In addition I have a thread that shall update the gui
    Why do you need that ? Could be easier to help if you show us some code.

  3. #3
    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: Difficult connect of signals and slots

    Additional to what stampede already said, you can access your widgets through the ui pointer, no matter if they are promoted or not

    Qt Code:
    1. connect(someSource, SIGNAL(someSignal()), ui->somePromotedWidget, SLOT(someSlot()));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  4. #4
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Hi,

    thats right. But from default, the ui is private. The question is whether it is useful to change all needed ui-objects to public...

  5. #5
    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: Difficult connect of signals and slots

    Even if it is private, you are doing that in the very samel class' constructor. It can access its own private members

    No need to make anything public.

    Cheers,
    _

  6. #6
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Hi,

    sorry, but I don't get it. Suppose I have a very nested design where the mainFrame has a widget that is promoted (by the designer) to another form (in this case form with a QFrame template). This form contains several widget including a widget that is also a form with a QFrame template. In this form I want to change the text of a label. So, how can I achieve this? My problem is how to connect the signal of the thread (easy) with the slot in the "last" frame widget.

  7. #7
    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: Difficult connect of signals and slots

    I am not quite sure I understand the problem.

    Lets say you have a custom widget MyWidget and in your MainForm you have that as a child (promoted in designer).
    So you can access the MyWidget instance something like this
    Qt Code:
    1. ui->myWidget
    To copy to clipboard, switch view to plain text mode 

    MyWidget has, as an implementation details, a label and you want to set its text. Since the label is a internal detal of MyWidget and this encapsulated in MyWidget, you need a way to access it through the MyWidget API (see basic OOP principles).

    Qt Code:
    1. void MyWidget::setText(const QString &text)
    2. {
    3. // set text on label
    4. }
    To copy to clipboard, switch view to plain text mode 

    You can make that a slot and connect to it from MainForm
    Qt Code:
    1. connect(someSource, SIGNAL(someSignal(QString)), ui->myWidget, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #8
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Hi,

    thank you very much for your reply! Now I got it working. The main problem was that the ui_widget1.h (created by the designer) was included in the .cpp file. So I could not find the widget in the mainFrame. Now I included it in the header file, defined the MainFrame as friend class ( to get access to the private ui) and everything works fine :-)

  9. #9
    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: Difficult connect of signals and slots

    That is definitely not how it is supposed to be used, but if it works for you who are we to argue

    Cheers,
    _

  10. #10
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Hi, but what do you do if you want to access a slot that is in a widget that is promoted to the first widget? So this would be ui->myWidget->ui->myWidget2... Or how do you access this slot?

  11. #11
    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: Difficult connect of signals and slots

    I answered that already, did I not? Comment #7

    The MainForm is dealing with MyWidget, it doesn't and should know about anything that is internal to MyWidget.

    Like, if you want to set the value of a QSpinBox, do you need to know that there is a QLineEdit inside QSpinBox, do you need to access it? Why not?

    Cheers,
    _

  12. #12
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Ok, I think I get your point. So I call a function in the first widget that calls again a function in the next widget, right? So everything keeps private and is only accessible through the 'direct' interfaces.

  13. #13
    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: Difficult connect of signals and slots

    Yes, exactly.

    Each widget is an encapsulated unit. If that unit is in fact composed of other units then this is not "visible" from outside the widget.

    Cheers,
    _

  14. #14
    Join Date
    Nov 2007
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Difficult connect of signals and slots

    Ok, thank you very much!

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Replies: 1
    Last Post: 28th January 2012, 12:35
  3. How to use connect() in IDE's(slots)
    By wenn32 in forum Newbie
    Replies: 7
    Last Post: 6th July 2010, 16:21
  4. Replies: 16
    Last Post: 16th February 2010, 13:17
  5. Replies: 12
    Last Post: 23rd June 2008, 08:05

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.