I wanted to know how's it possible to list number of items like we're showing some logs. actually I received some sorts of packets from the network and I want to show some kind of log on the GUI for the user. Currently I've chosen List Widget but I was interested to know if there's any other way to do so?
3 Answers
I use qInstallMsgHandler and then redirect to the GUI, console or log file depending on some command-line switches. If you have a lot of messages you might want to log to a file, but for short bursts of lines / items you can log to a custom QStringListModel and maybe a QListView with it.
Basically I have expanded on the example in the referenced link and a SO Question
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
void myMessageOutput(QtMsgType type, const char *msg)
{
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s\n", msg);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s\n", msg);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s\n", msg);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s\n", msg);
abort();
}
}
int main(int argc, char **argv)
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
...
return app.exec();
}

- 1
- 1

- 2,971
- 1
- 23
- 39
Guessing your purpose is only for logging, you can use qDebug()
as it is the simplest and easiest to log.

- 3,074
- 3
- 19
- 32
-
That could be used too, but I want to know what is the best widget to show logs on to it on Qt for the user on the GUI. – Novin Shahroudi May 15 '12 at 13:42
If you want to have some rich features like search, filtering and sorting I would even use QTreeWidget to split data into columns. List/Tree Widget would be good because you have quick append, quick remove.
Some people use text widgets to do such thing, but it has worse performence when there is a lot of data.

- 12,884
- 2
- 43
- 58
-
So Tree Widget is much better than Text Browser in performance for large amount of data? – Novin Shahroudi May 15 '12 at 15:03
-
Model based data storage has better performance than Text Browser – Kamil Klimek May 15 '12 at 15:32
-
Way better performance than QTextEdit or QPlainTextEdit. The latter two are completely useless for logs, and seem to have had unresolved performance regressions in the last year or two. They are only useful when the modifications are done at a human pace. Changing their contents often is a no-no. I ended up writing a custom LogView class, but using a model and a view is equally plausible. – Kuba hasn't forgotten Monica Jun 01 '12 at 06:41