Results 1 to 10 of 10

Thread: qt signal/slot auto-connect issue

  1. #1
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default qt signal/slot auto-connect issue

    Hi.

    I'm trying the auto-connect feature in my code but i don't arrive to get it working.

    this is my code:

    File .h
    Qt Code:
    1. class TBarAffichage : public QToolBar{
    2. Q_OBJECT
    3. private:
    4. TColorComboBox* CBoxTraitCouleurs;
    5. ....
    6. private Q_SLOTS:
    7. void on_CBoxTraitCouleurs_activated(int);
    8. ....
    9. };
    To copy to clipboard, switch view to plain text mode 
    File .cpp:
    Qt Code:
    1. TBarAffichage::TBarAffichage(QWidget *parent):
    2. QToolBar(parent),
    3. CBoxTraitCouleurs (new TColorComboBox(this)) {}
    4. ...
    5. void TBarAffichage::on_CBoxTraitCouleurs_activated(int i){
    6. qDebug() << "activated";
    7. }
    8. ...
    To copy to clipboard, switch view to plain text mode 
    The TColorComboBox.h:
    Qt Code:
    1. class TColorComboBox : public QComboBox{
    2. Q_OBJECT
    3. public:
    4. TColorComboBox(QWidget *widget = 0):QComboBox(widget){}
    5. };
    To copy to clipboard, switch view to plain text mode 

    When i select the color combobox (CBoxTraitCouleurs) and change it (from red to white) the qDebug isn't launched.
    I've tried with others objects and signals but nothing works.

    What i'm doing wrong?
    I've to put something into the .pro?
    I've to add something to the constructor?

    Thanks

  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: qt signal/slot auto-connect issue

    Can you change Q_SLOTS to slots.

  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: qt signal/slot auto-connect issue

    You have to call QMetaObject::connectSlotsByName() explicitly.

  4. #4
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qt signal/slot auto-connect issue

    Quote Originally Posted by yogeshgokul View Post
    Can you change Q_SLOTS to slots.
    I can't because i use boost signals and there are conflicts.

  5. #5
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qt signal/slot auto-connect issue

    Quote Originally Posted by Lykurg View Post
    You have to call QMetaObject::connectSlotsByName() explicitly.
    I have write
    Qt Code:
    1. QMetaObject::connectSlotsByName(this);
    To copy to clipboard, switch view to plain text mode 
    in the constructor and when i execute it gives that:

    Qt Code:
    1. QMetaObject::connectSlotsByName: No matching signal for on_CBoxTraitCouleurs_activated(int)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt signal/slot auto-connect issue

    QMetaObject::connectSlotsByName() works on OBJECT names, it doesn't know anything about variable names. See QObject::setObjectName(). Anyway, perhaps it would be more straight forward in this case to just establish the connection directly. Auto connections work nicely with UIC produced code, which sets the object names and calls connectSlotsByName() automatically for you....
    J-P Nurmi

  7. #7
    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: qt signal/slot auto-connect issue

    Try adding this line in .pro file.
    CONFIG += no_keywords

    It tells Qt not to define the moc keywords signals, slots, and emit, because these names will be used by a 3rd party library, e.g. Boost. Then to continue using Qt signals and slots with the no_keywords flag, simply replace all uses of the Qt moc keywords in your sources with the corresponding Qt macros Q_SIGNALS (or Q_SIGNAL), Q_SLOTS (or Q_SLOT), and Q_EMIT.

    Enjoy

  8. #8
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qt signal/slot auto-connect issue

    Quote Originally Posted by yogeshgokul View Post
    Try adding this line in .pro file.
    CONFIG += no_keywords

    It tells Qt not to define the moc keywords signals, slots, and emit, because these names will be used by a 3rd party library, e.g. Boost. Then to continue using Qt signals and slots with the no_keywords flag, simply replace all uses of the Qt moc keywords in your sources with the corresponding Qt macros Q_SIGNALS (or Q_SIGNAL), Q_SLOTS (or Q_SLOT), and Q_EMIT.

    Enjoy
    That is what i do It's because of that i write Q_SLOTS.
    Thank you for your reponse

  9. #9
    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: qt signal/slot auto-connect issue

    So, is it running or what ?

  10. #10
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qt signal/slot auto-connect issue

    Quote Originally Posted by jpn View Post
    QMetaObject::connectSlotsByName() works on OBJECT names, it doesn't know anything about variable names. See QObject::setObjectName(). Anyway, perhaps it would be more straight forward in this case to just establish the connection directly. Auto connections work nicely with UIC produced code, which sets the object names and calls connectSlotsByName() automatically for you....
    That's it.

    if i add
    Qt Code:
    1. CBoxTraitCouleurs->setObjectName("CBoxTraitCouleurs");
    To copy to clipboard, switch view to plain text mode 
    now it works.

    But maybe you are right about making the connect directly.

    Thank you very much

Similar Threads

  1. Application only 'animates' once
    By arkain in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2009, 08:09
  2. 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.