如何正确地将QWidget子类化以在QPlainTextEdit上构建标尺

时间:2019-06-14 22:52:27

标签: c++11 qt5 qwidget qpainter qplaintextedit

在尝试在QGraphicsView上设计标尺的my previous post之后,我遵循了official documentation上的教程进行垂直开发。

在垂直部分上,我可以看到所有数字,但是在标尺顶部看不到水平线。我试图显示的示例数字为1到30,但是水平标尺上没有任何显示。在学习完本教程之后,我将QWidget细分为子类,并在尝试实现其余部分的地方创建了派生的class LineNumberTop。如果复制并粘贴小项目,它将编译并运行。

但是在实现过程中,我确实成功地获得了垂直的工作面,但是没有水平的工作面。从下面的打印屏幕可以看到:

exampleRuler

我认为,再次QWidget子类化是一个好主意,因为这是创建一个附加类并为class LineNumberArea : public QWidget重新使用函数的问题。但是我并没有在这里停留,而是发现了this的其他来源,这对于理解如何设置标尺的尺寸非常有用。 但是,我不是完全有把握的另一点是在构造函数的private成员中,我在其中设置了QWidget的两倍,并且我不确定是否会覆盖另一个:

private:
    QWidget *lineNumberArea;
    QWidget *lineNumberAreaTop;

此练习使用的完整代码如下:

codeeditor.h

#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <QObject>
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumberArea;
class LineNumberTop;

class CodeEditor : public QPlainTextEdit
{
    Q_OBJECT

public:
    CodeEditor(QWidget *parent = nullptr);
    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();

    void lineNumberAreaPaintEventTop(QPaintEvent *event);
    int lineNumberAreaWidthTop();

protected:
    void resizeEvent(QResizeEvent *event) override;

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

private:
    QWidget *lineNumberArea;
    QWidget *lineNumberAreaTop;
};

class LineNumberArea : public QWidget
{
public:
    LineNumberArea(CodeEditor *editor) : QWidget(editor) {
        codeEditor = editor;
    }
    QSize sizeHint() const override {
        return QSize(codeEditor->lineNumberAreaWidth(), 0);
    }
protected:
    void paintEvent(QPaintEvent *event) override {
        codeEditor->lineNumberAreaPaintEvent(event);
    }
private:
    CodeEditor *codeEditor;
};

class LineNumberTop : public QWidget
{
public:
    LineNumberTop(CodeEditor *editor) : QWidget(editor) {
        codeEditor = editor;
    }
    QSize sizeHint() const override {
        return QSize(codeEditor->lineNumberAreaWidthTop(), 0);
    }
protected:
    void paintEvent(QPaintEvent *event) override {
        codeEditor->lineNumberAreaPaintEventTop(event);
    }
private:
    CodeEditor *codeEditor;
};
#endif

codeeditor.cpp

#include <QtWidgets>
#include "codeeditor.h"

CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
    lineNumberArea = new LineNumberArea(this);
    lineNumberAreaTop = new LineNumberTop(this);

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

    updateLineNumberAreaWidth(0);
    highlightCurrentLine();
}

int CodeEditor::lineNumberAreaWidth()
{
    int digits = 1;
    int max = qMax(1, blockCount());
    while (max >= 10) {
        max /= 10;
        ++digits;
    }
    int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
    return space;
}

void CodeEditor::lineNumberAreaPaintEventTop(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);
    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
    QString number = QString::number(blockNumber + 1);
    painter.setPen(Qt::black);
    painter.drawText(0, top, lineNumberAreaTop->width(), fontMetrics().height(),
                     Qt::AlignRight, number);
}

int CodeEditor::lineNumberAreaWidthTop()
{
    int digits = 1;
    int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('20')) * digits;
    return space;
}

void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}

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

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

void CodeEditor::resizeEvent(QResizeEvent *e)
{
    QPlainTextEdit::resizeEvent(e);
    QRect cr = contentsRect();
    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
    lineNumberAreaTop->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidthTop(), cr.height()));
}

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

    if (!isReadOnly()) {
        QTextEdit::ExtraSelection selection;
        QColor lineColor = QColor(Qt::yellow).lighter(160);
        selection.format.setBackground(lineColor);
        selection.format.setProperty(QTextFormat::FullWidthSelection, true);
        selection.cursor = textCursor();
        selection.cursor.clearSelection();
        extraSelections.append(selection);
    }
    setExtraSelections(extraSelections);
}

void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) 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, lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
        }
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}

main.cpp

#include <QtWidgets>
#include "codeeditor.h"

int main(int argv, char **args)
{
    QApplication app(argv, args);
    CodeEditor editor;
    editor.setWindowTitle(QObject::tr("Code Editor Example"));
    editor.show();
    return app.exec();
}

感谢您指出了解决此问题的正确方向。

0 个答案:

没有答案