QT Learning Solution qlistwidgetitem overlap, point to all selected issues

Source: Internet
Author: User

Qlistwidget in Qlistview::iconmode mode, when the entry can be moved, if some or all overlap occurs, then clicking on one of the Qlistwidgetitem will also check the overlapping qlistwidgetitem, the problem is as follows:


But I just want to choose the one I clicked on, solve this problem, didn't find a good way. To process by inheriting the Mousepressevent event:

void Listwidget::mousepressevent (Qmouseevent *event) {__super::mousepressevent (event); Qlistwidgetitem *pitem = Itemat (Event->pos ()), if (pitem) {int iCount = count (); for (size_t i = 0; i < ICount; i++) {QL Istwidgetitem *p = Item (i), if (!p) break;if (P! = Pitem) p->setselected (false); elsep->setselected (True);}}
or:
void Listwidget::mousepressevent (Qmouseevent *event) {__super::mousepressevent (event); Qlistwidgetitem *pitem = Itemat (Event->pos ()), if (pitem) {qlist<qlistwidgetitem *> pitems = SelectedItems (); for (Auto it = Pitems.begin (); It! = Pitems.end (); it++) {if (*it)! = Pitem) (*it)->setselected (false); else (*it)->se Tselected (True);}}}
can meet the requirements. If someone has a better way, please instruct.

Effect:


Complete test Code:

 #ifndef Listwidget_h#define listwidget_h#include <qtwidgets/qlistwidget>class listwidget:public QListWidget{Q_ Objectpublic:listwidget (Qwidget *parent = 0); ~listwidget ();p rotected:void mousepressevent (QMouseEvent *event); Private:}; #endif//Listwidget_h 
#include "listwidget.h" #include <qmouseevent>listwidget::listwidget (Qwidget *parent): Qlistwidget (parent) {// Add Itemqstringlist strlist = {"Item1", "Item2", "Item3", "Item4", "ITEM5"};this->additems (strlist);this-> Setviewmode (Qlistview::iconmode); This->setresizemode (qlistview::adjust); This->seticonsize (QSize (64, 64));} Listwidget::~listwidget () {}void listwidget::mousepressevent (qmouseevent *event) {__super::mousepressevent (event); Qlistwidgetitem *pitem = Itemat (Event->pos ()), if (pitem) {qlist<qlistwidgetitem *> pitems = SelectedItems (); for (Auto it = Pitems.begin (); It! = Pitems.end (); it++) {if (*it)! = Pitem) (*it)->setselected (false); else (*it)->se Tselected (True);}}} void Listwidget::mousepressevent (Qmouseevent *event)//{//__super::mousepressevent (event);//qlistwidgetitem * Pitem = Itemat (Event->pos ()),//if (pitem)//{//int ICount = count ();//for (size_t i = 0; i < ICount; i++)//{//qlistwi Dgetitem *p = Item (i);//if (!p)//break;////if (P! = pitem)//p->setselected (false);//else//p->setselected (true);//}//}//} 
#include "listwidget.h" #include <qtwidgets/qapplication>int main (int argc, char *argv[]) {qapplication A (argc, ARGV); Listwidget w;w.show (); return a.exec ();}
This address: http://blog.csdn.net/u011417605/article/details/50954779

AC qq:1245178753

QT Learning Solution qlistwidgetitem overlap, point to all selected issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.