Results 1 to 10 of 10

Thread: Comparing Items In List Box

  1. #1
    Join Date
    Feb 2007
    Posts
    22
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Comparing Items In List Box

    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

    Qt Code:
    1. int size,i,j;
    2. size = ChoosenParam_lb->numRows ();
    3. for (j=0;j<= size; j++)
    4. {
    5. for (i=0;i<36;i++)
    6. {
    7. AvailableParam_lb->setCurrentItem(i);
    8. ChoosenParam_lb->setCurrentItem(j);
    9. if ( ChoosenParam_lb->currentItem() == AvailableParam_lb->currentItem() )
    10. {
    11. AvailableParam_lb->removeItem( AvailableParam_lb->currentItem() );
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    With Kind Regards
    Kenny
    Last edited by wysota; 19th February 2007 at 13:11. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing Items In List Box

    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:
    Qt Code:
    1. if ( ChoosenParam_lb->currentItem() && AvailableParam_lb->currentItem() &&
    2. ChoosenParam_lb->currentItem()->text() == AvailableParam_lb->currentItem()->text() ) ...
    To copy to clipboard, switch view to plain text mode 

    Or better yet:
    Qt Code:
    1. for(QListBoxItem *iter = ChoosenParam_lb->item(0); iter!=0; iter = iter->next()){
    2. delete AvailableParam_lb->findItem(iter->text(), Qt::ExactMatch);
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 15th February 2007 at 14:35. Reason: wrapped too long line

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Smile Re: Comparing Items In List Box

    Try this code:
    Qt Code:
    1. QString selectedItem = ChoosenParam_lb->currentItem()->text();
    2.  
    3. QList<QListWidgetItem *> itemlist = AvailableParam_lb->findItems(selectedItem ,Qt::MatchExactly);
    4. if(itemlist.size()> 0)
    5. {
    6. while (!itemlist.isEmpty())
    7. delete itemlist.takeFirst();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 16th February 2007 at 12:21. Reason: changed [html] to [code]

  4. #4
    Join Date
    Feb 2007
    Posts
    22
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Comparing Items In List Box

    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.

  5. #5
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Comparing Items In List Box

    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 );

  6. #6
    Join Date
    Feb 2007
    Posts
    22
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Comparing Items In List Box

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing Items In List Box

    Does the code I provided work (especially the last part)?

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

  8. #8
    Join Date
    Feb 2007
    Posts
    22
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Comparing Items In List Box

    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.

    Qt Code:
    1. b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. s=b->ChoosenParam_lb->currentText();//Test
    2. printf("Choosen Param : %s\n",s);//Test
    To copy to clipboard, switch view to plain text mode 

    O/P:- (null);


    The Whole Code is For loading the values is given below

    Qt Code:
    1. //Insert Items From Table to List Box Which is in Parameter Selection Dialog
    2. const char *s;
    3. if (size1 >18)
    4. {
    5. a = size1-18;
    6. for (i=0; i<18; i++)
    7. {
    8. b->ChoosenParam_lb->setCurrentItem(i); //Test
    9. b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
    10.  
    11. s=b->ChoosenParam_lb->currentText();//Test
    12. printf("Choosen Param : %s\n",s);//Test
    13. }
    14. }
    15. else
    16. {
    17. for (i=0; i<size1; i++)
    18. {
    19. b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
    20. }
    21. }
    22. if ( a!=0)
    23. {
    24. for (i=0; i<a; i++)
    25. {
    26. b->ChoosenParam_lb->insertItem ( table1->text(i,0), -1 );
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    Please see this and tell me what wrong.

    With Kind Regards
    Kenny
    Last edited by wysota; 19th February 2007 at 13:10. Reason: missing [code] tags

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing Items In List Box

    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.

  10. #10
    Join Date
    Feb 2007
    Posts
    22
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Comparing Items In List Box

    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'

Similar Threads

  1. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 21:13
  2. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 13:17
  3. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 20:08
  4. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 13:20
  5. Replies: 3
    Last Post: 10th April 2006, 20:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.