Qt5 new signals-slots syntax does not work [SOLVED]



  • Why this works
    @connect(cmbList1, SIGNAL(currentIndexChanged(const QString&)),this, SLOT(func1(const QString&)));@

    but this does not?
    @connect(cmbList1, &QComboBox::currentIndexChanged, this, &MyClass::func1);@

    log
    error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 2 from 'overloaded-function' to 'const char *'
    Context does not allow for disambiguation of overloaded function



  • Are you sure you are using Qt5 (and not Qt4)?



  • yes. at other place this method works



  • Note that QComboBox::currentIndexChanged has two signals. Compiler don't know what signal do you want. Need to explicitly tell the compiler correct functions address.

    edit: for example for signal with QString argument, you need something like this:
    @
    void (QComboBox:: *indexChangedSignal)(QString) = &QComboBox::currentIndexChanged;
    connect(comboBox, indexChangedSignal, this, &MyClass::func1)
    @



  • when i tried to compile code above i have this
    error: C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__thiscall QComboBox::* )(QString)'
    None of the functions with this name in scope match the target type



  • @connect(cmbProfiles, SIGNAL(currentIndexChanged(const QString&)),
    this, SLOT(loadProfilesDetails(const QString&)));@
    And now I see that in qt5 this code does not work too, it compiles but does not work. Sorry for my inattention.



  • So now I must ask how to connect func to signal QComboBox::currentIndexChanged(const QString & text) in Qt5 ?



  • As cincirin already mentioned you will have to explicitly cast overloaded signals when using the function pointer syntax.

    connect(cmbProfiles,
            static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), 
            this,
            &MyClass::loadProfilesDetails);
    

    The equivalent of the string-based syntax is

    connect(cmbProfiles, 
            SIGNAL(currentIndexChanged(const QString &)),
            this,
            SLOT(loadProfilesDetails(const QString &)));
    

    or

    connect(cmbProfiles, 
            SIGNAL(currentIndexChanged(QString)),
            this,
            SLOT(loadProfilesDetails(QString)));
    


  • It works, thanks to all.



  • Wow... it works, but that is really ugly!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.