[Qt-interest] setSelected in QListWidget

Kenneth Beck nekkceb at comcast.net
Thu Oct 22 05:12:29 CEST 2009


Kenneth Beck wrote:
> 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.
> 
OOPS!
The problem is in coding without adequate dose of caffeine. Sorry about 
the post folks!



More information about the Qt-interest-old mailing list