Qt c ++拆分类

时间:2013-09-14 00:38:32

标签: c++ qt

我正在使用Qt的CodeEditor示例,来自此网址。 enter link description here

他们将LineNumberArea和CodeEditor类放在同一个文件中。我试图将它们分成两个单独的头/源文件。但我一直得到一个“未定义的引用”错误。这是我的代码:

codeeditor.h

#include <QPlainTextEdit>
#include <QObject>
#include "../headers/editor/linenumbers.h"

QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumbers;
QT_END_NAMESPACE


class CodeEditor : public QPlainTextEdit {
    Q_OBJECT

public:
    explicit CodeEditor(QWidget *parent = 0);
    virtual ~CodeEditor();

protected:
    void resizeEvent(QResizeEvent *event);

private:
    LineNumbers *lineNumberArea;

};

codeeditor.cpp

#include <QtGui>

#include "../headers/editor/linenumbers.h"
#include "../headers/editor/codeeditor.h"


CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit( parent ) {
    lineNumberArea = new LineNumbers(this);

    connect(this, SIGNAL(blockCountChanged(int))  , lineNumberArea, SLOT(updateLineNumberAreaWidth(int)));
    connect(this, SIGNAL(updateRequest(QRect,int)), lineNumberArea, SLOT(updateLineNumberArea(QRect,int)));
    connect(this, SIGNAL(cursorPositionChanged()) , lineNumberArea, SLOT(highlightCurrentLine()));

    lineNumberArea->updateLineNumberAreaWidth(0);
    lineNumberArea->highlightCurrentLine();
}


void CodeEditor::resizeEvent(QResizeEvent *e) {
    QPlainTextEdit::resizeEvent(e);

    QRect cr = contentsRect();
    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberArea->lineNumberAreaWidth(), cr.height()));

};


CodeEditor::~CodeEditor() {

}

linenumbers.h

#include <QtGui/QWidget>
#include "../headers/editor/codeeditor.h"

QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
QT_END_NAMESPACE


class LineNumbers : public QWidget {
public:
    explicit LineNumbers(QPlainTextEdit *codeEditor);
    virtual ~LineNumbers();

    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();
    QSize sizeHint() const;

protected:
    void paintEvent(QPaintEvent *event);

public slots:
    void updateLineNumberAreaWidth(int newBlockCount);
    void highlightCurrentLine();
    void updateLineNumberArea(const QRect &, int);

private:
    QPlainTextEdit *codeEditor;

};

linenumbers.cpp

#include <QtGui>
#include "../headers/editor/linenumbers.h"


LineNumbers::LineNumbers(QPlainTextEdit *editor) : QWidget( editor ) {
    codeEditor = editor;
};


int LineNumbers::lineNumberAreaWidth() {
    int digits = 2;
    int max = qMax(1, codeEditor->blockCount());
    while (max >= 10) {
        max /= 10;
        ++digits;
    }

    int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;

    return space;
};


void LineNumbers::updateLineNumberAreaWidth(int /* newBlockCount */) {
    codeEditor->setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}


void LineNumbers::updateLineNumberArea(const QRect &rect, int dy) {
    if(dy) {
        this->scroll(0, dy);
    } else {
        this->update(0, rect.y(), this->width(), rect.height());
    }

    if(rect.contains(codeEditor->viewport()->rect())) {
        updateLineNumberAreaWidth(0);
    }
};


void LineNumbers::highlightCurrentLine() {
    QList<QTextEdit::ExtraSelection> extraSelections;

    if(!codeEditor->isReadOnly()) {
        QTextEdit::ExtraSelection selection;

        QColor lineColor = QColor("#E1E1E1");

        selection.format.setBackground(lineColor);
        selection.format.setProperty(QTextFormat::FullWidthSelection, true);
        selection.cursor = codeEditor->textCursor();
        selection.cursor.clearSelection();
        extraSelections.append(selection);

    };

    codeEditor->setExtraSelections(extraSelections);
};


void LineNumbers::lineNumberAreaPaintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.fillRect(event->rect(), QColor("#CACACA"));


    QTextBlock block = codeEditor->firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) codeEditor->blockBoundingGeometry(block).translated(codeEditor->contentOffset()).top();
    int bottom = top + (int) codeEditor->blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, this->width(), fontMetrics().height(),
                             Qt::AlignCenter, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) codeEditor->blockBoundingRect(block).height();
        ++blockNumber;
    }
};


QSize sizeHint() const {
    return QSize(lineNumberAreaWidth(), 0);
};


void paintEvent(QPaintEvent *event) {
    lineNumberAreaPaintEvent(event);
};


LineNumbers::~LineNumbers() {

};

当我运行“make”时我得到的错误

codeeditor.o: In function `CodeEditor::CodeEditor(QWidget*)':
undefined reference to `LineNumbers::LineNumbers(QPlainTextEdit*)
undefined reference to `LineNumbers::updateLineNumberAreaWidth(int)
undefined reference to `LineNumbers::highlightCurrentLine()
codeeditor.o: In function `CodeEditor::resizeEvent(QResizeEvent*)
undefined reference to `LineNumbers::lineNumberAreaWidth()

请有人请指出我做错了什么,我一直坚持这个问题,试图解决一段时间,但遗憾的是无法找到问题。

1 个答案:

答案 0 :(得分:1)

链接器无法找到linenumbers.cpp。标题所有这些功能都承诺了,但找不到linenumbers.cpp

可能忘了将linenumbers.cpp添加到.pro文件中?

#include "../headers/editor/codeeditor.h"

中不需要

linenumbers.h