qt右键菜单及右键菜单功能

#include //右键菜单
#include //右键菜单

//在类中定义
QAction* set_par_only; //右键菜单
private slots:
void print_only(); //曹函数


//在主函数中
this->set_par_only = new QAction(tr("打印"), this); //右键菜单,参数
connect(set_par_only,SIGNAL(triggered()), this, SLOT(print_only()));
setContextMenuPolicy(Qt::DefaultContextMenu); //其实不用设置,默认就是这个值


//在这个函数中
void paint::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu(this);
menu->addAction(set_par_only);
menu->move(cursor().pos()); //让菜单显示的位置在鼠标的坐标上
menu->show();
}


//选中该选项后,执行下面的函数
void paint::print_only()
{
print<<"not very bad!";
}










你可能感兴趣的:(qt)