1

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

Sara Barlo
  • 23
  • 6

1 Answers1

1

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.

Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
  • but the horizontal bar is not visible until a long line is written. is there a way to make the scroller activated be default? thanks – Sara Barlo Oct 11 '13 at 19:10
  • 1
    Why do you need a scroll bar if there are no long lines in the editor? Both vertical and horizontal scroll bars appear only when the content exceed the editor dimensions. – Mahmoud Hassan Oct 11 '13 at 19:42