Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt c++;分班_C++_Qt - Fatal编程技术网

C++ Qt c++;分班

C++ Qt c++;分班,c++,qt,C++,Qt,我正在使用Qt中的CodeEditor示例,来自这个url。 他们将LineNumberArea和CodeEditor类放在同一个文件中。我试图将它们拆分为两个单独的头文件/源文件。但我一直得到一个“未定义引用”错误。这是我的密码: 代码编辑器.h #include <QPlainTextEdit> #include <QObject> #include "../headers/editor/linenumbers.h" QT_BEGIN_NAMESPACE clas

我正在使用Qt中的CodeEditor示例,来自这个url。

他们将LineNumberArea和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;

};

请有人指出我做错了什么,我一直在努力解决这个问题,但不幸的是,一直没有找到问题。

链接器找不到
行号。cpp
。header承诺提供所有这些函数,但找不到
linenumbers.cpp

是否可能忘记将
行号.cpp
添加到.pro文件


行号中不需要“./headers/editor/codeditor.h”
。h

啊哈!是的,那肯定行。谢谢:)@user2444217很高兴,我可以帮助:)接受它作为答案,如果这是你的问题的答案:)
#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() {

}
#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;

};
#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() {

};
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()