PDA

View Full Version : QMenu is not closing after popup



rittchat
17th January 2011, 06:32
Hi,

I am using Qt-4.7.1 on Windows XP SP3. I wrote a program which pops up a menu when the mouse touches the top of the screen. I am using the following code



void RDialog::slotShowMenuGlobally()
{
if(slotAllMenuHiden())
{
trayMenu->popup(QCursor::pos());
trayMenu->activateWindow();
}
}


If I click the mouse outside the menu immediately after opening it, the menu closes as desired. But, if I allow some time in between then menu is not closing even if I click the mouse outside the menu.

Please help me to sort this problem out.

thanks

rittchat
14th February 2011, 05:41
Someone please help.

vudvpro
17th March 2011, 21:43
You should post the full code than I can understand and help you.
Here's an example you may wish to apply.

- fileEx.h


#include <QSystemTrayIcon>

protected:
void createActions();
void createTrayIcon();
void setIcon();
virtual void closeEvent(QCloseEvent *event);
QAction *open;
QAction *close;
QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;

protected slots:
void trayIconClicked(QSystemTrayIcon::ActivationReason) ;



- fileEx.cpp


fileEx::fileEx(QWidget *parent) : Gadget(parent)
{
createActions();
createTrayIcon();
setIcon();
trayIcon->show();
}
void fileEx::createActions()
{
open = new QAction(tr("&Open"), this);
connect(open, SIGNAL(triggered()), this, SLOT(show()));

close = new QAction(tr("&Quit"), this);
connect(close, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void fileEx::createTrayIcon()
{
trayIconMenu = new QMenu();

trayIconMenu->addAction(open);
trayIconMenu->addSeparator();
trayIconMenu->addAction(close);

trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip("PIM Task");

connect(
trayIcon,
SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this,
SLOT(trayIconClicked(QSystemTrayIcon::ActivationRe ason))
);
}

void fileEx::setIcon()
{
trayIcon->setIcon(QIcon(":/AppIcon.png"));
}

void fileEx::trayIconClicked(QSystemTrayIcon::Activatio nReason reason)
{
if(reason == QSystemTrayIcon::DoubleClick)
this->show();
}

void fileEx::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
settings.setPosition(pos());
settings.setSize(size());
trayIcon->showMessage(tr("PIM Task"),
tr("This application is still running. To quit please click this icon and select Quit"));
hide();

event->ignore(); // Don't let the event propagate to the base class
}
}

rittchat
22nd March 2011, 09:36
I create a QTimer pointer hpCheck in the header file.



RDialog::RDialog()
{
hpCheck = new QTimer(this);
QObject::connect(hpCheck,SIGNAL(timeout()),this,SL OT(slotHotspotCheck()));
QObject::connect(this,SIGNAL(signalShowHPMainMenu( )),this,SLOT(slotShowMenuGlobally()));
hpCheck->start(1000);
}

void RDialog::slotHotspotCheck()
{
QDesktopWidget dstpWidget;
QPoint curPos = QCursor::pos();
QRect scrRect = dstpWidget.screenGeometry(curPos);

if((curPos.x()>=scrRect.left()) && (curPos.x()<=scrRect.right()) && (curPos.y()==scrRect.top()))
emit signalShowHPMainMenu();
}

void RDialog::slotShowMenuGlobally()
{
if(trayMenu->isHidden())
{
trayMenu->popup(QCursor::pos());
trayMenu->activateWindow();
}
}


Suppose I double click in an empty space of a desktop and then if I move the mouse to the top of the screen the menu is appearing. The problem is, if I click outside the menu, the menu is not closing. It is staying on top of all the other window. The same problem is happening both on windows as well as on Linux. On windows I can bypass the problem by calling trayMenu->grabMouse() after opening the menu. And trayMenu->releaseMouse() after closing the menu. But in Linux trayMenu->grabMouse() is not working.

How can I fixed this? Please help.