2017-03-20 19 views
0

この質問は重複しているように見えますが、研究をして同じ質問を見た後、マクロを使用する必要があることが判明しました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 slot QWidget::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 for Window_General

私の質問は、私がどのように使用できるか、ですこのコードセットのボタンイベント?

+1

ファイルの作成方法が表示されていません。リンクエラーはおそらく、ファイルにmocを実行していないために発生します。 –

+0

vtableエラーが表示されたときに、make distcleanで問題を解決できます。 Qtはいくつかの変更で更新されないサポートファイルを生成する傾向があります –

+0

Windows_Generalの先頭にQ_OBJECTがないため、最初のエラーは間違いなくQ_OBJECTを両方のクラスに配置する必要があります。最後にクリーンなプロジェクトとqmakeを実行することは良い提案です。 – Marco

答えて

3

mocを再生成するにはqmake utilityに電話する必要があるようです。 Q_OBJECTは、いくつかのQObjectのオーバーライドメンバ関数宣言をクラスに置き、qmakeはそれらの定義をclass.moc.cppに生成します。新しいQ_OBJECTを置くたびに、qmakeを呼び出す必要があります。

+0

回答をより明確にするためにqmakeを呼び出す方法をコードを追加してください。 –

+2

私はQtクリエイターメニューで 'Run qmake'を見つけました。それはトリックでした、ありがとう! –

2

SIGNALおよびSLOTマクロの代わりに、member-function-pointer-to-member-function構文を使用すると、Q_OBJECTマクロを完全に回避することができます。例:

connect(m_button, &QPushButton::released, this, &Window_General::MyhandleButton); 
関連する問題