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

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

Domanda

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').

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top