Qt QListWidget右鍵彈出刪除菜單

在QListWidget上右擊鼠標彈出刪除菜單

第一步,建立一個包含兩條數據的QListWidget
1     /*初始化一個包含兩個Action(Delete和ClearAll)的菜單*/
2     popMenu_In_ListWidget_ = new QMenu(this); 3     action_Delete_In_ListWidget_ = new QAction(tr("Delete"), this); 4     action_Clear_In_ListWidget_ = new QAction(tr("ClearAll"), this); 5     popMenu_In_ListWidget_->addAction(action_Delete_In_ListWidget_); 6     popMenu_In_ListWidget_->addAction(action_Clear_In_ListWidget_);
在這裏插入圖片描述
第二步,設置QListWidget的contextMenuPolicy屬性,否則不能顯示右鍵菜單
1     /*重要:設置QListWidget的contextMenuPolicy屬性,否則不能顯示右鍵菜單*/
2     list_widget->setProperty("contextMenuPolicy", Qt::CustomContextMenu);
第二步,建立一個包含QAction的QMenu菜單
1     /*初始化一個包含兩個Action(Delete和ClearAll)的菜單*/
2     popMenu_In_ListWidget_ = new QMenu(this); 3     action_Delete_In_ListWidget_ = new QAction(tr("Delete"), this); 4     action_Clear_In_ListWidget_ = new QAction(tr("ClearAll"), this); 5     popMenu_In_ListWidget_->addAction(action_Delete_In_ListWidget_); 6     popMenu_In_ListWidget_->addAction(action_Clear_In_ListWidget_);

在這裏插入圖片描述

第三步,爲菜單的上的QAction添加響應函數
 1 //事件處理函數
 2 void Dialog::onActionDelete()  3 {  4     /*獲取當前選中的Item*/
 5     QList<QListWidgetItem*> items = this->list_widget->selectedItems();  6 
 7     if (items.count() > 0)  8  {  9         /*彈出詢問對話框*/
10         if (QMessageBox::Yes == QMessageBox::question(this, QStringLiteral("Remove Item"), 11             QStringLiteral("Remove %1 item").arg(QString::number(items.count())), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)) 12  { 13             foreach(QListWidgetItem* var, items) { 14                 this->list_widget->removeItemWidget(var); 15                 items.removeOne(var); 16                 delete var; 17  } 18  } 19  } 20 }
1 //綁定處理函數
2 /*爲菜單上的Delete選項添加響應函數*/
3 connect(this->action_Delete_In_ListWidget_, SIGNAL(triggered()), this, SLOT(onActionDelete()));
第四步,爲QListWidget添加右鍵彈出菜單函數
1 //事件處理函數
2 void Dialog::onCustomContextMenuRequested(const QPoint &pos) 3 { 4     /*彈出右鍵菜單*/
5     popMenu_In_ListWidget_->exec(QCursor::pos()); 6 }
1 //綁定處理函數
2 /*綁定右鍵顯示菜單:在單擊右鍵以後會執行槽函數, 槽函數中負責彈出右鍵菜單*/
3 connect(this->list_widget, SIGNAL(customContextMenuRequested(const QPoint &)), 4     this, SLOT(onCustomContextMenuRequested(const QPoint &)));
效果

鼠標右鍵點擊,彈出菜單
在這裏插入圖片描述
點擊Delete彈出是否刪除對話框
在這裏插入圖片描述app

代碼示例

main_dialog.h函數

 1 #include <QDialog>
 2 
 3 class QMenu;  4 class QListWidget;  5 
 6 class Dialog : public QDialog  7 {  8  Q_OBJECT  9 
10 public: 11     Dialog(QWidget* parent = NULL); 12     ~Dialog(); 13 
14     private slots: 15 
16     /*處理鼠標右鍵事件,彈出菜單*/
17     void onCustomContextMenuRequested(const QPoint &pos); 18 
19     /*處理彈出菜單上的Delete事件*/
20     void onActionDelete(); 21 
22 private: 23 
24     QMenu *popMenu_In_ListWidget_;/*彈出菜單*/
25     QAction *action_Delete_In_ListWidget_;/*菜單上的Action*/
26     QAction *action_Clear_In_ListWidget_;/*菜單上的Action*/
27     QListWidget *list_widget;/*列表*/
28 
29 };

