my application (Qt 4.6) requires me to display certain messages in a list which always scrolls down upon adding a new line (so the most recent line is always visible).
As I am having performance issues in the whole process which includes displaying these one-line messages, I've run some tests and found the QListWidget I am using for this to be the main issue.
I've created a simple test project with a default list widget "listWidget" and a push button "pushButton" which adds 1000 items in a loop on click. Those two widgets are added in a layout on the main window. Here's the .cpp code (.h is default + the slot definition)
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_released()
{
for( int iLine = 0; iLine < 1000; iLine++ )
{
ui->listWidget->addItem(
QString( "%1: This is a dummy text" )
.arg( QString::number( iLine ).rightJustified( 4, '0' ) )
);
ui->listWidget->scrollToBottom();
QApplication::processEvents();
}
}
Without the scrollToBottom() the performance is alright, but if I add scrollToBottom, it also requires me to add processEvents() for a repaint, and things start getting really slow. When you resize the height of the window (and implicitly the list widget), you can literally watch the update speed changing.
I've tried playing around with performance flags like adding the following lines to the constructor:
ui->listWidget->setLayoutMode( QListWidget::Batched );
ui->listWidget->setBatchSize( 10 );
This speeds it up alot, but scrollToBottom() does no longer work.
Does anyone have an idea how to improve the speed? Feel free to suggest completely different approaches as long as they're using Qt.
[Edit] Taking a look at the performance of e.g. the Details list in the Install Dialogue of Qt Creator or programs like wireshark which don't have problems displaying a couple of lines per second, I know autoscrolling lists which update at a high speed are possible in general. The main question is: Is this possible with Qt?