Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2008
Is there a QWidget for...


Message 1 in thread

Hello,

I  have an application which runs continously and periodically needs to
display text messages to some kind of status/log window.  Currently, I am
using a modified QTextEdit to display a string generated via join from a
QStringList that I use to hold the last 100 messages.  When the list is
full and I get a new string to display, I pop the oldest string from the
front off the list.

Anyhow, I was wondering if anyone had a better way to do this, or if there
was a widget specifically designed for a scrollable log that stores up to N
messages.

Thanks,
Thomas Wharton

--
 [ signature omitted ] 

Message 2 in thread

Thomas Wharton wrote:
> Hello,
> 
> I  have an application which runs continously and periodically needs to
> display text messages to some kind of status/log window.  Currently, I am
> using a modified QTextEdit to display a string generated via join from a
> QStringList that I use to hold the last 100 messages.  When the list is
> full and I get a new string to display, I pop the oldest string from the
> front off the list.
> 
> Anyhow, I was wondering if anyone had a better way to do this, or if there
> was a widget specifically designed for a scrollable log that stores up to N
> messages.

How about a QTreeWidget? I don't know how "big" your messages are but it 
supports multi-line items I believe. Or you could do a summary line in 
the view with a detailed display in a separate line edit.

-- 
 [ signature omitted ] 

Message 3 in thread

Thomas Wharton wrote:
> Hello,

Hiya.

> I  have an application which runs continously and periodically needs to
> display text messages to some kind of status/log window.  Currently, I am
> using a modified QTextEdit to display a string generated via join from a
> QStringList that I use to hold the last 100 messages.  When the list is
> full and I get a new string to display, I pop the oldest string from the
> front off the list.
> 
> Anyhow, I was wondering if anyone had a better way to do this, or if there
> was a widget specifically designed for a scrollable log that stores up to N
> messages.

What I've done in the past is used a QListView with a QStringListModel.
 I subclassed the model as a LogStringListModel, with a public log(const
QString &) slot.  This slot basically appends the parameter and then
removes the first item if the size is greater than some size I passed in
to the constructor.

