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有以下误解:
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有以下误解:
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。其值类似于“”。