C++ Qt没有与调用mainWindow::connect()匹配的函数

C++ Qt没有与调用mainWindow::connect()匹配的函数,c++,qt,C++,Qt,我试图连接一个组合框值和一个标签,这样当组合框改变时,标签就会反映出这一点。我用谷歌搜索了我的心,试图找到一个答案,但是,到目前为止,一切都不起作用;我仍然得到错误:没有匹配的函数来调用mainWindow::connect(QComboBox*&,常量字符[38],QString*,常量字符[26]) 我尝试过QObject::connect,QWidget::connect以及其他处理Qt的方法,但都没有用 创建一个表示组合框值的标签不是我对程序的最终意图。相反,我希望让它使用一个简单的标签

我试图连接一个组合框值和一个标签,这样当组合框改变时,标签就会反映出这一点。我用谷歌搜索了我的心,试图找到一个答案,但是,到目前为止,一切都不起作用;我仍然得到错误:
没有匹配的函数来调用mainWindow::connect(QComboBox*&,常量字符[38],QString*,常量字符[26])

我尝试过
QObject::connect
QWidget::connect
以及其他处理Qt的方法,但都没有用

创建一个表示组合框值的标签不是我对程序的最终意图。相反,我希望让它使用一个简单的标签,然后将其更改为我希望它显示的内容(因此,
模板标签

mainwindow.h:

class MainWindow : public QMainWindow
{
public:
    MainWindow();

private slots:
    QString getClass(QComboBox *box);
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
    MainWindow();

private slots:
    void updateLabelText(const QString& className);

private:
    QComboBox* mathClassCombo;
    QLabel* tempLabel;
}
mainwindow.cpp:

MainWindow::MainWindow()
{
    QString qMathClassName;

    QComboBox* mathClassCombo = new QComboBox;
    QLabel* label = new QLabel(qMathClassName);

    // omitting layout code...

    connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString &)), 
            &qMathClassName, SLOT(getClass(mathClassCombo)));
}

QString MainWindow::getClass(QComboBox *box)
{
    return box->currentText();
}
MainWindow::MainWindow()
{
    mathClassCombo = new QComboBox;
    tempLabel = new QLabel;

    // omitting layout code...

    connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)), 
            this, SLOT(updateLabelText(const QString&)));
}

void MainWindow::updateLabelText(const QString& className)
{
    QString newLabelString = className + " is the best class ever!";
    tempLabel->setCurrentText(newLabelString);
}

任何帮助都将不胜感激

您正在将信号连接到具有不同签名的插槽。你必须把你的位置改成

getClass(const QString &)

匹配
currentIndexChanged
信号

您正在将信号连接到具有不同签名的插槽。你必须把你的位置改成

getClass(const QString &)

匹配
currentIndexChanged
信号

我想你需要阅读Qt的。再说一次,如果你已经这么做了。特别注意他们的例子

