Qt QListWidget performance issues - qt

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?

The view tries to calculate the individual size of each new item. Since you probably don't need that, you can disable it and gain some speed with:
ui->listWidget->setUniformItemSizes(true);
And you don't really need either to "update the widget whenever a line is added", even if that was possible, because you won't see any difference above a certain update rate.
So, you can use a timer (QTime or QElapsedTimer) to limit the rate at which you actually scroll and force a repaint:
void MainWindow::on_pushButton_released()
{
static QTime rateTimer;
rateTimer.start();
for( int iLine = 0; iLine < 50000; iLine++ )
{
ui->listWidget->addItem(
QString( "%1: This is a dummy text" )
.arg( QString::number( iLine ).rightJustified( 5, '0' ) )
);
// Limit at 60 updates/s
if(rateTimer.elapsed() > 1000/60) {
ui->listWidget->scrollToBottom();
QApplication::processEvents();
rateTimer.restart();
}
}
// For the items added after the last processEvents()
ui->listWidget->scrollToBottom();
}
But for very large lists, you might have to write your own model derived from QAbstractListModel, because QListWidget insertion speed seems to decrease rapidly with the number of item already in the list.

I am not sure if this will solve your problem or not but it may offer a different approach.
QWidget has updatesEnabled property that allows your control whether widget receives paint events or not.
Try disabling this properyt then populate your list and then re-enable it to paint your widget back.
I hope this helps.

Related

Custom Qt designer widget : a scroll Area containing a custom vertical layout

I want to do something fairly simple : add a custom widget to Qt designer that would basically be a scrollArea containing a custom vertical layout(I added some code to the vertical layout in order to handle its objects for my projects).
The idea would be to represent a vertical menu that would be on the side of my screen
What I have done so far
I created the custom widget plugin and my custom layout.
My custom widget codes looks like this:
#include "menuwidget.h"
MenuWidget::MenuWidget(QWidget *parent) :
QScrollArea(parent)
{
this->setWidgetResizable(true);
QWidget* layoutHoldingWidget= new QWidget(this);
layout= new MenuLayout();
layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
layout->addStretch(1);
layoutHoldingWidget->setLayout(layout);
this->setWidget(layoutHoldingWidget);
}
If I add manually to the layout (in the constructor code) some buttons
for(int i =0;i<20;i++)
layout->addWidget(new QPushButton(this));
It does work and I can see my scrollArea containing some buttons, which is almost what I want.
What I want
I would like to be able to add these buttons directly via Qt designer: the user would first drag the empty MenuWidget on the main window, then would drag QPushButtons on my custom widget exactly like he would do on a regular vertical layout.
Is that possible?How could I do such a thing?
Thank you ! :)
Edit 1
What I was missing was the "scrollAreaWidgetContents" widget that is always created when you drag and drop a QScrollArea. I did a similar thing by adding a widget (let's call it containerWidget) to my custom scrollArea in its domXml function, which enables me to drag and drop widgets on my scroll Area like I wanted to do.
BUT there's still something I can't figure out : I want the containerWidget to have a customLayout (myCustomLayout) . If I add it in the domXml function, I get the following line in the terminal :
Designer:The layout type 'MyCustomLayout' is not supported,
defaulting to grid.
So it means that I can't tell designer to use my custom layout to place my widgets, which is kind of sad :D
Is there any way to "cheat" here?
There are two things to consider:
1) Overwrite in the class you derive from QDesignerCustomWidgetInterface the function to return true
bool isContainer() const { return true; }
This tells QtDesigner that the widget can contain children. (In Qt nearly any Widget can contain any widget as child, but QtDesigner tries to restrict it in a sensible way - e.g. you cant add children to a QLabel in QtDesigner)
2) Implement childEvent of your Widget. Probably in your case it would add Widgets added in QtDesigner to a layout.
Here is a core I've implemented to try this out. I've created a skeleton using "Qt Widget Plugin" Wizard in QtCreator and modified a little bit.
Don't forget to build as release, for the compiler/Qt-version of your QtDesigner , to copy the .dll and .lib files in \plugins\designer directory and to restart QtDesigner!
verticalplugin.cpp
//all other functions remained as created by QtCreator wizard
bool VerticalMenuPlugin::isContainer() const
{
return true;
}
VerticalMenu.h
#ifndef VERTICALMENU_H
#define VERTICALMENU_H
#include <QtGui/QWidget>
#include <QtGui/QVBoxLayout>
class VerticalMenu : public QWidget
{
Q_OBJECT
protected:
virtual void childEvent ( QChildEvent * event );
public:
VerticalMenu(QWidget *parent = 0);
};
#endif
VerticalMenu.cpp
#include "verticalmenu.h"
#include <QChildEvent>
VerticalMenu::VerticalMenu(QWidget *parent) :
QWidget(parent)
{
setLayout (new QVBoxLayout);
}
void VerticalMenu::childEvent ( QChildEvent * event )
{
if ( event->added() )
{
QWidget * newChild = qobject_cast<QWidget *>(event->child());
if ( newChild )
{
layout()->addWidget( newChild );
}
}
}
I hope' it would help as a starting point.
Qt 4 does not support custom layout plugins for designer, so I couldn't achieve what I wanted to do. I will instead use a Vertical Layout and try to implement the additional features that were supposed to be in the custom layout code in the widget code.

