user7179690
user7179690

Reputation: 1123

How to make pages look in QTextEdit or QPlainTextEdit

I am trying to make the look of pages in QTextEdit or using it to do so.

Each page has a limited number of lines per page and a limited number of character per line and the first thing is the look and feel of pages.
so I try to use this code.

QVBoxLayout *layout =new QVBoxLayout();
ui->scrollArea->setLayout(layout);
ui->scrollArea->setWidgetResizable(true);

QTextEdit *edit = new QTextEdit("hello world");
edit->setSizePolicy(QSizePolicy::QSizePolicy::Preferred,QSizePolicy::Preferred);
QTextEdit *edit1 = new QTextEdit("hello world");
edit1->setSizePolicy(QSizePolicy::QSizePolicy::Preferred,QSizePolicy::Preferred);
QTextEdit *edit2 = new QTextEdit("hello world");
edit2->setSizePolicy(QSizePolicy::QSizePolicy::Preferred,QSizePolicy::Preferred);
// i added more
layout->addWidget(edit);
layout->addWidget(edit1);
layout->addWidget(edit2);

This idea is simple just use scrollbar container and add it vertical layout, and every time I need a page, make a new QTextEdit and add it to the vertical layout in the scrollbar.

The problem here is that whenever I add a new page, the QTextEdit I add it so too small and the scrollbar never work on it so that the QTextEdit be above each other so it makes a bad look.

Image

So what I make wrong so that the scrollbar not working, and how to make each QTextEdit take a good page look which has a good size to give me the look of something like Microsoft word or pdf which have pages.

Also if there is a better idea or a solution to what I am trying to do it will be better if there is an already implemented widget or library have this.

Upvotes: 0

Views: 481

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

You do not have to replace the existing layout of the QScrollArea, what you have to do is create a widget, and in that widget set the layout. After that widget you must place it in the QScrollArea through the setWidget() method, if you want the height shown to be larger then set a minimum size.

QWidget *contentWidget = new QWidget;

QVBoxLayout *layout =new QVBoxLayout(contentWidget);
ui->scrollArea->setWidget(contentWidget);
ui->scrollArea->setWidgetResizable(true);

QTextEdit *edit = new QTextEdit("hello world");
edit->setMinimumHeight(200);
// create others QTextEdit
layout->addWidget(edit);
// add the QTextEdits 

Upvotes: 1

Related Questions