Results 1 to 7 of 7

Thread: SIGNAL and SLOT problem

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: SIGNAL and SLOT problem

    I'm trying to keep track of which button(sender) is being clicked I tried using the QObject::sender() but that gives me weird numbers(ex: 0xb07f970) and not like an index
    so then I tired this for my header:
    Qt Code:
    1. class: QPushButton;
    2. class: QGridLayout;
    3. private slots:
    4. void timeButton_pressed(QPushButton *timeButton[24]);
    5.  
    6. private:
    7. QPushButton *timeButton[24];
    8. QGridLayout *layout;
    To copy to clipboard, switch view to plain text mode 

    and on my c++
    Qt Code:
    1. class::class(){
    2. layout = new QGridLayout;
    3.  
    4. for(int i = 0; i < 24; i++){
    5. timeButton[i] = new QPushButton;
    6. QString inum = QString("%1").arg(i);
    7. timeButton[i]->setText(inum);
    8. timeButtons->addButton(timeButton[i],i);
    9. textEdit[i] = new QTextEdit;
    10.  
    11. layout->addWidget(timeButton[i]);
    12. connect(timeButton[i],SIGNAL(clicked()),
    13. this,SLOT(timeButton_pressed(QPushButton*)));
    14. }
    15. setLayout(layout);
    16. }
    17.  
    18. void class::timeButton_pressed(QPushButton* timeButton[24]){
    19. cout << "SIGNAL BEING SENT" << endl;
    20. for(int i = 0;i<24;i++){
    21. QString inum = QString("%1").arg(i);
    22. if(inum == timeButton[i]->text()){
    23. cout << "button: " << i << " has been pressed!" << endl;
    24. }
    25. }
    26. //cout << "BUTTON " << QObject::sender() << " has been pressed" << endl;
    27. }
    To copy to clipboard, switch view to plain text mode 
    but the signal doesn't even seem to be sent to the timeButton_Pressed
    and if I remove the (QPushButton*) from connection and the functions it works but...if I press one button it thinks I pressed all the buttons because I'll get a cout of
    button: 0 has been pressed!
    button: 1 has been pressed!
    button: 2 has been pressed!
    button: 3 has been pressed!
    button: 4 has been pressed!
    button: 5 has been pressed!
    button: 6 has been pressed!
    button: 7 has been pressed!
    button: 8 has been pressed!
    button: 9 has been pressed!
    button: 10 has been pressed!
    button: 11 has been pressed!
    button: 12 has been pressed!
    button: 13 has been pressed!
    button: 14 has been pressed!
    button: 15 has been pressed!
    button: 16 has been pressed!
    button: 17 has been pressed!
    button: 18 has been pressed!
    button: 19 has been pressed!
    button: 20 has been pressed!
    button: 21 has been pressed!
    button: 22 has been pressed!
    button: 23 has been pressed!
    but if I pressed button 0 it should only of couted "button: 0 has been pressed!" and not all the others.

    I can think of one way to fix this...but I would rather not do it because of time.
    one fix would be to just connect each signal to a different slot and then make 24 slots and each slot do something different.

    can anyone think of a way for me to have them all on one slot but do different things based on the button being pressed?

    Thanks for any suggestions!


    Added after 8 minutes:


    sorry guys, was missing one tiny line in the slot =/
    if anyone else has this problem add in this line
    Qt Code:
    1. QPushButton button = qobject_cast<QPushButton *>(sender());
    To copy to clipboard, switch view to plain text mode 
    then just do an if statement
    Qt Code:
    1. if(button == buttonname){
    2. //blah
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by giblit; 26th March 2013 at 22:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SIGNAL and SLOT problem

    Take a look at the QSignalMapper class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SIGNAL and SLOT problem

    QObject::sender() gives You a pointer to the object so slot should looks like this :
    Qt Code:
    1. void class::timeButton_pressed(void)
    2. {
    3. cout << "SIGNAL BEING SENT" << endl;
    4. for(int i = 0;i<24;i++)
    5. {
    6. if(QObject::sender() == timeButton[i])
    7. {
    8. cout << "button: " << i << " has been pressed!" << endl;
    9. break;
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SIGNAL and SLOT problem

    but...if I press one button it thinks I pressed all the buttons
    No it doesn't. The loop is doing exactly what you told it to:

    For each integer between 0 and 24
    {
    Make a QString containing the integer
    Fetch the "timeButton" from the array at the given integer location
    Fetch the text from the button
    Compare the text and the QString, and if they are equal, print a phrase.
    }

    That loop doesn't care at all which button was pressed. All it is doing is comparing strings which were assigned in the constructor.

    You can either do as Lesiok suggested, and examine QObject::sender(), or you can implement a QSignalMapper as Wysota suggests. Either way will get you what you want.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SIGNAL and SLOT problem

    connect(timeButton[i],SIGNAL(clicked()), this,SLOT(timeButton_pressed(QPushButton*)));

    this will never work. you cannot connect a signal to a slot where the slot has more arguments
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: SIGNAL and SLOT problem

    Yeah you can amleto you can have as many signals as you want for a slot.
    I figured it out though yesterday by using a cast but lesiok's post does exact same thing took me a while to figure out I was missing the == button[i] after checking the sender
    thanks again

  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: SIGNAL and SLOT problem

    My recommendation for dealing with multiple, potentially dynamically created buttons, is QButtonGroup

    Cheers,
    _

Similar Threads

  1. signal & slot problem
    By ahmetturan in forum Newbie
    Replies: 1
    Last Post: 12th February 2012, 16:59
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. SIGNAL and SLOT Problem
    By RHMK in forum Qt Programming
    Replies: 5
    Last Post: 26th December 2010, 12:45
  4. Signal and Slot Problem
    By waynew in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2010, 10:49
  5. problem with signal/slot
    By ihoss in forum Newbie
    Replies: 2
    Last Post: 24th August 2007, 22:59

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.