Can you set a QWidget to take up an entire column of a QTreeWidget?

I have a custom QTreeWidget subclass that I'm using to display track names/etc. in my MIDI editor project (https://github.com/waddlesplash/ragingmidi). I'd like to add another column to this tree widget, but with one widget taking up the whole column and not per-item widgets.
Is this possible or will I have to figure out some other solution?
EDIT: I'm trying to accomplish something like this: http://www.anvilstudio.com/compose.jpg - see the last "column" in the header view (3rd after "L/R Balance") showing all the lines/notes (which is entirely custom, and written in VB.NET and closed-source anyway).
EDIT 2: You can't see it, but the last column scrolls without the other columns scrolling in the above picture. In their method, you have to scroll using the mouse. I want a scrollbar.
Looking at the Qt documentation, there seems to be a few options to accomplish this, however there are a few important factors to address before you can decide what approach best suits your needs.
Is the content being displayed in this custom tree column static or dynamic?
Is there a one to one mapping of rows from your QTreeWidget to your custom tree column?
If your custom tree column content IS static and there IS a one to one mapping of rows , use of the QTreeWidget::setItemWidget ( QTreeWidgetItem * item, int column, QWidget * widget ) function should suffice.
However, if the content of your custom tree column is dynamic OR there is not a one to one mapping of rows, this will require a more complex approach.
As described in the documentation for QTreeWidget; "If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate. "
QItemDelegate, and its sub classes, perform all drawing facilities for items inserted into Qt item views (like QTreeView, QListView, QTableView, etc..). This essentially allows you to control ALL drawing operations for any item inserted into a QTreeView class, letting you draw dynamic content in addition to being able to extend content across multiple rows.
Having implemented a similar approach for a QListWidget, I recommend using QStyledItemDelegate in lieu of QItemDelegate as it allows you to more easily integrate this widget with your application's style layout. As you did not detail the exact use of this custom QWidget, you also might need the additional facilities provided by QItemEditorCreator, QItemEditorCreatorBase and QItemEditorFactory. I would post the similar widget I developed here if I could, but sadly it is part of a proprietary software suite.
This is not completely pretty, because it got it's problems when the custom widget is in the right-most column and the column is made narrow, but it's a start:
#include <QtGui>
class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
TreeWidget();
QRect columnRect(int column) const;
private slots:
void repositionColumnWidget();
private:
QPushButton * mColumnWidget;
};
TreeWidget::TreeWidget()
: mColumnWidget(new QPushButton("Custom Column Button", viewport()))
{
const int COLUMN_COUNT = 6;
setColumnCount(COLUMN_COUNT);
for (int row = 0; row < 400; ++row)
{
QStringList columns;
for (int column = 0; column < COLUMN_COUNT; ++column)
{
columns << QString("row %1, column %2").arg(row + 1).arg(column + 1);
}
addTopLevelItem(new QTreeWidgetItem(columns));
}
for (int column = 0; column < COLUMN_COUNT; ++column)
{
resizeColumnToContents(column);
}
repositionColumnWidget();
mColumnWidget->show();
connect(header(), SIGNAL(sectionResized(int,int,int)), this, SLOT(repositionColumnWidget()));
connect(header(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(repositionColumnWidget()));
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(repositionColumnWidget()));
connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(repositionColumnWidget()));
}
QRect TreeWidget::columnRect(int column) const
{
int itemCount = topLevelItemCount();
if (!itemCount)
{
return QRect();
}
int columnX = header()->sectionViewportPosition(column);
int columnY = visualItemRect(topLevelItem(0)).top();
int columnWidth = header()->sectionSize(column);
int columnHeight = visualItemRect(topLevelItem(itemCount-1)).bottom() - columnY + 1;
return QRect(columnX, columnY, columnWidth, columnHeight);
}
void TreeWidget::repositionColumnWidget()
{
mColumnWidget->setGeometry(columnRect(3));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true);
TreeWidget treeWidget;
treeWidget.resize(800, 600);
treeWidget.show();
return a.exec();
}
#include "main.moc"
The though that has come to my mind after a week is to hijack the H-scrollbar for the QTreeWidget, and then make that scrollbar scroll only the final column. Because right now, all the columns fit when the window is 620x670px, and who has a screen that small anymore?
Unless anyone has a better solution or objections as to why this is a bad idea, this is the way I'm going to do it.

