PDA

View Full Version : Problem hiding main window on minimize



bpetty
30th August 2007, 20:46
I have read some previous posts on this, and some other articles on the subject... but am still having a problem. I am using 4.2.2 and don't know if 4.3.1 would fix it.

Essentially I have a window that derives from QMainWindow and is using a QSystemTrayIcon component. I am over-riding eventClose(...) to hide my window and show my systray icon. Double clicking the icon raise()es and activateWindows()s my main window. The problem is on minimize...

I tried over-riding event(...) and changeEvent(...). I hide my windows, show my icon and boom... something retarded happens. My windows is inactivated and sitting on my task bar. Overridding event(..) and calling hide() seems to work when I walk through it, but when it returns it adds my app back on to the task bar. I have tried to ignore the event and everything. Sounds like a bug in 4.2.2 to me, but I wanted to see what you guys think.

Here is some example code:



// tried a similar approach with event(QEvent*).. ignoring the event, etc.
void CINQtMainWindow::changeEvent(QEvent * e )
{
if (e->type() == QEvent::WindowStateChange)
{
if (isMinimized() == true)
{
hide();
m_oSysTray.ShowIcon();

return;
}
}

QMainWindow::changeEvent( e );
}




void CINQtMainWindow::slotSysTrayDblClk()
{
m_oSysTray.HideIcon();

raise();
activateWindow();

show();
}


When slotSysTrayDblClk() gets called after minimizing... my app does not get raised, but sits in the task bar. When the app is minimized, you can click it on the task bar and it will show an inactivated window (essentially a black box).

4.2.2 bug? Fixed in 4.3.x?

marcel
31st August 2007, 05:01
But this worked in 4.2.2 also, as far as I remember...
Have you tried this:


void Window::setVisible(bool visible)
{
minimizeAction->setEnabled(visible);
maximizeAction->setEnabled(!isMaximized());
restoreAction->setEnabled(isMaximized() || !visible);
QWidget::setVisible(visible);
}

void Window::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
hide();
event->ignore();
}
}

It is from the examples( of 4.3.0)
Should do the same for a state change event.

bpetty
31st August 2007, 14:51
My close event works perfectly. There is something about minimize that appears to force the application to appear on the task bar. I will see if something in your setVisible() method will get it to work correctly, but I have my doubts.

marcel
31st August 2007, 15:33
Does isMinimized() returns true in your change event?
I am not sure if the window is yet minimized when it gets this event. It shouldn't be, since you have the option to ignore it.

regards

bpetty
31st August 2007, 16:00
event() seems to get it before it minimizes, changeEvent() seems to get it after it minimizes. Either way, ignoring the event still makes it appear in the task bar after returning from those methods. That is why I think it is a bug... I just wanted to see if you guys had some obvious magical answer before I bothered the trolls.

bpetty
18th September 2007, 17:41
I contacted Trolltech and they gave me this gem:


void CINQtMainWindow::changeEvent(QEvent * e )
{
if (e->type() == QEvent::WindowStateChange)
{
if (isMinimized() == true)
{
QTimer::singleShot(0, this, SLOT(hide()));
m_oSysTray.ShowIcon();
e->ignore();

return;
}
}

QMainWindow::changeEvent( e );
}

QTimer::singleShot(0, this, SLOT(hide())); instead of hide()