Qt之QAbstractItemView右鍵菜單


一、功能概述

    說起右鍵菜單,之前Qt之自定義QLineEdit右鍵菜單這篇文章中我已經講述過3種右鍵菜單的實現方式,今兒也是在啰嗦一下,針對QListWidget類在定制一下右鍵菜單,我使用的具體方式呢,是直接重寫了contextMenuEvent方法,在這個方法中彈出右鍵菜單。

二、效果展示

    如圖1是針對QListWidget定制的右鍵菜單,美觀程度一般,但是功能基本實現

圖1 QListWidget右鍵菜單

三、代碼講解

    右鍵菜單在相關文章小節中我已經給出了一些文章,關於右鍵菜單的彈出邏輯我就不在細說了,在這里我就簡單說下右鍵菜單的處理代碼

    ListItem是定制的item項,使用setItemWidget接口設置為QStandardListWidgetItem項的窗口

1、刪除,首先獲取當前項,然后獲取獲取當前項上的窗口,並把其析構

 

 1 void DragList::DeleteSotck()
 2 {
 3     QListWidgetItem * item = currentItem();
 4     if (item == nullptr)
 5     {
 6         return;
 7     }
 8     
 9     if (ListItem * itemWidget = ItemWidget(item))
10     {
11         itemWidget->deleteLater();
12         itemWidget = nullptr;
13     }
14     
15     delete item;
16     item = nullptr;
17 }

 

2、置頂,這個操作需要注意,如果當前項已經是第一個,那么不需要做任何處理

 1 void DragList::TopSotck()
 2 {
 3     QListWidgetItem * item = currentItem();
 4     if (item == nullptr)
 5     {
 6         return;
 7     }
 8 
 9     if (row(item) == 0)
10     {
11         return;
12     }
13 
14     ListItem * itemWidget = ItemWidget(item);
15     QListWidgetItem * newItem = takeItem(row(item));
16     insertItem(0, newItem);
17     ListItem * topWidget = new ListItem;
18     topWidget->SetData(itemWidget->GetData());
19     setItemWidget(newItem, topWidget);
20 
21     if (itemWidget)
22     {
23         delete itemWidget;
24         itemWidget = nullptr;
25     }
26     setCurrentItem(newItem);
27 }

3、置低,同樣的道理,如果當前項是最后一個,那么也不需要做處理

 1 void DragList::BottomSotck()
 2 {
 3     QListWidgetItem * item = currentItem();
 4     if (item == nullptr)
 5     {
 6         return;
 7     }
 8 
 9     if (row(item) == count() - 1)
10     {
11         return;
12     }
13 
14     ListItem * itemWidget = ItemWidget(item);
15     QListWidgetItem * newItem = takeItem(row(item));
16     addItem(newItem);
17     ListItem * bottomWidget = new ListItem;
18     bottomWidget->SetData(itemWidget->GetData());
19     setItemWidget(newItem, bottomWidget);
20 
21     if (itemWidget)
22     {
23         delete itemWidget;
24         itemWidget = nullptr;
25     }
26     setCurrentItem(newItem);
27 }

4、上移一位

 1 void DragList::UpSotck()
 2 {
 3     QListWidgetItem * item = currentItem();
 4     if (item == nullptr)
 5     {
 6         return;
 7     }
 8 
 9     int itemRow = row(item);
10     if (itemRow == 0)
11     {
12         return;
13     }
14 
15     ListItem * itemWidget = ItemWidget(item);
16     QListWidgetItem * newItem = takeItem(row(item));
17     insertItem(itemRow - 1, newItem);
18     ListItem * upWidget = new ListItem;
19     upWidget->SetData(itemWidget->GetData());
20     setItemWidget(newItem, upWidget);
21 
22     if (itemWidget)
23     {
24         delete itemWidget;
25         itemWidget = nullptr;
26     }
27 
28     setCurrentItem(newItem);
29 }

5、下移一位

 1 void DragList::DownSotck()
 2 {
 3     QListWidgetItem * item = currentItem();
 4     if (item == nullptr)
 5     {
 6         return;
 7     }
 8 
 9     int itemRow = row(item);
10     if (itemRow == count() - 1)
11     {
12         return;
13     }
14 
15     ListItem * itemWidget = ItemWidget(item);
16     QListWidgetItem * newItem = takeItem(row(item));
17     insertItem(itemRow + 1, newItem);
18     ListItem * downWidget = new ListItem;
19     downWidget->SetData(itemWidget->GetData());
20     setItemWidget(newItem, downWidget);
21 
22     if (itemWidget)
23     {
24         delete itemWidget;
25         itemWidget = nullptr;
26     }
27     setCurrentItem(newItem);
28 }

四、示例代碼

    Qt之QAbstractItemView視圖右鍵菜單 

五、相關文章

    Qt之自定義QLineEdit右鍵菜單

    qt之菜單項定制

    Qt 彈出式菜單陰影

    Qt之默認菜單語言設置

如果您覺得文章不錯,不妨給個 打賞,寫作不易,感謝各位的支持。您的支持是我最大的動力,謝謝!!! 

 

  


很重要--轉載聲明

  1. 本站文章無特別說明,皆為原創,版權所有,轉載時請用鏈接的方式,給出原文出處。同時寫上原作者:朝十晚八 or Twowords
  2. 如要轉載,請原文轉載,如在轉載時修改本文,請事先告知,謝絕在轉載時通過修改本文達到有利於轉載者的目的。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM