【问题标题】:Qt connect function [duplicate]Qt连接功能[重复]
【发布时间】:2011-10-16 18:07:18
【问题描述】:

我正在尝试用 C++ 编写 Qt GUI。这是代码:

样本.h:

#ifndef SAMPLE_H
#define SAMPLE_H

#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <qobject.h>

class Sample : public QObject
{
    Q_OBJECT
public:
    Sample();
public slots:
    void buttonPressed();
private:
    QWidget *widget;
    QGridLayout *layout;
    QLineEdit *le;
    QPushButton *button;
};

#endif // SAMPLE_H

sample.cpp:

#include "sample.h"

Sample::Sample()
{
    widget = new QWidget();
    widget->setWindowTitle("Sample");
    layout = new QGridLayout();

    le = new QLineEdit();

    button = new QPushButton();
    button->setText("Button");

    connect(button, SIGNAL(clicked()), this, SLOT(buttonPressed()));
    layout->addWidget(le, 0, 0);
    layout->addWidget(button, 1 , 0);

    widget->resize(300, 300);
    widget->setLayout(layout);
    widget->show();
}

void Sample::buttonPressed(){
    le->setText("pressed");
}

我在构建时收到此错误:

error: undefined reference to `vtable for Sample'

我正在使用来自官方 Qt 网页的 QtCreator。

有人知道该怎么做才能让它工作吗?非常感谢您的回复:)

【问题讨论】:

    标签: c++ qt user-interface reference vtable


    【解决方案1】:

    如果在编写和编译类之后添加 Q_OBJECT 宏,通常会发生这种错误。重新运行 qmake 通常会修复它。

    【讨论】:

    • 问题解决了 :) 我清理了项目重新运行 qmake 并且它工作了:) 非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2021-09-03
    • 2021-03-05
    相关资源
    最近更新 更多