PDA

View Full Version : My showevent does not work properly.



tonnot
16th February 2011, 17:42
void Ui_main::showEvent(QShowEvent * event )
{
qDebug()<<event->type();
}
I only see the value 17 before the window be displayed.
I want to catch it just AFTER the window is displayed.
Have I to use another event handling?

And, is there an event (for example) 'ready' to be sure that my window is ready for first time ? (So, I can avoid to control the showevent trigger when a window minimized is maxified)

Any help ? Thanks

ChrisW67
16th February 2011, 22:14
Please put your code snippets in [code] tags.

It's hardly surprising that you receive a QEvent of type QEvent::Show in the handler for events of that type.

For my money, the window is "ready" after you construct it and visible after you show() it. Another definition of "ready" might mean "returned to the event loop after the show()". Look at using a zero duration single-shot timer from the window constructor to trigger a slot immediately after the event loop is reached.

If you read the QShowEvent docs you will see there are two types of event: spontaneous and not. User triggered events will be spontaneous, the programmatic show() of the window is not (at least on my machine).

tonnot
17th February 2011, 07:12
Excuse me for the code tags missing.
I understand the QT events, but believe me I dont know how to be sure that the window is already showed.
I want to show the main window and before doing more things I want to check some issues.
If something is wrong I want to show a messagebox showing the information to the user and close the window.
Now, because the event is launched before the main window is show, my user would see first a messagebox .
How can I to fix the problem.?
Thanks

franz
17th February 2011, 07:42
Try this:

void MyMainWindow::showEvent(QShowEvent *e)
{
QTimer::singleShot(20, this, SLOT(showSomeDialogUponSomeAction()));
}

void MyMainWindow::showSomeDialogUponSomeAction()
{
...
}

tonnot
17th February 2011, 08:17
It works!
So, the trick is to wait some seconds or calling the slot ?
(The 'slot call' means a whole process or pending events?)

And, also can be a solution to process all events instead of using the singleshot?
I have try "QCoreApplication::processEvents" but it does not work
(I see the messagebox before the window)

Thank you very much.

ChrisW67
17th February 2011, 22:06
Or this, as mentioned in my response:


void MyMainWindow::MyMainWindow(...)
{
...
QTimer::singleShot(0, this, SLOT(showSomeStuffLater()));
}

void MyMainWindow::showSomeStuffLater()
{
...
}

// then

...
MyMainWindow *w = new MyMainWindow(...);
w->show();
...

The slot will be called as soon as the program returns to the event loop after executing show() (and whatever follows it).

tonnot
18th February 2011, 06:54
Yes... it can be another solution.
But it implies to have a Qtimer and a slot.
I remember 'doEvents' from old VB, Is there somehing like in QT ?
Thank you

ChrisW67
18th February 2011, 07:18
Yes, you've already tried it and declared it does not solve your problem.

Why are you still looking for a solution?