この質問は重複しているように見えますが、研究をして同じ質問を見た後、マクロを使用する必要があることが判明しましたSLOT
を使用していますが、コンパイルエラー。ここに私のコードスニペットがあります。このコードでスロットにQtマクロQ_OBJECTを使用する
main.cppに
#include <QApplication>
#include "window_general.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window_General window_general;
window_general.show();
return app.exec();
}
windows_general.h
#ifndef WINDOW_GENERAL_H
#define WINDOW_GENERAL_H
#include <QWidget>
#include <QApplication>
class QPushButton;
class Window_General : public QWidget
{
public:
explicit Window_General(QWidget *parent = 0);
private slots:
void MyhandleButton();
private:
QPushButton *m_button;
};
#endif // WINDOW_GENERAL_H
windows_general.cpp
#include "window_general.h"
#include <QPushButton>
Window_General::Window_General(QWidget *parent) :
QWidget(parent)
{
// Set size of the window
setFixedSize(800, 500);
// Create and position the button
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
connect(m_button, SIGNAL (released()), this, SLOT (MyhandleButton()));
}
void Window_General::MyhandleButton()
{
m_button->setText("Example");
m_button->resize(100,100);
}
私はここでQ_OBJECT
マクロを置く場合は、
QObject::connect
: No such slotQWidget::MyhandleButton()
in ../prj/window_general.cpp:14
class Window_General : public QWidget
{
Q_OBJECT
public:
explicit Window_General(QWidget *parent = 0);
private slots:
void MyhandleButton();
private:
QPushButton *m_button;
};
私はこのエラーがあります:私は、ランタイムエラーを持っている
D:\work\my_qt\prj\window_general.h:8: error: undefined reference to
vtable
forWindow_General
私の質問は、私がどのように使用できるか、ですこのコードセットのボタンイベント?
ファイルの作成方法が表示されていません。リンクエラーはおそらく、ファイルにmocを実行していないために発生します。 –
vtableエラーが表示されたときに、make distcleanで問題を解決できます。 Qtはいくつかの変更で更新されないサポートファイルを生成する傾向があります –
Windows_Generalの先頭にQ_OBJECTがないため、最初のエラーは間違いなくQ_OBJECTを両方のクラスに配置する必要があります。最後にクリーンなプロジェクトとqmakeを実行することは良い提案です。 – Marco