Qt Quick调试之显示代码行号

在Qt Quick中,使用console打印日志时默认不显示行号。通过设置QT_MESSAGE_PATTERN环境变量,可以在Qt 5.0及更高版本中实现显示。对于使用qmlscene调试的用户,可在Windows下进行相应设置。而在Qt Creator中,可以在Run Environments中添加环境变量。本文回顾了Qt Quick系列教程,涵盖了QML基础、事件处理、组件创建、布局、图像处理等多个主题。
摘要由CSDN通过智能技术生成

    QML 文档可以使用 console 对象来打印日志信息,可默认是无法输出行号的。帮助文档中说需要设置 QML_CONSOLE_EXTENDED 环境变量即可,测试了一下不管用。后来找到 qt.gitorious.org 上的一个文章(链接点这里),说 Qt 5.0 之后这个宏已经随风而去了。不过有更好用的方式,设置 QT_MESSAGE_PATTERN 。

    如果你使用qmlscene来调试qml文档,在Windows下这样设置:

set QT_MESSAGE_PATTERN=%{file}:%{line} %{message}

    然后就可以 qmlscene 加载 qml 文档来测试了,看图:

QtQTextEdit 组件默认是没有行号显示的,但是可以通过自定义 QSyntaxHighlighter 类来实现行号显示。 下面是一个简单的示例代码: ```cpp class LineNumberArea : public QWidget { public: LineNumberArea(QTextEdit* editor) : QWidget(editor) { m_editor = editor; } QSize sizeHint() const override { return QSize(m_editor->lineNumberAreaWidth(), 0); } protected: void paintEvent(QPaintEvent* event) override { m_editor->lineNumberAreaPaintEvent(event); } private: QTextEdit* m_editor; }; class TextEdit : public QTextEdit { public: TextEdit(QWidget* parent = nullptr) : QTextEdit(parent) { m_lineNumberArea = new LineNumberArea(this); connect(this, &TextEdit::blockCountChanged, this, &TextEdit::updateLineNumberAreaWidth); connect(this, &TextEdit::updateRequest, this, &TextEdit::updateLineNumberArea); connect(this, &TextEdit::cursorPositionChanged, this, &TextEdit::highlightCurrentLine); updateLineNumberAreaWidth(); highlightCurrentLine(); } protected: void resizeEvent(QResizeEvent* event) override { QTextEdit::resizeEvent(event); QRect cr = contentsRect(); m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); } private: void updateLineNumberAreaWidth() { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); } void updateLineNumberArea(const QRect& rect, int dy) { if (dy) m_lineNumberArea->scroll(0, dy); else m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height()); if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(); } void lineNumberAreaPaintEvent(QPaintEvent* event) { QPainter painter(m_lineNumberArea); painter.fillRect(event->rect(), Qt::lightGray); QTextBlock block = firstVisibleBlock(); int blockNumber = block.blockNumber(); int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top()); int bottom = top + qRound(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, m_lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number); } block = block.next(); top = bottom; bottom = top + qRound(blockBoundingRect(block).height()); ++blockNumber; } } void 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); } int lineNumberAreaWidth() { int digits = 1; int max = qMax(1, blockCount()); while (max >= 10) { max /= 10; ++digits; } int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; return space; } private: LineNumberArea* m_lineNumberArea; }; ``` 在上面的代码中,我们自定义了 LineNumberArea 和 TextEdit 两个类。其中,LineNumberArea 是用来显示行号的 QWidget 子类,而 TextEdit 则是用来显示文本和行号QTextEdit 子类。 在 TextEdit 类中,我们重载了 resizeEvent()、updateRequest() 和 cursorPositionChanged() 三个函数,并且连接了它们的信号到对应的槽函数中。这些槽函数用来更新行号区域的大小、内容和当前行的高亮显示。 注意,在 TextEdit 类中,我们还重载了 setViewportMargins() 函数,用来设置视口的外边距。这里我们将左边距设置为行号区域的宽度,以便在 QTextEdit 中显示行号。 最后,我们可以像下面这样使用 TextEdit 类来显示带有行号的文本编辑器: ```cpp int main(int argc, char** argv) { QApplication app(argc, argv); TextEdit editor; editor.show(); return app.exec(); } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

foruok

你可以选择打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值