gpt4 book ai didi

c++ - Qt C++ 拆分类

转载 作者:行者123 更新时间:2023-11-28 07:25:37 24 4
gpt4 key购买 nike

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

他们将 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

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

}

行号.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;

};

行号.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()

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

最佳答案

链接器找不到 linenumbers.cpp。 header promise 所有这些功能,但找不到 linenumbers.cpp

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

#include "../headers/editor/codeeditor.h"linenumbers.h 中不需要

关于c++ - Qt C++ 拆分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com