[Qt-interest] setSelected in QListWidget

Kenneth Beck nekkceb at comcast.net
Wed Oct 21 14:58:40 CEST 2009


I am having trouble pre-specifying the selected items in a QListWidget 
(Windows XP, Qt 4.5, MS Visual C++ 2003).

In the constructor of my dialog, I call:
graphVarList1->setSelectionMode(QAbstractItemView::ExtendedSelection);

Then in the showEvent, I fill the graphVarList1 with text items from a 
QStringList using addItems(vlst) (vlst has about 30 items in it).

Now I read a much shorter QStringList of items (g1vl) I want selected 
and do the following loop (gsz = 30, and is the number of items in 
graphVarList1):

for (i=0;i<gsz;i++)
     for (j=0;j<g1vl.size();j++)
       if (g1vl.at(j).compare(graphVarList1->at(i),
           Qt::CaseInsensitive)!=0)
         graphVarList1->item(i)->setSelected(false);
       else
         {
         graphVarList1->item(i)->setSelected(true);
         }

If there are multiple items in the g1vl, only the last item remains 
selected when the dialog is displayed. When I single-step through the 
code, the setSelected(true) line is being called appropriately. I have 
no trouble selecting multiple items and saving them, but the initial 
setup is not acting right, as if the second call to setSelected 
de-selects other items, contrary to the ExtendedSelection rule I setup 
in the constructor.




More information about the Qt-interest-old mailing list