2016-02-21 37 views
1

我試過從QCustomPlot類派生出來,然後將這個小部件提升到那個特定的類,但是在構建它時會顯示以下錯誤。 Graphytti/myqcustomplot.cpp:2:錯誤:未定義參考`虛表爲MyQCustomPlot」 :-1:錯誤:collect2:錯誤:LD返回1退出狀態從QCustomPlot衍生而來的類構造函數

下面是myqcustomplot.h文件

的內容
#ifndef MYQCUSTOMPLOT_H 
#define MYQCUSTOMPLOT_H 
#include "qcustomplot.h" 
#include<QPoint> 
class MyQCustomPlot:public QCustomPlot{ 

    Q_OBJECT 
    QPoint cursor_pos; 
    public: 
    explicit MyQCustomPlot(QWidget *parent=0); 
    void mouseMoveEvent(QMouseEvent * event); 
    void paintEvent(QPaintEvent *event); 
    void paintCoordinate(); 


}; 

#endif // MYQCUSTOMPLOT_H 

下面是我的qcustomplot.cpp文件

#include "myqcustomplot.h" 
MyQCustomPlot::MyQCustomPlot(QWidget *parent): QCustomPlot(parent) 
{ 
    ; 
} 

void MyQCustomPlot::mouseMoveEvent(QMouseEvent * event) 
{ 
    cursor_pos = event->pos(); 
    replot(); 
    QCustomPlot::mouseMoveEvent(event); 
} 


void MyQCustomPlot::paintEvent(QPaintEvent *event) 
{ 
    QCustomPlot::paintEvent(event); 
    paintCoordinate(); 
} 

void MyQCustomPlot::paintCoordinate() 
{ 
    /*double price = getPrice(cursor_pos); 
    int y = yAxis->coordToPixel(price);*/ 
    int y=0; 
    QPainter painter(this); 

    painter.drawLine(QPoint(50, y), QPoint(width(), y)); 
    painter.drawLine(cursor_pos, QPoint(cursor_pos.x(), y)); 

    //painter.drawText(QPoint(0, y), QString::number(price)); 
    //painter.drawText(cursor_pos, timestamp); 
} 

經過一番搜索的內容,我意識到可能是我的構造函數沒有明確定義,或者有一些可能的連接問題。

我是Qt開發新手,希望能夠在這方面提供幫助。

問題就解決了 我不知道到底是什麼。我刪除這兩個文件並重新添加它們使用QtCreator的問題。這次建設之後沒有問題。可能是商務部的問題,因此提高了答案。

+2

您能否包含您的.pro文件? – Ilya

回答

0

每次創建Q_OBJECT類時,都應運行qmake以生成所需的moc cpp文件。

Build -> Run qmake