Don't repaint instantly after setVisble in Qt

I'm using QPushButton in my mineSweeping game.
After changing from easy mode to hard mode, the number of QPushButton is supposed to change from 9x9 to 30x16.
So, I add QPushButton with the largest number(which is of hard mode) to GridLayout in constructor of MainWindow.
btnArr = new QPushButton[HARD_WIDTH * HARD_HEIGHT]; // member element
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
ui->mainGrid->addWidget(&btnArr[index], i, j, 1, 1,
Qt::AlignCenter);
++index;
}
}
Then if the user change mode(e.g.: easy mode to hard mode), resetBtn(HARD_WIDTH, HARD_HEIGHT); will be called.
void MainWindow::resetBtn(const int width, const int height)
{
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
if (j < width && i < height) {
btnArr[index].setVisible(true);
} else {
btnArr[index].setVisible(false);
}
++index;
}
}
}
The problem is that it seems the widget repaints each time setVisible is called. So in the hard mode case, it will be called 30x16 times, which caused strange effect like this:
So how can I set the widget not repaint during this loop?
Thanks in advance.
I think that you are trying to solve the wrong problem. You shouldn't be updating the widgets like this. If you necessarily want to, then hiding the parent widget of the layout before the change and showing it again afterwards should work.
A better approach is to use QStackedWidget and have all the boards prepared initially. Switching to a different board is then simply a matter of switching the active widget.
The total number of QPushButton is really big: 30x16 = 480!!! I don't use to make people change their programming logic, but in this case I think that using QPushButtons is not the better approach. The layout must have a really bad time trying to move the objects as they are added, and perhaps you are reaching some internal limit in refresh time for the layout to be repainted.
What I would have done is a custom widget with a custom paintEvent method. There you can divide its width and height in the number of columns and rows that you wish and paint the cells with pixmaps as the game is played.
For the mouse interaction, the best would have been to override the mousePressEvent with a custom logic that calculates the mouse position in the grid and calls the corresponding methods or emits signals indicating the position of the event. Not very hard to code. You can also use the event->buttons() method to know which mouse button was pressed and emit different signals if you wish.
I don't use to answer telling that it is better to change your whole program, but in this case I think you are going "the hard way". I know this is not the kind of answer you are looking for, but consider this possibility.
You could try calling setUpdatesEnabled(false) on the parent widget before doing those "massive" changes, and re-enable it once all is done.
Maybe I'm wrong but as far as I know Qt doesn't render the widget right after setVisible() is called. Rendering happens as a result of a 'render' event, except if you call render() manually.
From the official Qt doc (http://qt-project.org/doc/qt-4.8/qwidget.html#paintEvent):
Qt also tries to speed up painting by merging multiple paint events
into one. When update() is called several times or the window system
sends several paint events, Qt merges these events into one event with
a larger region (see QRegion::united()). The repaint() function does
not permit this optimization, so we suggest using update() whenever
possible.
My instincts tell me that it's not a painting problem rather a layouting (not enough space to present every button in 'hard mode').
Also I think you shouldn't use Qt::AlignCenter when you add your buttons to the layout, it will try to centerize every button in the layout. You should rather centerize the parent widget of the layout (if you don't have one create one and centerize it) and set size-policies correctly (QWidget setSizePolicy).
But as #Mat suggested if this really is a painting problem you can use setUpdatesEnabled(false/true) (if setUpdatesEnabled solves your problem please accept #Mat 's solution)
Try to enabling/disabling instead of visible/invisible:
void MainWindow::resetBtn(const int width, const int height)
{
int index = 0;
for (int i = 0; i < HARD_HEIGHT; i++)
for (int j = 0; j < HARD_WIDTH; j++)
btnArr[index++].setEnabled(j < width && i < height);
}

