ShowEvent not working
-
I have a very simple and strait forward showEvent(QShowEvent * event) method in a QDialog class that doenst seem to be called when calling exec(). Wonder if this is bug, or something wrong in my code (althoug my code It works on symbian and on desktop) ?
@protected:
void showEvent(QShowEvent * event);
........void SystemEq2_dlg::showEvent(QShowEvent * event)
{
ui->listWidget_solutions->clear();
ui->listWidget_solutions->addItems(this->solutions);
}
.....
dlg2.exec();@I've at least other class where showEvent is not being called
-
After all, showEvent() is working, but I dont know why it's not updating the listwidget. I created a pushbutton that does.
@void SystemEq2_dlg::on_pushButton_solution_clicked()
{
ui->listWidget_solutions->clear();
ui->listWidget_solutions->addItems(this->solutions);
}@It's not a pretty solution because forces the user to an extra button clik, but for now it will have to do untill I figure why showEvent is failling to update the listwidget
-
If your list widget did not get updated from showEvent, the chance is pretty good that there is a race-condition.
I was thinking of a possible solution to schedule your code to be done very soon after the show event.
Maybe you want to try using a QTimer::singleshot() to populate it 1ms later.
You would call a new slot you create on your SystemEq2_dlg class and that slot would have the code you now put in your showEvent() method. -
Using a singleslot() does the trick
@void SystemEq2_dlg::showEvent(QShowEvent * event)
{
QTimer::singleShot(0, this, SLOT(update()));
}void SystemEq2_dlg::update()
{
ui->listWidget_solutions->clear();
ui->listWidget_solutions->addItems(this->solutions);
}@I'm not sure what is a race condition, but I not using any threads, if its related to that. I also tried
@void SystemEq2_dlg::update()
{
update();
//or
emit update();
}@but din't work. I also tried declaring showevent() has a signal and connect it with update but got an error, I guess I cant declare showevent has a signal.
Overall is not a very nice solution but it works. -
With 'race condition' I meant that there is an ordering issue. You are right I could have chosen better words :)
What most likely happens is that the showEvent is called just before the widgets inside of it are shown. So when you call your update the widget it is done on ignores it since its still not visible.
The call to showEvent is just one small part of a lot of stuff that goes on in the single 'show' event.
Using the singleshot with zero just creates a new event on the event queue to be executed after the show event has completed. -
Just to add more information on this subject, I notice other widgets, like labels, are updated on the showevent(). Not sure if this behavior could be considered a bug, I always tought that showevent() was suposed to be used in this kind of situation of updating widgets. Anyway it's "fixed" with singleshot()