其他分享
首页 > 其他分享> > QT使用右键菜单QMenu

QT使用右键菜单QMenu

作者:互联网

看下面动画
在这里插入图片描述
关键代码:

#pragma once
 
#include <QtWidgets/QMainWindow>
#include<QAction>
#include<QMenu>
#include <qmessagebox.h>
#include <qobject.h>
 
#include "ui_MyQmenu.h"
 
class MyQmenu : public QMainWindow
{
	Q_OBJECT
 
public:
	MyQmenu(QWidget *parent = Q_NULLPTR);
 
	private slots:
		void clicked_A();
		void clicked_B();
		void clicked_C();
 
private:
	Ui::MyQmenuClass ui;
	QAction *A ;
	QAction *B;
	QAction *C;
};
#include "MyQmenu.h"
 
MyQmenu::MyQmenu(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
 
	A = new QAction(QString::fromLocal8Bit("打开(o)"));
	B = new QAction(QString::fromLocal8Bit("新建(n)"));
	C = new QAction(QString::fromLocal8Bit("关闭(q)"));
 
	A->setObjectName("A");
	B->setObjectName("B");
	C->setObjectName("C");
 
	addAction(A);
	addAction(B);
	addAction(C);
 
	setContextMenuPolicy(Qt::ActionsContextMenu);
 
	connect(A, SIGNAL(triggered()), this, SLOT(clicked_A()));
	connect(B, SIGNAL(triggered()), this, SLOT(clicked_B()));
	connect(C, SIGNAL(triggered()), this, SLOT(clicked_C()));
 
}
void MyQmenu::clicked_A()
{
	QMessageBox::about(NULL, "tips", "A"+A->objectName());
}
 
void MyQmenu::clicked_B()
{
	QMessageBox::about(NULL,"tips","B" + B->objectName());
}
 
void MyQmenu::clicked_C()
{
	QMessageBox::about(NULL, "tips", "C" + C->objectName());
}

标签:addAction,clicked,QT,QMenu,void,QAction,右键,include,MyQmenu
来源: https://blog.csdn.net/qq_45662588/article/details/120355288