QT QTreeWidgetItem 右键点击弹出多个菜单选项

                                                                    困扰我很久的QT问题二

环境介绍

VS2015 [Debug/Release x86]    Qt 5.10.0 ;

一、效果

QT QTreeWidgetItem 右键点击弹出多个菜单选项_第1张图片

二、实现

1、变量、槽函数
QT QTreeWidgetItem 右键点击弹出多个菜单选项_第2张图片

2、实现

{
    ......//前面内容都省略了  注:toolBoxTree = new QTreeWidget(this);//......
	RGB_to_GRAY = new QAction("SRC_img", toolBoxTree);   //因为是在QTreeWidget内实现右键,所以以其作为父类
	Threshold = new QAction("SRC_gray_img", toolBoxTree);  
	test_else = new QAction("SRC_img", toolBoxTree);  
	ThresholdList << test_else\
		<< Threshold;
	toolBoxTree_preprocess_1_menu = new QMenu(toolBoxTree);
	toolBoxTree_preprocess_2_menu = new QMenu(toolBoxTree);

	connect(toolBoxTree, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, SLOT(TreeWidgetItemPressed_Slot(QTreeWidgetItem*, int)));
	connect(RGB_to_GRAY, SIGNAL(triggered(bool)), this, SLOT(RGB_to_GRAY_Slot()));
	connect(Threshold, SIGNAL(triggered(bool)), this, SLOT(Threshold_Slot()));
	connect(test_else, SIGNAL(triggered(bool)), this, SLOT(Threshold_Slot2()));

}

void W_TOOL::RGB_to_GRAY_Slot()
{
	showShrinkPicture->setStyleSheet("background:rgb(255,0,0)");
}

void W_TOOL::Threshold_Slot()
{
	showShrinkPicture->setStyleSheet("background:rgb(0,255,0)");
}

void W_TOOL::Threshold_Slot2()
{
	showShrinkPicture->setStyleSheet("background:rgb(0,0,255)");
}

void W_TOOL::TreeWidgetItemPressed_Slot(QTreeWidgetItem * pressedItem, int column)
{
	if (qApp->mouseButtons() == Qt::RightButton)
	{
		if (pressedItem->text(column) == "rgb_to_gray")
		{
			toolBoxTree_preprocess_1_menu->addAction(RGB_to_GRAY);
			toolBoxTree_preprocess_1_menu->exec(QCursor::pos());   //菜单弹出位置为鼠标点击位置
		}
		if (pressedItem->text(column) == "threshold")
		{
			toolBoxTree_preprocess_2_menu->addActions(ThresholdList);
			toolBoxTree_preprocess_2_menu->exec(QCursor::pos());
		}
	}
}

你可能感兴趣的:(Qt5)