Results 1 to 14 of 14

Thread: Difficult connect of signals and slots

Hybrid View

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

  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: 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,
    _

  3. #3
    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 :-)

  4. #4
    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,
    _

  5. #5
    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?

  6. #6
    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,
    _

  7. #7
    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.

  8. #8
    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,
    _

  9. #9
    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.