Results 1 to 11 of 11

Thread: How to connect a slot(QWidget*) with signal(clicked())

  1. #1
    Join Date
    Jul 2011
    Posts
    26
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Question How to connect a slot(QWidget*) with signal(clicked())

    Hey mates.

    I want to connect a button's clicked() signal with a custom slot that gets a QWidget as parameter. Afaik it is not possible to connect non-parameter signals with parameter slots.
    How can I solve that?


    Qt Code:
    1. connect(startButton,SIGNAL(clicked()),this,SLOT(calculationAlgoStart(subWindowTabWidget)));
    To copy to clipboard, switch view to plain text mode 


    any suggestions ?

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    Reimplement startButton and make your signal. Maybe this help:

    Qt Code:
    1. class PushButton : public QPushButton
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PushButton( QWidget *parent = 0 );
    7. virtual ~PushButton() {}
    8.  
    9. protected:
    10. virtual void click();
    11.  
    12. signals:
    13. void onClick( QWidget * );
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. PushButton::PushButton( QWidget *parent ):
    2. QPushButton( parent )
    3. {
    4. }
    5.  
    6. void PushButton::click()
    7. {
    8. emit onClick( this );
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Jonny174; 5th August 2011 at 12:45.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    ...or make an custom slot, which calls calculationAlgoStart(subWindowTabWidget).

  4. #4
    Join Date
    Jul 2011
    Posts
    26
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    Quote Originally Posted by Lykurg View Post
    ...or make an custom slot, which calls calculationAlgoStart(subWindowTabWidget).
    no sense. I have to give the custom slot the parameter subWindowTabWidget....

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    Quote Originally Posted by revellix View Post
    no sense.
    A lot of sense, if one try to understand...
    Qt Code:
    1. connect(startButton,SIGNAL(clicked()),this,SLOT(mySlot()));
    2. //...
    3. void XXX:mySlot()
    4. {
    5. calculationAlgoStart(subWindowTabWidget);
    6. }
    To copy to clipboard, switch view to plain text mode 
    And if you have more buttons you could use QSignalMapper. But this statement probably also has no sense.

  6. #6
    Join Date
    Jul 2011
    Posts
    26
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    subWindowTabWidget is a parameter that I give to the constructor... finally I have to tell the slot(), what subWindowTabWidget is. So I have again: mySlot(QTabWidget *).

    Yaaa... QSignalMapper... I tried to use the class, but I dont get it. Too newbish for that. If you want to explain it, feel free to do so.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    Quote Originally Posted by revellix View Post
    subWindowTabWidget is a parameter that I give to the constructor... finally I have to tell the slot(), what subWindowTabWidget is. So I have again: mySlot(QTabWidget *).
    Umpf, ok, lets take small steps: Where is your connect statement? Does it look like
    Qt Code:
    1. MyClass::MyClass(/*...*/, QObject* subWindowTabWidget)
    2. {
    3. // Ui setup (also startButton)
    4. connect(startButton, SIGNAL(clicked()),
    5. this, SLOT(calculationAlgoStart(subWindowTabWidget)));
    6. }
    To copy to clipboard, switch view to plain text mode 
    ?

  8. #8
    Join Date
    Jul 2011
    Posts
    26
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    Constructor:

    Qt Code:
    1. UserInput::UserInput(QWidget *parent, int form, QTabWidget *subWindowTabWidget) : QWidget(parent)
    2. {
    3. this->setAttribute(Qt::WA_DeleteOnClose);
    4.  
    5. this->setLayout(userInputLayout);
    6. this->constructUserInput(form, subWindowTabWidget); ........
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Constructormethod:

    Qt Code:
    1. void UserInput::constructUserInput(int constructForm, QTabWidget *)
    2. {
    3. ....
    4. connect(startButton,SIGNAL(clicked()),this, SLOT(calculationAlgoStart(QTabWidget *)));
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 


    CalculateAlgorithm...:

    Qt Code:
    1. void UserInput::calculationAlgoStart(QTabWidget *)
    2. {
    3. ....
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    In that case, why do you need to pass the pointer at all? Make a private member variable and use this in your slot.
    Qt Code:
    1. class UserInput
    2. {
    3. private:
    4. QTabWidget *m_subWindowTabWidget;
    5. };
    6.  
    7. UserInput::UserInput(QWidget *parent, int form, QTabWidget *subWindowTabWidget) : QWidget(parent), m_subWindowTabWidget(subWindowTabWidget)
    8. {
    9. this->setAttribute(Qt::WA_DeleteOnClose);
    10. this->setLayout(userInputLayout);
    11. connect(startButton,SIGNAL(clicked()),this, SLOT(calculationAlgoStart()));
    12. }
    13.  
    14. void UserInput::calculationAlgoStart(QTabWidget *)
    15. {
    16. // simple use m_subWindowTabWidget here
    17. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Lykurg for this useful post:

    revellix (5th August 2011)

  11. #10
    Join Date
    Jul 2011
    Posts
    26
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    see... this is why I am too newbish. THX mate.

    how is this parameter substitution called ? I should read something about that.

  12. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to connect a slot(QWidget*) with signal(clicked())

    It is simple use of private members. Nothing special. If you mean the initialization of members alongside the c-tor, see (constructor) initialization lists.

  13. The following user says thank you to Lykurg for this useful post:

    revellix (5th August 2011)

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 14:56
  4. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  5. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55

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.