Qt's right key menu implementation __QT

Source: Internet
Author: User

A recent example of the C + + GUI Qt4 section provides a context menu for extending an application.

The pop-up menu (ContextMenu) of the parts in Qt, depending on the value of Contextmenupolicy, has four forms:
One, default menu
At this point, the value of Contextmenupolicy is qt::D Efaultcontextmenu, which is the default value that displays the default menu for the part definition
Second, no menu
At this point, the value of the Contextmenupolicy is Qt::nocontextmenu
Three, the action defined menu
At this point, the value of the Contextmenupolicy is Qt::actionscontextmenu, and to define this menu for this part, it's easy to just insert the defined action part into the part where you want to display the menu, and the part will automatically display the menu sequentially.
Qwidget::addaction (Qaction *action);
Four, custom menu
At this point, the value of Contextmenupolicy is Qt::customcontextmenu, this time, there are two ways to define the menu, one is the response
Customcontextmenurequested () This signal displays the menu (the Exec () method of the Qmenu) in the slot of the response. The second is to derive a class from the class of this part and rewrite the contextmenuevent () function to display the menu (the Qmenu exec () method displays).


The third kind is simple:

void Mainwidget::createcontextmenu ()
{
This->addaction (m_exitaction);
This->setcontextmenupolicy (Qt::actionscontextmenu);
}

The fourth type of Qt::customcontextmenu is described below:

#ifndef Widget_h
#define Widget_h

#include <QtGui>

Class Widget:public Qwidget
{
Q_object

Public
Widget (Qwidget *parent = 0);
~widget ();
Private
qmenu* M_menu;
qaction* m_exitaction;
qaction* m_aboutaction;

Private
void CreateAction ();
void CreateMenu ();
void Createcontextmenu ();
Private Slots:
void Contextmenuslot (Qpoint p);
};

#endif//Widget_h

#include "Widget.h"
#include <QDebug>

