0

I have a QPlainTextEdit, in which I append new lines dynamically. If at any given time the number of lines currently in QPlainTextEdit becomes 500, I remove the oldest line and keep the number of lines constant.

We can easily achieve this by using following:

 ui->plainTextEdit->setMaximumBlockCount(500);

OK. But I dont want the user to know that I remove lines when maximum block count is reached. This means I have to decrease the size of the scrollbar, just like it would decreased automatically, when we do not, set the setMaximumBlockCount, property.

But because we have set the setMaximumBlockCount, property, the size of scrollbar will remain constant after the maximum block count is reached.

If I am able to decrease the size of scroll bar, then we will successfully give the illusion that we are not removing any lines.

My Second question is:

If we successfully give this illusion effect to the user and say we load 10000 lines.

So our QPlainTextEdit, will currently store the last 500 lines of 10000 lines. But because of the illusion effect ,the user will assume he can view all the 10000 lines.

So when the user scrolls up, I have to maintain the illusion effect. This means if the user scrolls up, I need to display the old lines that were removed.

Since the QPlainTextEdit , does not have a prepend , operation, I do this by saving the lines in file, and reading the lines from file when necessary and repopulate the entire QPlainTextEdit. But main thing is we have to keep the scroll bar as it is even when we completely repopulate the QPlainTextEdit, to maintain the illusion effect.

Any ideas or links or answers would be appreciated.

Sumeet
  • 8,086
  • 3
  • 25
  • 45

2 Answers2

0

It seems like you're implementing a log viewer. In such case, the QPlainText is very cumbersome to use because it's quite slow. I'd suggest to use a QListView and configure it so that it's fast.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Yes, you guessed it right. So will it be possible with QListView to have all the functionalities. – Sumeet May 05 '17 at 04:20
  • Also if the illusion effect is possible, can you guide to appropriate links or class that will achieve the illusion effect. – Sumeet May 05 '17 at 04:21
  • My ultimate aim is to achieve optimization. So is my approach correct? – Sumeet May 05 '17 at 04:32
  • Your approach is complex. There are no ready-made classes that would implement the illusion. You have to code it up yourself, by overriding the functionality of the scrollbars. Using a list view will likely be much simpler. It already implements the "illusion" for you, i.e. it doesn't keep any data itself, referring to the model for data. – Kuba hasn't forgotten Monica May 08 '17 at 12:45
0

I've provide you perfect solution in another of your questions: https://stackoverflow.com/a/43771056/1387438

Subclass QAbstractListModel implement cache there. When cell value is read you are fetching data from cache and update it if value is not present in cache.

Tweak QTableView, by altering delegate to achieve needed visualization of cells. In case QListView remember to tweak it properly as pint out in alternative answer, but it looks like you should use a QTableView anyway.

In other answer there is example for large file hex viewer so you should use same approach with text.

Community
  • 1
  • 1
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Me being rookie in Qt, I am finding it hard to understand. Plus I have to incorporate my implementation in already written code. So, the UI components and already existing code has to be taken care of at the same time. But Code is so large that I cannot paste it here. – Sumeet May 05 '17 at 10:10