从网上下载了一个小程序测试QComboBox的信号槽
#include <QApplication>
#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QWidget>
#include <QString>
void print2Screen(){
printf("changed\n");
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *win =new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
QComboBox *combo = new QComboBox();
QLabel *label = new QLabel("hello");
QPushButton *helloButton = new QPushButton("Show Label");
combo->addItem("A");
combo->addItem("B");
combo->addItem("C");
QObject::connect(combo, SIGNAL(activated(int)),label, SLOT(hide()));
QObject::connect(combo, SIGNAL(currentIndexChanged(int)),label, SLOT(print2Screen()));
QObject::connect(helloButton, SIGNAL(clicked()),label, SLOT(show()));
layout->addWidget(label);
layout->addWidget(combo);
layout->addWidget(helloButton);
win->setLayout(layout);
win->showMaximized();
return app.exec();
}
运行时报错:QObject::connect: No such slot QLabel::print2Screen()
哪位帮忙看看什么问题?
谢谢!
------解决方案--------------------
你在类中没用定义这个slot函数。