PDA

View Full Version : Comparing Items In List Box



kenny_isles
15th February 2007, 08:12
Hi All,

I have two list boxes. If items in list box 2 are there in list box 1 then that item should be deleted from listbox1.

Some One Please tell me why the below piece of code ain't working.


ListBox 1 - AvailableParam_lb
ListBox 2 - ChoosenParam_lb


int size,i,j;
size = ChoosenParam_lb->numRows ();
for (j=0;j<= size; j++)
{
for (i=0;i<36;i++)
{
AvailableParam_lb->setCurrentItem(i);
ChoosenParam_lb->setCurrentItem(j);
if ( ChoosenParam_lb->currentItem() == AvailableParam_lb->currentItem() )
{
AvailableParam_lb->removeItem( AvailableParam_lb->currentItem() );
}
}
}

With Kind Regards
Kenny

wysota
15th February 2007, 12:32
You're comparing pointers, not their contents. An item can only be in one list at once, therefore even if items have the same text, they are different items. Try doing something like:

if ( ChoosenParam_lb->currentItem() && AvailableParam_lb->currentItem() &&
ChoosenParam_lb->currentItem()->text() == AvailableParam_lb->currentItem()->text() ) ...

Or better yet:

for(QListBoxItem *iter = ChoosenParam_lb->item(0); iter!=0; iter = iter->next()){
delete AvailableParam_lb->findItem(iter->text(), Qt::ExactMatch);
}

rajesh
16th February 2007, 06:32
Try this code:


QString selectedItem = ChoosenParam_lb->currentItem()->text();

QList<QListWidgetItem *> itemlist = AvailableParam_lb->findItems(selectedItem ,Qt::MatchExactly);
if(itemlist.size()> 0)
{
while (!itemlist.isEmpty())
delete itemlist.takeFirst();
}

kenny_isles
16th February 2007, 10:29
Hi Rajesh,

The code segment u gave is not working for me. I think it is not compatible with qt3. Anyway thanx for the help.

rajesh
16th February 2007, 10:48
Hi kenny,

I have written Qt4 code.

try following code for Qt3:


QListViewItem *item;
item = AvailableParam_lb->findItem ( ChoosenParam_lb->currentItem()->text(), 0, QListView::ExactMatch | Qt::CaseSensitive )
if(item != NULL)
delete item; // or AvailableParam_lb->removeItem( item );

kenny_isles
16th February 2007, 12:44
Hi Rajesh,

I think I have narrowed down the problem. I am Inserting items to the List Box Using InsertItem Method. Which is a int. All other statements for comparison, manipulation etc go thro the currentText() or other Text() Methods which is QString. So I think if we give data to the list box as text then it will solve the problem.

I put a printf statement right after entering the values using InsertItem() and to my horror is is showing (NULL);

So can anyone tell me how to Insert a Text to the List Box.


Regards
Kenny

wysota
16th February 2007, 13:57
Does the code I provided work (especially the last part)?

BTW. I don't see any "insertItem" method taking its content as an int...

kenny_isles
19th February 2007, 11:38
Hi,

Sorry I think I am confusing myself and others. This is what I am doing.

I am loading values to the list box from the table in the first place.

The Table is in the main window and the list box is residing in another dialog box.

So to send data I am using the following code segment.


b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );

where
b is object to the dialog that holds the list box,
ChoosenParam_lb is the name of the dialog
and Table1 is the name of the Table.

The test statement and output are as follows
This is given immediately after this code


s=b->ChoosenParam_lb->currentText();//Test
printf("Choosen Param : %s\n",s);//Test

O/P:- (null);


The Whole Code is For loading the values is given below


//Insert Items From Table to List Box Which is in Parameter Selection Dialog
const char *s;
if (size1 >18)
{
a = size1-18;
for (i=0; i<18; i++)
{
b->ChoosenParam_lb->setCurrentItem(i); //Test
b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );

s=b->ChoosenParam_lb->currentText();//Test
printf("Choosen Param : %s\n",s);//Test
}
}
else
{
for (i=0; i<size1; i++)
{
b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
}
}
if ( a!=0)
{
for (i=0; i<a; i++)
{
b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
}
}

Please see this and tell me what wrong.

With Kind Regards
Kenny

wysota
19th February 2007, 12:09
I don't understand what the problem you have, but I see that the NULL you get might be because current item may be invalid. If you have 0 items in your list and set the current item to 0 then you'll get NULL as there is no item with index 0. If you add an item then, the current item will still be invalid. Then you set the current item to 1, but there is no item with that index in the list box so again the current item is not valid. Then you add an item, but the current item is still empty, so you'll never get a current item this way. Try moving the setCurrentItem() statement after you insert the item or don't use the current item at all - what do you need it for? You can use QListBox::text() instead.

kenny_isles
21st February 2007, 13:06
Hi Guys,

Sorry for confusing U all and myself.

The code has run.

My logic is rite.

Only thing is that it should have been placed in the Parent Dialog box rather than Child Dialog Box.

Regards
Kenny'