Qt in qmenu The realization of three or four thousand lines ...

There was a special need , It requires a few pixels of shadow around the menu , I've been thinking about it for a long time , use QMenu Can't do it , Just write one in the window

However, how to make the menu disappear is very troublesome

1. Click on the menu item

2. Click elsewhere

3. Blocked by other windows

4. Press the button

Then he wrote with the method of judging the focus , The method is similar to pop up an auto close window .

But the menu can be multi-level , This creates a very disgusting focus transfer . Although it was finally dealt with ( The code is disgusting - -!, I feel like vomiting )

It's a pity that the sky doesn't fulfill people's wishes , But when other windows grab the focus together , My menu became a miracle . I can't think of a solution

Finally, I'll see Qt The source code of .

After watching the , Be suddenly enlightened , Think about how you used to be 2 Well . It is 2 Got home .

The key is a few lines of code :

QAction *QMenu::exec(const QPoint &p, QAction *action)
{
............
QEventLoop eventLoop;
d->eventLoop = &eventLoop;
................ (void) eventLoop.exec();
...................
}

QMenu Inherited from QWiget, And set up Qt::Popup type

Then handle the shutdown in the following events .

mousePressEvent

mouseReleaseEvent

keyPressEvent

The reason lies in QEventLoop::exec After you expand the message loop , Direct interception of all events , In this way, users can capture any click on the desktop , And it's the same with keystroke messages .

About eventloop nesting , You can read this article ,  Random talk Qt Event loop nesting

Qt in QMenu More related articles on how to handle the menu close

  1. [ turn ]Qt Two methods used in timer

    Qt There are two ways to use timers in , One is to use QObject Class , Another is to use QTimer class . Its accuracy generally depends on the operating system and hardware , But the general support is 20ms. Here are two ways to use the timer . Method ...

  2. Qt Chinese characters The solution once and for all

    QT Chinese character problem in Chinese , Is there a once and for all solution ? At present, we have the following problems 1. When there is Chinese in the string , Compile the prompt " Constant contains line breaks " 2. It can't be displayed normally when Chinese is output in the console window , All Chinese characters are displayed as ? Objective ...

  3. QT How to use several functions in

    One . Convert a string to an integer demo1:QString str = "FF";bool ok;int hex = str.toInt(&ok, 16); // hex == 25 ...

  4. 【 original 】 control perl and python Whether the script file is closed during script execution

    Introduction track perl and python Script access to files , In the actual process ,perl and python After the parser parses the script , It's closed directly Script files , The name of the script file that is used to access the file cannot be found in the process . shell.perl and pyt ...

  5. Qt Use in QSqlDatabase::removeDatabase() The right way good

    If you used Qt Of QSqlDatabase Words , Most of you will be interested in the following warning message : QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_defau ...

  6. Qt Middle yield Qwidget The way to set the top

    Generally speaking, the top of the form and cancel as long as         setWindowFlags(Qt::WindowStaysOnTopHint);        setWindowFlags(Qt::Widget); want ...

  7. analyse QMenu & Qt Completely customized menu

    Paste an effect picture :  Customization includes : 1. Shadow around 2. Elements of menu items ( Separator , control ICON size , Text display position and color , Submenu indicator ) The effect in the menu , Part of it can be used stylesheet Realization , But to do that ...

  8. qt The menu in QMenu QAction

    Qt To create a menu , There are three classes that are important : QMenuBar(QWidget * parent = 0) QMenu(QWidget * parent = 0) QMenu(const QString &am ...

  9. How the Word In looking for MathType menu

    Some user friends are using word In the process of , Find yourself suddenly unable to find MathType Formula editor menu item , At this time, we are in a hurry to write the formula , So I will be very anxious . Now let's make a good analysis of this problem , And provide solutions . Please turn it off ...

Random recommendation

  1. Model1 Mode of student information addition, deletion, modification and search

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  2. Linux Under the installation and setup memcache( turn )

    memcache It's high performance , Distributed memory object caching system , Used to reduce database load in dynamic applications , Improve access speed . It is said that the official said , Its users include twitter.digg.flickr etc. , They are all Internet Celebrities . Currently used memca ...

  3. from MVC To the front and back end

    Abstract :MVC The model dates back to the last century 70 The age was born , It still exists today , It can be seen that the vitality is quite strong .MVC Patterns were first used in Smalltalk In language , In the end, it has been well used in many other development languages , for example ,Java Medium Stru ...

  4. ulimit Turn on coredump Core dump

    [root@localhost ~]# ulimit -c [root@localhost ~]# ulimit -a core data seg size (kbytes, -d) unlimite ...

  5. HTML5 File loading progress management

    /** * File loading progress management */ DownloadUtils = function(options){ options = options || {}; this.init(options); }; ...

  6. How to activate Microsoft Office 2010?

    Microsoft Office After the installation is complete, it is usually inactive , How to activate this software ? Experience in baidu :jingyan.baidu.com Tools / raw material Office 2010 Toolkit Software Experience in baidu :ji ...

  7. Browser virtual process IP plug-in unit

    Browser virtual process This is a plug-in calling API When combined , For example, call a large number of IP To click on http://www.opew.cn and http://www.gtev.cn function QMPlugin.gbip() ...

  8. test RemObjects Pascal Script

    unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  9. Terminal shortcuts

    <backspace> Delete <ctrl+l> Clear the screen , amount to clear <ctrl+e> The cursor jumps to the end of the command <ctrl+a> The cursor jumps to the beginning of the command & ...

  10. [BZOJ4868][ The joint examination of six provinces 2017] final exam ( Three points )

    4868: [Shoi2017] final exam Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 964  Solved: 439[Submit][Status ...