我认为您对C++中的Qt有以下误解:

  • QLabel引用一个QString,当该字符串更改时,它将更新其文本。不会的。当您为QLabel指定字符串时,它将显示该字符串的值。这是它唯一一次更新

  • 在堆栈上构造的对象不会在函数结束时被销毁。他们不会。在构造函数结束时,qMathClassName将被销毁,对它的任何引用都将变得无效。因此,即使可以,您也不想与它建立连接

  • 连接的第三个参数是指向放置插槽返回值的位置的指针。不是。第三个参数是指向要在其上调用插槽的QObject的指针。插槽的返回值不用于通过QObject::connect对其进行的任何调用

  • 您可以将值绑定到连接中的插槽。不幸的是没有。在插槽宏中,必须放置插槽的函数签名。您不能引用任何变量。参数部分只能有类名。这是
    SLOT(getClass(QComboBox*))
    ,而不是
    SLOT(getClass(mathClassCombo))

  • 确保组合框内容显示在标签中的最简单方法如下:

    QComboBox* mathClassCombo = new QComboBox;
    QLabel* tempLabel = new QLabel;
    connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)), 
            tempLabel, SLOT(setText(const QString&)));
    
    如果你想做一些更复杂的事情,我建议在你的窗口上开一个可以处理这些复杂问题的插槽。例如:

    mainwindow.h:

    class MainWindow : public QMainWindow
    {
    public:
        MainWindow();
    
    private slots:
        QString getClass(QComboBox *box);
    };
    
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
        MainWindow();
    
    private slots:
        void updateLabelText(const QString& className);
    
    private:
        QComboBox* mathClassCombo;
        QLabel* tempLabel;
    }
    
    mainwindow.cpp:

    MainWindow::MainWindow()
    {
        QString qMathClassName;
    
        QComboBox* mathClassCombo = new QComboBox;
        QLabel* label = new QLabel(qMathClassName);
    
        // omitting layout code...
    
        connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString &)), 
                &qMathClassName, SLOT(getClass(mathClassCombo)));
    }
    
    QString MainWindow::getClass(QComboBox *box)
    {
        return box->currentText();
    }
    
    MainWindow::MainWindow()
    {
        mathClassCombo = new QComboBox;
        tempLabel = new QLabel;
    
        // omitting layout code...
    
        connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)), 
                this, SLOT(updateLabelText(const QString&)));
    }
    
    void MainWindow::updateLabelText(const QString& className)
    {
        QString newLabelString = className + " is the best class ever!";
        tempLabel->setCurrentText(newLabelString);
    }
    

    我想你需要读Qt的。再说一次,如果你已经这么做了。特别注意他们的例子

    我认为您对C++中的Qt有以下误解:

  • QLabel引用一个QString,当该字符串更改时,它将更新其文本。不会的。当您为QLabel指定字符串时,它将显示该字符串的值。这是它唯一一次更新

  • 在堆栈上构造的对象不会在函数结束时被销毁。他们不会。在构造函数结束时,qMathClassName将被销毁,对它的任何引用都将变得无效。因此,即使可以,您也不想与它建立连接

  • 连接的第三个参数是指向放置插槽返回值的位置的指针。不是。第三个参数是指向要在其上调用插槽的QObject的指针。插槽的返回值不用于通过QObject::connect对其进行的任何调用

  • 您可以将值绑定到连接中的插槽。不幸的是没有。在插槽宏中,必须放置插槽的函数签名。您不能引用任何变量。参数部分只能有类名。这是
    SLOT(getClass(QComboBox*))
    ,而不是
    SLOT(getClass(mathClassCombo))

  • 确保组合框内容显示在标签中的最简单方法如下:

    QComboBox* mathClassCombo = new QComboBox;
    QLabel* tempLabel = new QLabel;
    connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)), 
            tempLabel, SLOT(setText(const QString&)));
    
    如果你想做一些更复杂的事情,我建议在你的窗口上开一个可以处理这些复杂问题的插槽。例如:

    mainwindow.h:

    class MainWindow : public QMainWindow
    {
    public:
        MainWindow();
    
    private slots:
        QString getClass(QComboBox *box);
    };
    
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
        MainWindow();
    
    private slots:
        void updateLabelText(const QString& className);
    
    private:
        QComboBox* mathClassCombo;
        QLabel* tempLabel;
    }
    
    mainwindow.cpp:

    MainWindow::MainWindow()
    {
        QString qMathClassName;
    
        QComboBox* mathClassCombo = new QComboBox;
        QLabel* label = new QLabel(qMathClassName);
    
        // omitting layout code...
    
        connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString &)), 
                &qMathClassName, SLOT(getClass(mathClassCombo)));
    }
    
    QString MainWindow::getClass(QComboBox *box)
    {
        return box->currentText();
    }
    
    MainWindow::MainWindow()
    {
        mathClassCombo = new QComboBox;
        tempLabel = new QLabel;
    
        // omitting layout code...
    
        connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)), 
                this, SLOT(updateLabelText(const QString&)));
    }
    
    void MainWindow::updateLabelText(const QString& className)
    {
        QString newLabelString = className + " is the best class ever!";
        tempLabel->setCurrentText(newLabelString);
    }
    

    您混淆了类、实例和值。这三样东西是不能互换的。QmathClassName是一个实例。它的类型是QString。它的值类似于“”。您混淆了类、实例和值。这三样东西是不能互换的。QmathClassName是一个实例。它的类型是QString。其值类似于“”。