QT使用右键菜单QMenu

看下面动画

 

QT使用右键菜单QMenu_第1张图片

 

关键代码:

#pragma once

#include 
#include
#include
#include 
#include 

#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());
}

 

你可能感兴趣的:(Qt开发总结,qt,qmenu)