当前位置:网站首页>QT error: no matching member function for call to ‘connect‘

QT error: no matching member function for call to ‘connect‘

2022-04-23 18:22:00 Things will turn when they reach the extreme 1024

error: no matching member function for call to ‘connect’

Include connect This error can also occur in functions other than , An error encountered at present , example :

  // Error function 
connect(ui->spinBox_HEX,&QSpinBox::valueChanged,[=](){
    });
//QSpinBox::valueChanged Function declaration 
Q_SIGNALS:
    void valueChanged(int);
    void valueChanged(const QString &);

There is no problem with such a signal connection format , however QSpinBox::valueChanged Function has overload , The compiler doesn't know which overload to use , It's a mistake . We use void valueChanged(const QString &); Format , So in connect Casts are used in

// After cast 
connect(ui->spinBox_HEX,static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),[=](){


 });//static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged)

Specific writing method
connect( Control name ,static_cast< return type ( class :: *)( Parameters )>(& class :: Function name ),={

});

Another example
void currentIndexChanged(int index);
void currentIndexChanged(const QString &);
This function has two parameters
Writing method of ordinary function :

connect(ui->comboBox,&QComboBox::currentIndexChanged,[=]{
    //RqDebug()<<ui->comboBox->currentText();
});

Correct usage to
void currentIndexChanged(int index); For example

First Return value void Parameters int index class QComboBox

In the original writing
&QComboBox::currentIndexChanged
remain unchanged , Add... To the front

static_cast< return type  ( class :: *)( Parameters )>(&QComboBox::currentIndexChanged    // Format 
static_cast<void ( class :: *)( Parameters )>(&QComboBox::currentIndexChanged           // Add return value 
static_cast<void (QComboBox:: *)( Parameters )>(&QComboBox::currentIndexChanged   // Additive 
static_cast<void (QComboBox:: *)(int index)>(&QComboBox::currentIndexChanged    // The last plus parameter 

Finally, the actual code

connect(ui->comboBox,static_cast<void (QComboBox::*)(int index)>(&QComboBox::currentIndexChanged),[=]{
    
    qDebug()<<ui->comboBox->currentText();
});

Corresponding
void currentIndexChanged(const QString &);

    connect(ui->comboBox,static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),[=]{
    
    qDebug()<<ui->comboBox->currentText();
});

版权声明
本文为[Things will turn when they reach the extreme 1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610057331.html