Qt - invalid use of incomplete type 'class QScrollBar' - Add horizontal scroll bar to text edit widget

StackOverflow https://stackoverflow.com/questions/19300217

The default QPlainTextEdit has only vertical scroll bar, I want to add horizontal scroll bar.

I tried this (this code in the constructor of the QMainWindow class)

QPlainTextEdit * editor = new QPlainTextEdit(this);
QScrollBar * hScroll = new QScrollBar(Qt::Horizontal);
editor->addScrollBarWidget(hScroll);
setCentralWidget(editor);

but the build failed with the error (invalid use of incomplete type 'class QScrollBar').

有帮助吗?

解决方案

You can easily add horizontal scroll bar in the QPlainTextEdit Widget by setting the line wrap property, your code should be:

QPlainTextEdit * editor = new QPlainTextEdit(this);
editor->setLineWrapMode(QPlainTextEdit::NoWrap);
setCentralWidget(editor);

QPlainTextEdit::NoWrap mode will automatically add the horizontal scroll bar when the line width exceed the editor width.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top