Results 1 to 9 of 9

Thread: twice execution of click on pushbutton?

  1. #1
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question twice execution of click on pushbutton?

    Hello,

    I'm very confused about the fact, that if I click (left mouse button) on a pushbutton, then the function connected via SLOT to the SIGNAL of pushbuttons is executed twice. Why? I want it to be executed only one time! How to avoid this behaviour?

    I have created my GUI Framework by means of Qt Designer 4.5.1 and then connect the SLOTs automatically by the command "QMetaObject::connectSlotsByName(this);"

    Here is the part of cpp code:

    Qt Code:
    1. #include <iostream>
    2. #include <time.h>
    3.  
    4. #include <QtUiTools>
    5. #include <QtGui>
    6.  
    7. #include "mainframe.h"
    8. #include "Vertex.h"
    9. #include "graphicscene.h"
    10.  
    11. using namespace std;
    12.  
    13. mainFrame::mainFrame(QWidget *parent)
    14. : QMainWindow(parent)
    15. {
    16. ui.setupUi(this);
    17.  
    18. ui_pushButton_START = qFindChild<QPushButton*>(this, "pushButton_START");
    19. ui_pushButton_Select = qFindChild<QPushButton*>(this, "pushButton_Select");
    20. ui_pushButton_Recombine = qFindChild<QPushButton*>(this, "pushButton_Recombine");
    21.  
    22. /*
    23. connect(ui_pushButton_START, SIGNAL(clicked()), this, SLOT(on_pushButton_Recombine_clicked()));
    24. connect(ui_pushButton_Select, SIGNAL(clicked()), this, SLOT(on_pushButton_Select_clicked()));
    25. connect(ui_pushButton_Recombine, SIGNAL(clicked()), this, SLOT(on_pushButton_Recombine_clicked()));
    26. */
    27.  
    28. QMetaObject::connectSlotsByName(this);
    To copy to clipboard, switch view to plain text mode 

    and here a part of header file:

    Qt Code:
    1. class mainFrame : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. mainFrame(QWidget *parent = 0);
    7. ~mainFrame();
    8.  
    9.  
    10.  
    11. // here the slots are declared, which are kind of sink for connected objects
    12. private slots:
    13. void on_pushButton_Quit_clicked();
    14. void updateParameters();
    15. void on_pushButton_START_clicked();
    16. void on_pushButton_Recombine_clicked();
    17. void on_pushButton_Select_clicked();
    To copy to clipboard, switch view to plain text mode 

    Help me please to avoid this failure function. Thank you.

    best regards,

    Vitali

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: twice execution of click on pushbutton?

    Please comment this line and see the results.
    Qt Code:
    1. QMetaObject::connectSlotsByName(this);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    1

    Default Re: twice execution of click on pushbutton?

    i am guessing it is because of:

    Qt Code:
    1. ui_pushButton_START = qFindChild<QPushButton*>(this, "pushButton_START");
    2. ui_pushButton_Select = qFindChild<QPushButton*>(this, "pushButton_Select");
    3. ui_pushButton_Recombine = qFindChild<QPushButton*>(this, "pushButton_Recombine");
    To copy to clipboard, switch view to plain text mode 

    i think it's causing connectSlotsByName to connect the signal/slot for each button twice because you have an aditional pointer ...
    i may be wrong though...

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: twice execution of click on pushbutton?

    notice: QMetaObject::connectSlotsByName(this); is also used in generated ui_classname.h file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: twice execution of click on pushbutton?

    You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why?

  6. #6
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: twice execution of click on pushbutton?

    Quote Originally Posted by rambo83 View Post
    You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why?
    Because you named them(slots) like that.
    If you have a QPushButton named btn. And you created a slot named on_btn_clicked().
    It will automatically get connected.
    QT ROCKS

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: twice execution of click on pushbutton?

    because of this "notice: QMetaObject::connectSlotsByName(this); is also used in generated ui_classname.h file."
    read this QMetaObject::connectSlotsByName.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: twice execution of click on pushbutton?

    Quote Originally Posted by rambo83 View Post
    You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why?
    setupUi() calls connectSlotsByName. You are then calling it yourself too, thus you get two function call per click

  9. #9
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: twice execution of click on pushbutton?

    Thank you very much for the explanations. They were very helpful.

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 14:12
  2. pushButton click event
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 11:19
  3. Working out table's current row from pushbutton click
    By shooogun in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2008, 23:40
  4. connecting image to a pushbutton
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 4th December 2007, 09:23
  5. QGraphicsScene Click / Double Click
    By philentropist in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2007, 04:32

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.