Right-click menu

Source: Internet
Author: User

 

 

I have been diving. Now I am posting something to help you. Http://blog.chinaunix.net/u3/103355/showart_2101288.html

Author: wangxinus, <wangxinus@gmail.com>
Source: http://wangxinus.cublog.cn
Note: you are welcome to repost the original article. Please email the exchange to the author.

For the implementation of the right-click menu, you can refer to another article I have reproduced and add the right-click menu in QT.

Q: How to right-click a list. If the item is displayed, the "modify" option is available. In other blank spaces, only the "add" and "delete" options are available.

Right-click the menu, send the listwidget from the qlistwidget, and rewrite it.
Void qwidget: contextmenuevent (qcontextmenuevent * event) [Virtual protected]
This event is called when you right-click the listwidget.
Void listwidget: contextmenuevent (qcontextmenuevent * event)
{
Qmenu * popmenu = new qmenu (this );
Popmenu-> addaction (New qaction ("add", this ));
Popmenu-> addaction (New qaction ("delete", this ));
Popmenu-> addaction (New qaction ("modify", this ));

Popmenu-> exec (qcursor: pos (); // the position where the menu appears is the position of the current mouse
}

When you use listwidget in a program and right-click the mouse, the menu in the code above will appear, but the same option will appear no matter where you right-click it. Obviously, the "modify" option should not be displayed on the right-click menu in the blank space. Otherwise, the modified option is ???

The key to the problem is to determine whether the right-click position is an item when the right-click menu is called. The implementation code should be as follows:
Void listwidget: contextmenuevent (qcontextmenuevent * event)
{
Qmenu * popmenu = new qmenu (this );
Popmenu-> addaction (New qaction ("add", this ));
Popmenu-> addaction (New qaction ("delete", this ));
If (currentmouseposhasaitem ())
{
Popmenu-> addaction (New qaction ("modify", this ));
}

Popmenu-> exec (qcursor: pos (); // the position where the menu appears is the position of the current mouse
}
How can we determine whether the right-click is on an item? Cute QT is easy to implement.

Qlistwidgetitem * qlistwidget: itemat (const qpoint & P) const
Returns a pointer to the item at the coordinates p.

Qlistwidgetitem * qlistwidget: itemat (int x, int y) const
This is an overloaded member function, provided for convenience.
Returns a pointer to the item at the coordinates (x, y ).

The two overload functions above are used to obtain the item by using the coordinate position and return NULL, so there is no item.
Void listwidget: contextmenuevent (qcontextmenuevent * event)
{
Qmenu * popmenu = new qmenu (this );
Popmenu-> addaction (New qaction ("add", this ));
Popmenu-> addaction (New qaction ("delete", this ));
If (this-> itemat (qcursor: pos ())! = NULL) // if there is an item, add the "modify" menu [1] *
{
Popmenu-> addaction (New qaction ("modify", this ));
}

Popmenu-> exec (qcursor: pos (); // the position where the menu appears is the position of the current mouse
}

Write the above code, okay? Or not? Well, neither can I do it here. Because the coordinates accepted in itemat () are in the listwidget coordinate system. The coordinates obtained through qcursor: pos () are global coordinates. It must be mapped to the listwidget, which is described in QT assist.
Qpoint qcursor: pos () [Static]
Returns the position of the cursor (hot spot) in global screen coordinates.
You can call qwidget: mapfromglobal () to translate it to widget coordinates.
See also setpos (), qwidget: mapfromglobal (), and qwidget: maptoglobal ().

So the final code is:
Void listwidget: contextmenuevent (qcontextmenuevent * event)
{
Qmenu * popmenu = new qmenu (this );
Popmenu-> addaction (New qaction ("add", this ));
Popmenu-> addaction (New qaction ("delete", this ));
If (this-> itemat (mapfromglobal (qcursor: pos ()))! = NULL) // if there is an item, add the "modify" menu [1] *
{
Popmenu-> addaction (New qaction ("modify", this ));
}

Popmenu-> exec (qcursor: pos (); // the position where the menu appears is the position of the current mouse
}

OK. Remember to always connect qaction to the slot for processing in your code. The above Code menu is not functional

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nowgoant/archive/2011/05/06/6398993.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.