Solved How to make pages in QTextEdit or QPlainTextEdit
Iam trying to make the look of pages in QTextEdit or using it to so each page has a limited number of lines per page and 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 code 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
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 iam trying to do it will be better if there is an already implemented widget or library have this.
Thanks in advance
You have to set minimumSize on the QTextEdit as
not to have them become very small.
Should be the size of a page.
i add those before set the layout
edit->setMinimumSize(500,500); edit1->setMinimumSize(500,500); edit2->setMinimumSize(500,500); edit3->setMinimumSize(500,500); edit4->setMinimumSize(500,500);
and not work and the scrollbar still not working too
That is odd.
It works with no issues in Designer
When you draw it, it seems to create a widget (scrollAreaWidgetContents_2) and put layout on that
scrollArea_2 = new QScrollArea(centralWidget); scrollArea_2->setWidgetResizable(true); scrollAreaWidgetContents_2 = new QWidget(); verticalLayout_2 = new QVBoxLayout(scrollAreaWidgetContents_2); textEdit = new QTextEdit(scrollAreaWidgetContents_2); textEdit->setMinimumSize(QSize(0, 500)); verticalLayout_2->addWidget(textEdit); scrollArea_2->setWidget(scrollAreaWidgetContents_2);
@mrjj can you please show me the code it not work for me i don't know why
I think you need to make a widget, put layout on that and
insert edits there.
then set widget to scroll area with