Results 1 to 8 of 8

Thread: Slot gets called twice

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Slot gets called twice

    Hi, I'm a 10+ year C++ programmer but n3wb to Qt...

    I've successfully installed the Qt 4.5.2 SDK (MinGW g++) but, since I primarily use VC++, also installed the VS2008 plugin and built the Qt 4.5.2 sources for VC++ in a separate directory. After fixing some Environmental Values/Paths issues between the 2 Qt now works fine under both compilers/IDEs.

    I've made some simple test programs to try out Qt, but noticed that my custom slots always gets called twice for a single signal - and it happens in both IDEs

    In QtDesigner I add a signal/slot to a QPushButton where the Sender is the QPushButton (signal is clicked()) and the Receiver a custom slot (called on_pbAdd_clicked()) on my form class derived form QDialog.

    Since QtDesigner does not automatically generate the slots anymore, I've declared the slots like this:

    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. Dialog(QWidget *parent = 0, Qt::WFlags flags = 0);
    6. ~Dialog();
    7. private:
    8. Ui::DialogClass ui;
    9. public slots:
    10. virtual void on_pbAdd_clicked(void);
    11. virtual void on_pbRemove_clicked(void);
    12. };
    To copy to clipboard, switch view to plain text mode 

    The slot is implemented like this:

    Qt Code:
    1. void Dialog::on_pbAdd_clicked(void)
    2. {
    3. if(ui.leEdit->text().length() > 0) {
    4. ...
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I've tried to debug the issue: When the button is clicked one of the void QMetaObject::activate(...) functions in qobject.cpp gets called, specifically:

    Qt Code:
    1. void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal_index, void **argv)
    2. {
    3. ....
    4. int count = connectionLists->at(signal).count();
    5. for (int i = 0; i < count; ++i) {
    6. const QObjectPrivate::Connection *c = &connectionLists->at(signal)[i];
    7. ...
    8. receiver->qt_metacall(QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv);
    9. ...
    10. }
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 

    The count variable is set to 2 from the call to connectionLists->at(signal).count();. This means the next for loopis executed twice - which causes the slot to be called twice in the receiver->qt_metacall() function call. This resolves to a call to Dialog::qt_metacall(...) that was generated by the moc, which looks like this:

    Qt Code:
    1. int Dialog::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    2. {
    3. _id = QDialog::qt_metacall(_c, _id, _a);
    4. if (_id < 0)
    5. return _id;
    6. if (_c == QMetaObject::InvokeMetaMethod) {
    7. switch (_id) {
    8. case 0: on_pbAdd_clicked(); break;
    9. case 1: on_pbRemove_clicked(); break;
    10. default: ;
    11. }
    12. _id -= 2;
    13. }
    14. return _id;
    15. }
    To copy to clipboard, switch view to plain text mode 

    The case 0 in the switch statement then calls my on_pbAdd_clicked() slot - which seems perfect. So everything seems to work fine, with the exception of that connectionLists->at(signal).count(); value that is returned, which result in the slot being called twice!

    I am not yet familiar with the Qt code-base, so can anybody maybe help and maybe shed some light on this? How does one overcome this?

    Thanx

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slot gets called twice

    If you're using the GUI designer you don't need to manually declare slots, you can get designer to create them for you from the object's context menu. (goto->slot)

    Maybe delete your slots and re-create them with designer and see if you still have a problem.

  3. #3
    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: Slot gets called twice

    your slot already connected in ui-file using QMetaObject::connectSlotsByName.
    so, you should rename your slots or remove connections in designer.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot gets called twice

    thanks for your very quick response. But I am not sure i understand how to do this....

    I don't think I have this context menu (goto->slot) in my 4.5.2 Designer (not in the VS Plugin and also not in 4.5.2 QtCreator either).

    In my Designer if I view the Signals end slots editor I do not see any context menus [right-clcicking, from ny menu, etc].

    In fact, if I look at http://www.qtcentre.org/forum/faq.ph..._signals_inqt4:
    How can I add a custom slot in Qt4 Designer? In short - you can't. Beginning with Qt4.0 Qt Designer seized to be an all-purpose development environment and became strictly a GUI Designer...etc
    Can you be more specific...

    Thankx

  5. #5
    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: Slot gets called twice

    you wrote
    In QtDesigner I add a signal/slot to a QPushButton where the Sender is the QPushButton (signal is clicked()) and the Receiver a custom slot (called on_pbAdd_clicked()) on my form class derived form QDialog.
    that means, that "on_pbAdd_clicked" default slot according to this, in ui-file you can find such call QMetaObject::connectSlotsByName (this will create first connection), than you have declared slot on_pbAdd_clicked and probably connected it in ctor (I'm not sure) in that case you created the second connection.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot gets called twice

    @spirit - thanks for your very prompt reply.

    It seems to have done the trick!

    But - just to make sure I understand: Does this imply that if you follow the the slot function naming convention of on_<objectname>_<signalname>(), then it is not necessary to define signal/slot combinations in the signal/slot editor of Designer since Qt can figure out which custom slot to call (which - as you imply - is done in the QMetaObject::connectSlotsByName() function)?

    I've noticed that removing the connections in the signal/slot editor of Designer also removes the connections being generated by the moc previously:

    Qt Code:
    1. void setupUi(QDialog *DialogClass)
    2. {
    3. ...
    4. QObject::connect(pbAdd, SIGNAL(clicked()), DialogClass, SLOT(on_pbAdd_clicked()));
    5. QObject::connect(pbRemove, SIGNAL(clicked()), DialogClass, SLOT(on_pbRemove_clicked()));
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is this on_<objectname>_<signalname>() convention a kind of
    short-hand
    for custom
    on the fly
    slot generation as an alternative to the Designer route?

    Thanx again [can see I still have lots to learn about Qt!]

  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: Slot gets called twice

    all what you need it's to create a slot, but don't connect it, because it's already connected in ui.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot gets called twice

    Got it!

    Thanks a stack @spirit!

Similar Threads

  1. slot won´t be called (d3lphin debugging)
    By sirwitti in forum Qt Programming
    Replies: 7
    Last Post: 29th June 2008, 23:58
  2. Replies: 2
    Last Post: 23rd May 2008, 13:22
  3. SLOT not being called
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2007, 11:30
  4. connect() returns true but slot not called
    By OriginalCopy in forum Qt Programming
    Replies: 6
    Last Post: 4th November 2007, 18:54
  5. Replies: 8
    Last Post: 1st May 2007, 22:35

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.