Then in my app, all I have to do is call m_model->log("some log
string").  The view is automagically updated because the log() function
emits dataChanged().

For added convenience, you can subclass QApplication and add a log(const
QString &) slot to that.  Then you can define a macro similar to the
qApp macro that returns the instance of YourApplication object.  That
way from anywhere in your app, you can just call myApp->log("some log
message"), provided you #include "your_application.h".

Hope that helps.

Cheers,
Darrik

-- 
 [ signature omitted ] 

Message 4 in thread

On Mon, Apr 21, 2008 at 12:39 PM, Thomas Wharton <
Thomas_Wharton@xxxxxxxxxxxx> wrote:

>
> Hello,
>
> I  have an application which runs continously and periodically needs to
> display text messages to some kind of status/log window.  Currently, I am
> using a modified QTextEdit to display a string generated via join from a
> QStringList that I use to hold the last 100 messages.  When the list is
> full and I get a new string to display, I pop the oldest string from the
> front off the list.
>
> Anyhow, I was wondering if anyone had a better way to do this, or if there
> was a widget specifically designed for a scrollable log that stores up to
> N
> messages.
>

The QTextEdit has a QTextDocument, and you can use
QTextDocument::setMaximumBlockCount(100) to say that only the last 100 lines
should be kept. Then you can just call QTextEdit::append() as each new
message comes in.

Tom

Message 5 in thread

Thomas Wharton wrote:
> Hello,
> 
> I  have an application which runs continously and periodically needs to
> display text messages to some kind of status/log window.  Currently, I am
> using a modified QTextEdit to display a string generated via join from a
> QStringList that I use to hold the last 100 messages.  When the list is
> full and I get a new string to display, I pop the oldest string from the
> front off the list.
> 
> Anyhow, I was wondering if anyone had a better way to do this, or if there
> was a widget specifically designed for a scrollable log that stores up to N
> messages.

If you are or are planning on using 4.4, you should take a look at the new
QPlainTextEdit widget:
http://doc.trolltech.com/4.4rc1/qplaintextedit.html#details

You'll like this section,
http://doc.trolltech.com/4.4rc1/qplaintextedit.html#using-qplaintextedit-as-a-display-widget, 

which says "If you want to limit the total number of paragraphs in a
QPlainTextEdit, as it is for example useful in a log viewer, then you can
use the maximumBlockCount property. The combination of
setMaximumBlockCount() and appendPlainText() turns QPlainTextEdit into an
efficient viewer for log text."

-- 
 [ signature omitted ] 

Message 6 in thread

If your way works, then congratulations.

As to "Is there a better way ...?", is there any specific functionality you want ?

Have you considered feeding your status log into an SQLite database ?  All you would need is the text string and a time stamp, and then your display would be a simple database viewer that gets the 100 newest lines.  You can add functionality to your viewer app to optionally clean out older lines periodically to keep the table from growing too large.

 -------------- Original message ----------------------
From: Thomas Wharton <Thomas_Wharton@xxxxxxxxxxxx>
> 
> Hello,
> 
> I  have an application which runs continously and periodically needs to
> display text messages to some kind of status/log window.  Currently, I am
> using a modified QTextEdit to display a string generated via join from a
> QStringList that I use to hold the last 100 messages.  When the list is
> full and I get a new string to display, I pop the oldest string from the
> front off the list.
> 
> Anyhow, I was wondering if anyone had a better way to do this, or if there
> was a widget specifically designed for a scrollable log that stores up to N
> messages.
> 
> Thanks,
> Thomas Wharton
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ] 

Message 7 in thread

Am Montag, 21. April 2008 schrieb ygor@xxxxxxxxxxx:
> If your way works, then congratulations.
> As to "Is there a better way ...?", is there any specific functionality you
> want ?
> Have you considered feeding your status log into an SQLite database ?  All
> you would need is the text string and a time stamp, and then your display
> would be a simple database viewer that gets the 100 newest lines.  You can
> add functionality to your viewer app to optionally clean out older lines
> periodically to keep the table from growing too large.

But be warned that sqlite is _very_ slow when you add more than say 100 
entries per second. Even when the sqlite-db-file is on a tmpfs this 
significantly slowed down my app.

So don't do big debugging if you choose to go that way.

But why would you need a full database, when all you want is a simple table. 
Maybe a model with a list of structs would be enough. The QListView-idea is 
nice...

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 8 in thread

That sounds very nice, Darrik.
I am going to try it that way.

 -------------- Original message ----------------------
From: Darrik Mazey <darrik@xxxxxxxxxxxxxxxxxx>
> Thomas Wharton wrote:
> > Hello,
> 
> Hiya.
> 
> > I  have an application which runs continously and periodically needs to
> > display text messages to some kind of status/log window.  Currently, I am
> > using a modified QTextEdit to display a string generated via join from a
> > QStringList that I use to hold the last 100 messages.  When the list is
> > full and I get a new string to display, I pop the oldest string from the
> > front off the list.
> > 
> > Anyhow, I was wondering if anyone had a better way to do this, or if there
> > was a widget specifically designed for a scrollable log that stores up to N
> > messages.
> 
> What I've done in the past is used a QListView with a QStringListModel.
>  I subclassed the model as a LogStringListModel, with a public log(const
> QString &) slot.  This slot basically appends the parameter and then
> removes the first item if the size is greater than some size I passed in
> to the constructor.
> 
> Then in my app, all I have to do is call m_model->log("some log
> string").  The view is automagically updated because the log() function
> emits dataChanged().
> 
> For added convenience, you can subclass QApplication and add a log(const
> QString &) slot to that.  Then you can define a macro similar to the
> qApp macro that returns the instance of YourApplication object.  That
> way from anywhere in your app, you can just call myApp->log("some log
> message"), provided you #include "your_application.h".
> 
> Hope that helps.
> 
> Cheers,
> Darrik
> 
> -- 
> Darrik Mazey
> Developer
> DMT Programming, LLC.
> P.O. Box 91
> Torrington, CT 06790
> mobile: 330.808.2025
> office: 330.785.1269
> darrik@xxxxxxxxxxxxxxxxxx
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ]