dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

qtcreator: memory overflow in text box (PlainTextEdit) ? [solved]

Sat Oct 05, 2019 8:51 am

I actually want to use a text box (PlainTextEdit) for debug monitoring, like the Serial terminal in Arduino:
https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg

in that case all current pinstates are listed one after the other, beneath each other, showing their actual state (in future of course many other things of interest should be listed if needed or if useful)

my apprehension:
will there once be a memory overflow because the oldest outputs are not deleted any more?
If so, how to avoid that (perhaps with a different monitor text box, other than PlainTextEdit ?)

or limiting the text box to perhaps 1000 lines, and then always automatically deleting the oldest line if a new one appears (sort of FIFO)?
Last edited by dsyleixa123 on Sun Oct 06, 2019 6:55 pm, edited 2 times in total.

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sat Oct 05, 2019 10:14 am

update,
yes: after about 1 hour the qt window program hangs up, freezes, and don't react anymore.

so how to fix that issue and limit the text box memory size, still showing up all new lines though?

Heater
Posts: 13606
Joined: Tue Jul 17, 2012 3:02 pm

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sat Oct 05, 2019 2:41 pm

dsyleixa123,

I have not looked at your code in detail and it is 5 or 6 years since I have done this kind of thing in Qt but think:

1) You should not use pthreads in a Qt project. Rather use Qt's own qthread class. https://doc.qt.io/qt-5/qthread.html That makes the code clean and easy and is cross-platform.

2) If you are passing data between a thread and a Window you may need to register the class that carries the data with the meta object system. https://doc.qt.io/qt-5/qtcore-threads-q ... ample.html

I recall having all kind of problems with data corruption when sending signals between threads and Widgets back then. It took me quite a while to find out one had to register the class used in the signal and then make it all work correctly.

I'm not sure now if one has to register Qt classes but if you create your own to use as messages you had to.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sat Oct 05, 2019 10:11 pm

hi, thanks for yor hints about pthread, but perhaps you con write me code to put my pthread blink code example into a Qrhread code (I don't get it, tbh).
Currently I actually can't reacognize issues yet though. (Perhaps you woudl wish to haev a closer look at my code 8-) )
Anyway, it's sort of off-topic here, ther topic about qt + pthread is this one: https://www.raspberrypi.org/forums/view ... 850f93ef64 - I would appreciate very much to provide some Qthread eample code in that topic!

As to the text box overflow (this topic), that is really an issue currently because in that case it comes to data/memory overflow as actually expected, so I need to clean up the first lines of the text box repeatedly - but how to do that?
Or have anoter mobitor box instead?

User avatar
Paeryn
Posts: 2701
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 12:37 am

dsyleixa123 wrote:
Sat Oct 05, 2019 10:11 pm
As to the text box overflow (this topic), that is really an issue currently because in that case it comes to data/memory overflow as actually expected, so I need to clean up the first lines of the text box repeatedly - but how to do that?
Or have anoter mobitor box instead?
You can limit the amount of blocks that are allowed in a QPlainTextEdit by calling it's setMaximumBlockCount(n) method with n being an integer above 0 (-ve or 0 means unlimited). I'm not totally sure about what constitutes a block but the strings inserted via appendPlainText() or appendHtml() definitely are (it may be that a newline character starts a block but I'm not 100% sure).

If the block count is reached then each new block added afterwards will cause the first block to be deleted, so a simple log viewer would always appendPlainText() a new entry and the widget will only keep the maximumBlockCount newest entries.
She who travels light — forgot something.

Heater
Posts: 13606
Joined: Tue Jul 17, 2012 3:02 pm

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 2:28 am

Using qthread should be pretty simple. See here: https://wiki.qt.io/QThreads_general_usage
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 7:44 am

@Heater: this is not the place for postings about pthread or Qthread, so your post is off-topic.
as stated, the pthread topic is this one, in order to keep this topic clean please post over there about multithreading:
https://www.raspberrypi.org/forums/view ... 850f93ef64
Last edited by dsyleixa123 on Sun Oct 06, 2019 7:53 am, edited 1 time in total.

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 7:51 am

edited because of Heater-off-topic posts
Last edited by dsyleixa123 on Sun Oct 06, 2019 10:24 am, edited 2 times in total.

Heater
Posts: 13606
Joined: Tue Jul 17, 2012 3:02 pm

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 9:52 am

dsyleixa123,
@Heater: this is not the place for postings about pthread or Qthread, so your post is off-topic.
Excuse me, what? Did you forget what you wrote above? I'll remind you:

"hi, thanks for yor hints about pthread, but perhaps you con write me code to put my pthread blink code example into a Qrhread code (I don't get it, tbh)."

As such, and given that you hinted at memory issues and that I took the trouble to peek at your code, I believe I was very much on topic.

I was only trying to be helpful by suggesting qthread and offering a hint from my past experiences with threads and memory sharing problems with Qt.

You now have four threads going here for essentially the same program, sorry if I can't keep up with the chaos.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 9:59 am

@Heater:
which part of
this is not the place for postings about pthread or Qthread, so your post is off-topic.
as stated, the pthread topic is this one, in order to keep this topic clean please post over there about multithreading:
https://www.raspberrypi.org/forums/view ... 850f93ef64
did you not understand?

just to remind you: my complete quote was:
dsyleixa123 wrote:
Sat Oct 05, 2019 10:11 pm
hi, thanks for yor hints about pthread, but perhaps you con write me code to put my pthread blink code example into a Qrhread code (I don't get it, tbh).
Currently I actually can't reacognize issues yet though. (Perhaps you woudl wish to haev a closer look at my code 8-) )
Anyway, it's sort of off-topic here, the topic about qt + pthread is this one: https://www.raspberrypi.org/forums/view ... 850f93ef64 - I would appreciate very much to provide some Qthread eample code in that topic!

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 10:04 am

edited because of Heater-off-topic posts
Last edited by dsyleixa123 on Sun Oct 06, 2019 10:24 am, edited 1 time in total.

Heater
Posts: 13606
Joined: Tue Jul 17, 2012 3:02 pm

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 10:16 am

dsyleixa123,
which part of...did you not understand?
That reads clearly enough to me. Which is why it makes no sense. Coming after your request for help with pthread/qthread as it does.

If you don't want help just say so. No need to be rude.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ?

Sun Oct 06, 2019 10:24 am

back to topic:
Paeryn wrote:
Sun Oct 06, 2019 12:37 am
dsyleixa123 wrote:
Sat Oct 05, 2019 10:11 pm
As to the text box overflow (this topic), that is really an issue currently because in that case it comes to data/memory overflow as actually expected, so I need to clean up the first lines of the text box repeatedly - but how to do that?
Or have anoter mobitor box instead?
You can limit the amount of blocks that are allowed in a QPlainTextEdit by calling it's setMaximumBlockCount(n) method with n being an integer above 0 (-ve or 0 means unlimited). I'm not totally sure about what constitutes a block but the strings inserted via appendPlainText() or appendHtml() definitely are (it may be that a newline character starts a block but I'm not 100% sure).

If the block count is reached then each new block added afterwards will cause the first block to be deleted, so a simple log viewer would always appendPlainText() a new entry and the widget will only keep the maximumBlockCount newest entries.
thank you Paeryn, that sounds as if it's just exactly what I am looking for.
Currently I just have drag+dropped my textbox (PlainTextEdit resp. PlainTextEdit1 ) into my form, so I do not have created yet any slots or signals (events) or even any code about that textbox object, I am just writing text to it (targeting it's name) from out of another function, similar to when I write a text caption into a label, just targeting it's name.


PS,
edit,
I think I found it out:
I added the following line in MainWindow::MainWindow(Qwidget *parent) { } (in mainwindow.cpp)
ui->PlainTextEdit1->setMaximumBlockCount(1000);

at least the compiler didn't beef about that...

PPS,
update:

yes, it overwrites the first couple of lines repeatedly, and it meanwhile runs since 2 hours like a charm, no issues any more.
I think I got it! :)

Thank you very much!

User avatar
Paeryn
Posts: 2701
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: qtcreator: memory overflow in text box (PlainTextEdit) ? [solved]

Sun Oct 06, 2019 9:46 pm

Yes you can do it there, but its property can also be set is in qtcreator's form editor which will automatically set it in the window's ui_setupUi(), I've just tried it...
QPlainTextEdit.png
QPlainTextEdit
QPlainTextEdit.png (29.49 KiB) Viewed 695 times
She who travels light — forgot something.

dsyleixa123
Posts: 423
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator: memory overflow in text box (PlainTextEdit) ? [solved]

Mon Oct 07, 2019 7:41 am

Paeryn wrote:
Sun Oct 06, 2019 9:46 pm
Yes you can do it there, but its property can also be set is in qtcreator's form editor which will automatically set it in the window's ui_setupUi(), I've just tried it...
aah yes, I was looking for sth like that (like I'm used to in Borland C++ Builder's object inspector) - I didn't find it because this submenu down left was completely invisible , my Raspi screen is just not big enough to see all qt designer menu features ;)

Return to “C/C++”