Results 1 to 4 of 4

Thread: QMenu is not closing after popup

  1. #1
    Join Date
    Dec 2009
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QMenu is not closing after popup

    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

    Qt Code:
    1. void RDialog::slotShowMenuGlobally()
    2. {
    3. if(slotAllMenuHiden())
    4. {
    5. trayMenu->popup(QCursor::pos());
    6. trayMenu->activateWindow();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Dec 2009
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMenu is not closing after popup

    Someone please help.

  3. #3
    Join Date
    Mar 2011
    Location
    Việt Nam
    Posts
    8
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Cool Re: QMenu is not closing after popup

    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::ActivationReason))
    );
    }

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

    void fileEx::trayIconClicked(QSystemTrayIcon::ActivationReason 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
    }
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMenu is not closing after popup

    I create a QTimer pointer hpCheck in the header file.

    Qt Code:
    1. RDialog::RDialog()
    2. {
    3. hpCheck = new QTimer(this);
    4. QObject::connect(hpCheck,SIGNAL(timeout()),this,SLOT(slotHotspotCheck()));
    5. QObject::connect(this,SIGNAL(signalShowHPMainMenu()),this,SLOT(slotShowMenuGlobally()));
    6. hpCheck->start(1000);
    7. }
    8.  
    9. void RDialog::slotHotspotCheck()
    10. {
    11. QDesktopWidget dstpWidget;
    12. QPoint curPos = QCursor::pos();
    13. QRect scrRect = dstpWidget.screenGeometry(curPos);
    14.  
    15. if((curPos.x()>=scrRect.left()) && (curPos.x()<=scrRect.right()) && (curPos.y()==scrRect.top()))
    16. emit signalShowHPMainMenu();
    17. }
    18.  
    19. void RDialog::slotShowMenuGlobally()
    20. {
    21. if(trayMenu->isHidden())
    22. {
    23. trayMenu->popup(QCursor::pos());
    24. trayMenu->activateWindow();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. [Qt] QMenu problem with method popup.
    By Xandareva in forum Newbie
    Replies: 1
    Last Post: 24th July 2010, 12:29
  2. Replies: 0
    Last Post: 27th April 2010, 15:15
  3. Popup Menu not closing after clicking custom widget
    By stefanadelbert in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 05:38
  4. QMenu popup: how close when clicked outside
    By powermax in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2009, 03:18
  5. how to popup and close a QMenu
    By Placido Currò in forum Qt Programming
    Replies: 15
    Last Post: 14th May 2007, 16:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.