Widget::widget (Qwidget *parent)
: Qwidget (parent)
{
CreateAction ();
CreateMenu ();
Createcontextmenu ();
Connect (this,signal (customcontextmenurequested (Qpoint)), This,slot (Contextmenuslot (qpoint));
}

Widget::~widget ()
{

}
void Widget::createaction ()
{
M_exitaction = new Qaction ("Exit", this);
Connect (m_exitaction,signal (triggered ()), Qapp,slot (Quit ()));

M_aboutaction = new Qaction ("about", this);
Connect (m_aboutaction,signal (triggered ()), Qapp,slot (ABOUTQT ()));
}
void Widget::createmenu ()
{
M_menu = new Qmenu (this);
M_menu->addaction (m_exitaction);
M_menu->addseparator ();
M_menu->addaction (m_aboutaction);
}
void Widget::createcontextmenu ()
{

This->setcontextmenupolicy (Qt::customcontextmenu);//The Contextmenupolicy property is set to: Customcontextmenu, this step cannot be forgotten, Otherwise the right key does not respond.
}
void Widget::contextmenuslot (Qpoint p)
{
M_menu->exec (This->maptoglobal (p));
}


take Qtreewidget as an example:

Under qt+vs2010, use the Slot function customcontextmenurequested (Qpointpos) to implement the Treewidget right-click menu bar.

1, to add a right menu bar in a form, such as in Qtreewidget, you can use the Slot function customcontextmenurequested (qpointpos).

You can implement the specific menu bar in the new customcontextmenurequested (Qpointpos) function.

In order to determine the right click on the location, that is, the point is different item node, there will be different right-click menu, we can use the API Itemat () function implementation.

In order to give each node a different key value, it can be achieved by SetData ().

In the property setting, set the Contextmenupolicy property to: Customcontextmenu, this step can not be forgotten, otherwise the right key does not respond.

I added the implementation code for the right-click menu in Treewidget:

2. Add nodes for Treewidget and assign different key values to each node by SetData ().

Qtreewidgetitem *root;

root = new Qtreewidgetitem (Ui->treewidget, Qstringlist (QString ("Connection"));

Qvariant var0 (0);

Root->setdata (0,QT::USERROLE,VAR0);

3. Use the Itemat () function in the slot function to get the currently clicked node and then add a different menu for the different nodes.

void mainwindow::on_treewidget_customcontextmenurequested (Qpoint pos)

{

qtreewidgetitem* Curitem=ui->treewidget->itemat (POS); Get the node that is currently clicked

if (curitem==null) return; This situation is where the right key is not in the range of the TreeItem, that is, right click in the blank position

qvariant var = curitem->data (0,qt::userrole);

if (0 = = var)//data (...) The returned data has been set by SetData () before the node was established.

Qmenu *popmenu =new Qmenu (this);//define a right-click pop-up menu

Popmenu->addaction (UI->ACTION_NEWDB)//Add qaction to the menu The action is defined earlier with the designer

Popmenu->addaction (UI->ACTION_OPENDB);

Popmenu->addaction (UI->ACTION_DELDB);

Popmenu->exec (qcursor::p OS ());//Pop-up right-click menu, menu position is cursor position}

Else

{

Qmenu *popmenu =new Qmenu (this);//define a right-click pop-up menu

Popmenu->addaction (ui->action_newtable)//Add qaction to the menu The action is defined earlier with the designer

Popmenu->addaction (ui->action_opentable);

Popmenu->addaction (ui->action_designtable);

Popmenu->exec (qcursor::p OS ());//Pop-up right-click menu, menu position is cursor position

}

}

//////////////////////////////////////////////////////////////////////////

But more advanced is the redefinition of the event-handling function void Contextmenuevent (Qcontextmenuevent *event). The following explains its contextmenuevent (Qcontextmenuevent *event).

Qwidget and its subclasses can have a right-click menu because Qwidget has the following two functions related to the right-click menu:

Qt::contextmenupolicy Contextmenupolicy () const

void Setcontextmenupolicy (Qt::contextmenupolicy policy)

Qt::contextmenupolicy enumeration types include: qt::D efaultcontextmenu, Qt::nocontextmenu, QT::P Reventcontextmenu, QT:: Actionscontextmenu, and Qt::customcontextmenu.

The use of the following methods:

1) The default is QT::D efaultcontextmenu.
It is handled using the right-click menu Event Contextmenuevent () (which means the contextmenuevent () handler is called). is to rewrite the contextmenuevent (qcontextmenuevent * event) function.

Example (the example that I rewrote)

[CPP]

void Mainwindow::contextmenuevent (Qcontextmenuevent *event)

{

context = new Qmenu ();

Context->addaction (Ui->actioncut);

Context->addaction (Ui->actioncope);

Context->addaction (ui->actionpase);

Spreadsheet->setcontextmenupolicy (Qt::D efaultcontextmenu);

Context->exec (qcursor::p OS ());

}

2) using Qt::customcontextmenu.
It is to send a qwidget::customcontextmenurequested signal, note that just signal, meaning to write the right button to show the menu slot. This signal is the only qwidget of the right key menu (also its own unique signal), but also easily overlooked signal:

void customcontextmenurequested (const qpoint & POS)

The signal is issued under the condition that the user requests ContextMenu (the general is the right mouse click) and the same time the contextmenupolicy of the hit is Qt::customcontextmenu.
Note: POS is the location where the widget receives the right-click menu event, typically in the coordinate system of the part. But for the exception of Qabstratscrollarea and its subclasses, it is the coordinate system corresponding to its viewport viewport (). such as the commonly used Qtableview, Qheaderview is the Qabstratscrollarea subclass.
Because only sends the signal, therefore needs to write the right key menu slot to respond.

For example, a table (Qtableview type) table header Display right button menu slot:

