I. Overview of functions
Speaking of the right-click menu, before Qt's custom Qlineedit right-click menu This article I have described the implementation of 3 kinds of right-click menu, today is also in a wordy, for the Qlistwidget class in the Custom right-click menu, I use the specific way, is to directly rewrite the Contextmenuevent method, which pops up the right-click menu in this method.
Second, the effect shows
1 is the right-click menu for Qlistwidget customization, the general aesthetic level, but the basic realization of the function
Figure 1 Qlistwidget Right-click menu
Third, the code explanation
Right-click menu in the related articles section I have given some articles, on the right-click menu pop-up logic I do not elaborate, here I will simply say right-click menu processing Code
ListItem is a custom item item that uses the Setitemwidget interface to set the window to the Qstandardlistwidgetitem item
1, delete, first get the current item, and then get the window on the current item, and the destructor
1 voiddraglist::D eletesotck ()2 {3Qlistwidgetitem * item =CurrentItem ();4 if(Item = =nullptr)5 {6 return;7 }8 9 if(ListItem * Itemwidget =Itemwidget (item))Ten { OneItemwidget->Deletelater (); AItemwidget =nullptr; - } - the Deleteitem; -item =nullptr; -}
2, top, this operation needs to be noted, if the current item is already the first, then do not need to do any processing
1 voidDraglist::topsotck ()2 {3Qlistwidgetitem * item =CurrentItem ();4 if(Item = =nullptr)5 {6 return;7 }8 9 if(Row (item) = =0)Ten { One return; A } - -ListItem * Itemwidget =itemwidget (item); theQlistwidgetitem * NewItem =Takeitem (Row (item)); -InsertItem (0, newitem); -ListItem * Topwidget =NewListItem; -Topwidget->setdata (itemwidget->GetData ()); + Setitemwidget (NewItem, topwidget); - + if(Itemwidget) A { at DeleteItemwidget; -Itemwidget =nullptr; - } - Setcurrentitem (newitem); -}
3, low, the same truth, if the current item is the last one, then do not need to do processing
1 voidDraglist::bottomsotck ()2 {3Qlistwidgetitem * item =CurrentItem ();4 if(Item = =nullptr)5 {6 return;7 }8 9 if(Row (item) = = COUNT ()-1)Ten { One return; A } - -ListItem * Itemwidget =itemwidget (item); theQlistwidgetitem * NewItem =Takeitem (Row (item)); - AddItem (newitem); -ListItem * Bottomwidget =NewListItem; -Bottomwidget->setdata (itemwidget->GetData ()); + Setitemwidget (NewItem, bottomwidget); - + if(Itemwidget) A { at DeleteItemwidget; -Itemwidget =nullptr; - } - Setcurrentitem (newitem); -}
4. Move up one
1 voidDraglist::upsotck ()2 {3Qlistwidgetitem * item =CurrentItem ();4 if(Item = =nullptr)5 {6 return;7 }8 9 intItemrow =row (item);Ten if(Itemrow = =0) One { A return; - } - theListItem * Itemwidget =itemwidget (item); -Qlistwidgetitem * NewItem =Takeitem (Row (item)); -InsertItem (Itemrow-1, newitem); -ListItem * Upwidget =NewListItem; +Upwidget->setdata (itemwidget->GetData ()); - Setitemwidget (NewItem, upwidget); + A if(Itemwidget) at { - DeleteItemwidget; -Itemwidget =nullptr; - } - - Setcurrentitem (newitem); in}
5. Move down one
1 voiddraglist::D ownsotck ()2 {3Qlistwidgetitem * item =CurrentItem ();4 if(Item = =nullptr)5 {6 return;7 }8 9 intItemrow =row (item);Ten if(Itemrow = = count ()-1) One { A return; - } - theListItem * Itemwidget =itemwidget (item); -Qlistwidgetitem * NewItem =Takeitem (Row (item)); -InsertItem (Itemrow +1, newitem); -ListItem * Downwidget =NewListItem; +Downwidget->setdata (itemwidget->GetData ()); - Setitemwidget (NewItem, downwidget); + A if(Itemwidget) at { - DeleteItemwidget; -Itemwidget =nullptr; - } - Setcurrentitem (newitem); -}
Iv. Sample Code
QT Qabstractitemview View Right-click menu
V. RELATED articles
QT's Custom Qlineedit right-click menu
QT Menu item Customization
Qt Pop-up menu shadow
QT's default menu language setting
QT's Qabstractitemview Right-click menu