我有这样的代码:
class MyListView : public QListView
{
public:
MyListView();
~MyListView();
public slots:
void insertData();
void deleteData();
void showData();
private:
QStringListModel *model;
QListView *listView;
};
构造函数就像:
MyListView :: MyListView()
{
QStringList data;
data << "Letter A" << "Letter B" << "Letter C";
model = new QStringListModel;
model->setStringList(data);
listView = new QListView;
listView->setModel(model);
/* the three buttons */
QPushButton *insertBtn = new QPushButton(QObject::tr("insert"),this);
QObject::connect(insertBtn,SIGNAL(clicked()),this,SLOT(insertData()));
QPushButton *deleteBtn = new QPushButton(QObject::tr("delete"),this);
QObject::connect(deleteBtn,SIGNAL(clicked()),this,SLOT(deleteData()));
QPushButton *showBtn = new QPushButton(QObject::tr("show"),this);
QObject::connect(showBtn,SIGNAL(clicked()),this,SLOT(showData()));
/* layout */
QHBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addWidget(insertBtn);
btnLayout->addWidget(deleteBtn);
btnLayout->addWidget(showBtn);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(listView);
mainLayout->addLayout(btnLayout);
setLayout(mainLayout);
}
所以我想将按钮连接到插槽函数,但是当我编译它时,我收到错误消息:
QObject::connect: No such slot QListView::insertData()
我认为问题来自于connect函数,其中“this”不是正确的指针,有什么帮助吗?提前谢谢。
答案 0 :(得分:2)
您需要在MyListView
中添加Q_OBJECT宏来自Qt API文档:
请注意,Q_OBJECT宏对于任何对象都是必需的 实现信号,槽或属性。您还需要运行Meta 源文件上的对象编译器。我们强烈建议使用 无论是否与QObject的所有子类中的此宏 他们实际上使用信号,插槽和属性,因为没有做到 所以可能导致某些功能表现出奇怪的行为。
所以,它应该是:
class MyListView : public QListView
{
Q_OBJECT
public:
...
}