main.cppui

 1 #include <QtWidgets/QApplication>
 2 #include <QListWidget>
 3 #include <QMenu>
 4 #include <QMessageBox>
 5 
 6 #include "main_dialog.h"
 7 
 8 Dialog::Dialog(QWidget *parent) :QDialog(parent)  9 { 10     /*設置對話框大小*/
11     this->setFixedSize(500, 400); 12 
13     /*在對話框上添加一個QListWidget控件*/
14     list_widget = new QListWidget(this); 15     list_widget->setSelectionMode(QAbstractItemView::ContiguousSelection);/*設置能夠多選*/
16 
17     /*在QListWidget控件上添加兩個Item*/
18     list_widget->addItem("this is 1th item"); 19     list_widget->addItem("this is 2th item"); 20 
21     /*重要:設置QListWidget的contextMenuPolicy屬性,否則不能顯示右鍵菜單*/
22     list_widget->setProperty("contextMenuPolicy", Qt::CustomContextMenu); 23 
24     /*初始化一個包含兩個Action(Delete和ClearAll)的菜單*/
25     popMenu_In_ListWidget_ = new QMenu(this); 26     action_Delete_In_ListWidget_ = new QAction(tr("Delete"), this); 27     action_Clear_In_ListWidget_ = new QAction(tr("ClearAll"), this); 28     popMenu_In_ListWidget_->addAction(action_Delete_In_ListWidget_); 29     popMenu_In_ListWidget_->addAction(action_Clear_In_ListWidget_); 30 
31     /*爲菜單上的Delete選項添加響應函數*/
32     connect(this->action_Delete_In_ListWidget_, SIGNAL(triggered()), this, SLOT(onActionDelete())); 33 
34     /*綁定右鍵顯示菜單:在單擊右鍵以後會執行槽函數, 槽函數中負責彈出右鍵菜單*/
35     connect(this->list_widget, SIGNAL(customContextMenuRequested(const QPoint &)), 36         this, SLOT(onCustomContextMenuRequested(const QPoint &))); 37 
38 } 39 Dialog::~Dialog() 40 { 41 
42 } 43 
44 void Dialog::onCustomContextMenuRequested(const QPoint &pos) 45 { 46     /*彈出右鍵菜單*/
47     popMenu_In_ListWidget_->exec(QCursor::pos()); 48 } 49 
50 void Dialog::onActionDelete() 51 { 52     /*獲取當前選中的Item*/
53     QList<QListWidgetItem*> items = this->list_widget->selectedItems(); 54 
55     if (items.count() > 0) 56  { 57         /*彈出詢問對話框*/
58         if (QMessageBox::Yes == QMessageBox::question(this, QStringLiteral("Remove Item"), 59             QStringLiteral("Remove %1 item").arg(QString::number(items.count())), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)) 60  { 61             foreach(QListWidgetItem* var, items) { 62                 this->list_widget->removeItemWidget(var); 63                 items.removeOne(var); 64                 delete var; 65  } 66  } 67  } 68 } 69 
70 int main(int argc, char *argv[]) 71 { 72  QApplication app(argc, argv); 73  Dialog dia; 74  dia.show(); 75     return app.exec(); 76 }

CMakeListsthis

 1 cmake_minimum_required (VERSION 2.6 FATAL_ERROR)  2 
 3 project(QListWidget_Menu)  4 find_package (Qt5 REQUIRED Core Widgets)  5 
 6 set (QT_UI_HEADERS main_dialog.h)  7 QT5_WRAP_CPP (project_HEADERS_MOC ${QT_UI_HEADERS})  8 
 9 file(GLOB Demo_Src ./*.cpp) 10 set (project_SOURCES ${Demo_Src}) 11 
12 file(GLOB Demo_Header ./*.h) 13 set (INCLUDE_FILE ${Demo_Header} ) 14 
15 set (project_HEADERS ${INCLUDE_FILE} ${QT_UI_HEADERS}) 16 
17 ADD_DEFINITIONS (${QT_DEFINITIONS}) 18 ADD_DEFINITIONS(-D _XKEYCHECK_H) 19 ADD_EXECUTABLE (Demo ${project_SOURCES} ${project_HEADERS_MOC} ${project_HEADERS}) 20 
21 set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Demo) 22 
23 TARGET_LINK_LIBRARIES (Demo Qt5::Core Qt5::Widgets)
相關文章
相關標籤/搜索