Datatable->horizontalheader ()->setcontextmenupolicy (Qt::customcontextmenu);
Connect (Datatable->horizontalheader (), SIGNAL (customcontextmenurequested (const qpoint&)),
This, SLOT (Show_contextmenu (const qpoint&));//this is the window where the DataTable resides
Qmenu *cmenu = NULL;
Show_contextmenu (const qpoint& POS)
{
if (CMenu)//ensures that only one menu exists at the same time, releasing memory in time
{
Delete CMenu;
CMenu = NULL;
}
Qmenu CMenu = new Qmenu (Datatable->horizontalheader ());

Qaction *ascendsortaction = cmenu->addaction ("ascending");
Qaction *descendsortaction = cmenu->addaction ("descending");
Qaction *filteraction = cmenu->addaction ("filtration");
Qaction *reshowaction = Cmenu->addaction ("overload");

Connect (ascendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_ascend ()));
Connect (descendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_descend ()));
Connect (filteraction, SIGNAL (triggered (bool)), this, SLOT (Show_filter_dlg ()));
Connect (reshowaction, SIGNAL (triggered (bool)), this, SLOT (Reshow_data ()));

Cmenu->exec (qcursor::p OS ());//display at current mouse position
Cmenu->exec (POS) is displayed in viewport
}

You can also do CMenu first, the advantage is always to use a:
Qmenu CMenu = new Qmenu (Datatable->horizontalheader ());

Qaction *ascendsortaction = cmenu->addaction ("ascending");
Qaction *descendsortaction = cmenu->addaction ("descending");
Qaction *filteraction = cmenu->addaction ("filtration");
Qaction *reshowaction = Cmenu->addaction ("overload");

Connect (ascendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_ascend ()));
Connect (descendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_descend ()));
Connect (filteraction, SIGNAL (triggered (bool)), this, SLOT (Show_filter_dlg ()));
Connect (reshowaction, SIGNAL (triggered (bool)), this, SLOT (Reshow_data ()));
Show_contextmenu (const qpoint& POS)
{
if (CMenu)
{
Cmenu->exec (qcursor::p OS ());
}
}

3) using Qt::actionscontextmenu.
Display all the action of the part, that is, qwidget::actions () as the context menu.
Or the example above, to display the right-click menu in the table (Qtableview type) Header:
Qaction *ascendsortaction = new Qaction ("ascending", this);
Qaction *descendsortaction = new Qaction ("descending", this);
Qaction *filteraction = new Qaction ("filter", this);
Qaction *unfilteraction = new Qaction ("Cancel Filter", this);

Connect (ascendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_ascend ()));
Connect (descendsortaction, SIGNAL (triggered (bool)), this, SLOT (Sort_descend ()));
Connect (filteraction, SIGNAL (triggered (bool)), this, SLOT (Filter_table ()));
Connect (unfilteraction, SIGNAL (triggered (bool)), this, SLOT (Unfilter_table ()));

Datatable->horizontalheader ()->addaction (ascendsortaction);
Datatable->horizontalheader ()->addaction (descendsortaction);
Datatable->horizontalheader ()->addaction (filteraction);
Datatable->horizontalheader ()->addaction (unfilteraction);
Datatable->horizontalheader ()->setcontextmenupolicy (Qt::actionscontextmenu);

The other two are not displaying Context menu:
Qt::nocontextmenu
    the widget does  not feature a context menu, context menu handling is  Deferred to the widget ' S parent.
    
Qt::P reventcontextmenu
    the widget does  not feature a context menu, and in contrast to  Nocontextmenu, the handling is not deferred to the widget ' s  parent. this means that all right mouse button events are  guaranteed to be delivered to the widget itself through  Mousepressevent (),  and mousereleaseevent ().

Add:
Using Qt::actionscontextmenu is simpler, but if you need to define a different menu based on where the current menu pops up or, as in the previous example, when the table (Qtableview type) header shows the right-click menu, I need to know which list header is clicked, Thus, when the sort_ascend () sorting function is later invoked, different sorting strategies can be made based on different columns, so that qt::actionscontextmenu cannot be done.
This need to capture the pop-up position of the situation had to use Qt::actionscontextmenu, customcontextmenurequested (const Qpoint & POS) The signal returns to the click Position POS (position in the header viewport coordinate system), then the table header can call the Logicalindexat (POS) function to get the column number of the index that is clicked, corresponding to the click Section, and then save it for use in the sort slot of the later action activation.
Show_contextmenu (const qpoint& POS)
{
Get related column of Headerview
Contextmenu_column = Datatable->horizontalheader ()->logicalindexat (POS);

Show ContextMenu
if (CMenu)
{

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.