PDA

View Full Version : QListWidget refreshing issue



ouekah
7th July 2010, 11:04
Hi,

I have QListWidget within a QGroupBox and I would like the group box title to display the number of items of QListWidget. So I wrote the following the function:



void Test::add(const QString& item) {
listWidget->addItem(item); // adds item to QListWidget
groupBox->setTitle("Currently (" + QString::number(listWidget->count()) + ")"); // updates QGroupBox title
}


Unfortunately when add is called, no modification appears on the QListWidget even though the title is updated immediately. listWidget is refreshed when one clicks on it... This is annoying since I need a real-time update.

I tried to perform a workaround by calling one of the functions below right after setTitle:

listWidget->update();

QCoreApplication::processEvents();

QCoreApplication::flush ()
but all my attempts failed.

What could I do next ?

wladek
8th July 2010, 08:47
Hi ouekah,

Find attached to this reply a small project that I created to test your scneraio. In my case, it works. Maybe it has to to with the fact the I've been using Qt Designer mode to create the ui.

Anyway, maybe this project will help you identifying your problem.

Regards,
Wladek

high_flyer
8th July 2010, 08:55
This should be done automatically.
My guess is that you have overridden the Test::paintEvent(), or somewhere else in the widget hierarchy.
Try this:


void Test::add(const QString& item) {
listWidget->addItem(item); // adds item to QListWidget
groupBox->setTitle("Currently (" + QString::number(listWidget->count()) + ")"); // updates QGroupBox title
update();
}

ouekah
8th July 2010, 12:22
@wladek: thanks a lot for your example ! Unfortunately I have the same problem with your program... :(

When you add the items to the listWidget, the count is updated immediately but not the listWidget's content.. I need to click on the listWidget to actually see the updated content.

I start to wonder if the problem lies with something else like my operating system or the way I compile the project... This is really weird...

I'm using OS X 10.6.3 and I compiled your example in two steps:

1) qmake -spec macx-g++
2) make

I have attached four screenshots, so you can see by your self what happens...

@high_flyer: I tried the update(), but doesn't work... :(

wladek
8th July 2010, 12:27
I have some MACs around here in the office. Let me just test this example on them and let you know the result.
This is becoming very interesting (and I am sure annoying for you).

Wladek.

wladek
8th July 2010, 12:35
My testing on MAC OS X version 10.5.8 (Snow) gave the same results like yours.
This seems to be a Qt issue. Could this be a possible bug in the framework?

I don't know what to suggest for making this work.
If you find something, please let me also know.

Regards,
Wladek

ouekah
8th July 2010, 13:31
I tried almost everything I guess... I will just the remove the "count" functionality (it works fine if the program does not update the groupBox title all the time...).

That's a shame. Is there any way to forward this issue to Qt ?

Thanks again for your support !

wladek
8th July 2010, 13:49
Hey ouekah,

Good news! I believe i have found a hack for solving this.:)
I just had an idea and tried it out on my Mac. What I did was the following:
1. added a private slot in my header

private slots:
void buttonWasPushed();
void updateGroupBoxTitle();


2. In the already defined add function I do not call directly groupBox->setTitle(), but instead I use the QTimer:singleShot() static function:


void Form::add(const QString& item)
{
ui->listWidget->addItem(item);
QTimer::singleShot(0, this, SLOT(updateGroupBoxTitle()));
}.

Then, in the implementation of the updateGroupBoxTitle() slot i just set the title of the groupbox:

void Form::updateGroupBoxTitle()
{
ui->groupBox->setTitle("Currently (" + QString::number(ui->listWidget->count()) + ")"); // updates QGroupBox title
}

This works for me. I hope it will be functional for you also.
If it does work, do not ask me why :).

Waiting for your feedback...
Wladek

ouekah
8th July 2010, 18:03
Wow ! what a trick ! It works perfectly !

Thank you so much for your help !

And don't worry... I won't ask :)