screenshot of a qt application from inside the application

I am trying to capture a screenshot of my application within the application. Its a Qt-based application. Is anyone aware of how to do this? Any suggestions are very welcome.
CV
You can tell any QWidget (including your QMainWindow) to render itself off-screen:
http://developer.qt.nokia.com/doc/qt-4.8/qwidget.html#render
Technically this is not a screenshot as it renders the widget explicitely for this purpose instead of capturing what is seen on-screen. For almost any purpose it doesn't matter.
If you have a GL widget, you can/must instead use grabFramebuffer() which has the advantage of capturing what is seen on the screen.
With this example you could get all your widget screen.
You could attach this method to any key press or signal, as you prefer, to get successive screenshot.
MyClass::screenshot()
{
QWidget *w = QApplication::activeWindow();
if(w) {
static int count = 0;
QPixmap p = QPixmap::grabWidget(w);
p.save(QString("/your/path/screenshot%1.png").arg(count));
count++;
}
}
QPixmap lets you do a window grab if you have the ID. My references are for PyQt but I'm sure you can make the adjustments:
How to get RGB values of QPixmap or QImage pixel - Qt, PyQt
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpixmap.html#grabWindow
In modern QT 5+ it can be done like that:
void MainWindow::takeScreenshot(const QString screenshotFileName)
{
if(isActiveWindow())
{
auto grabbedScreenshot = QWidget::grab();
grabbedScreenshot.save(screenshotFileName);
}
}

Forcing an Aspect Ratio when resizing a main window

I understand that in Qt4, there is no single flag you can set to have the mouse driven resizing of the window maintain a certain aspect ratio (say, 1:1). Is there a way to force a new size for the window while in "resizeEvent (QResizeEvent * event)"? the parameters of the event don't seem to support change or give the user a chance to inject a new size.
My goal: GUI/Mouse resizing of a certain window will maintain its current aspect ratio, regardless of where you resize it from (sides, top, bottom, corners).
Quoting a message in the thread referenced by #blueskin:
This is surprisingly difficult to do. Most of the window systems do not
allow the application to set constraints on the window other than the
max and min sizes and that it can be fixed size.
The most common way to attempt this is to call resize from within the resize event. This often causes recursion problems or looks strange because the widget resizes in ways the user has not requested.
Since most window systems do not allow the main window to be resized appropriately, the best solution is often to constrain a child widget rather than the parent. One such way to do this is with a class provided by libqxt.
libqxt has support for keeping a child widget at a certain aspect ratio through its QxtLetterBoxWidget. #blueskin's answer provides one good attempt at doing what was originally requested.
If you're interested I'd recommend you read the source for the resizeWidget() function and observe the places in which it gets called. As background, libqxt uses the pimpl idiom and qxt_d() gets at the private member.
Duplicate Question?
How to maintain widgets aspect ratio in Qt?
Citing the answer by 'df' in the above link
Calling resize() from within resizeEvent() has never worked well for
me -- at best it will cause flickering as the window is resized twice
(as you have), at worst an infinite loop.
I think the "correct" way to maintain a fixed aspect ratio is to
create a custom layout. You'll have to override just two methods,
QLayoutItem::hasHeightForWidth() and QLayoutItem::heightForWidth().
Also checkout Sam Dutton's solution # http://lists.trolltech.com/qt-interest/2007-01/msg00204.html
void MyWindow::resizeEvent(QResizeEvent * /*resizeEvent*/)
{
int containerWidth = _myContainerWidget->width();
int containerHeight = _myContainerWidget->height();
int contentsHeight = containerHeight ;
int contentsWidth = containerHeight * _aspectRatio;
if (contentsWidth > containerWidth ) {
contentsWidth = containerWidth ;
contentsHeight = containerWidth / _aspectRatio;
}
resizeContents(contentsWidth, contentsHeight);
}
Another quick and simpler way to do it:
http://developer.qt.nokia.com/forums/viewthread/5320/#31866
#include <QWidget>
#include <QApplication>
#include <QSizePolicy>
class MyWidget:public QWidget
{
public:
MyWidget():QWidget(){};
~MyWidget(){};
virtual int heightForWidth ( int w ) const { return w*9/16;};
};
int main (int argv, char** argc)
{
QApplication a(argv,argc);
MyWidget w;
QSizePolicy qsp(QSizePolicy::Preferred,QSizePolicy::Preferred);
qsp.setHeightForWidth(true);
w.setSizePolicy(qsp);
w.show();
a.exec();
}

Resources