Results 1 to 6 of 6

Thread: Remove ownership of QTableWidgetitem from Qtablewidget

  1. #1
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Remove ownership of QTableWidgetitem from Qtablewidget

    Hello,


    QList<QTableWidgetItem*> pItemlst = ui->tableWidget->findItems(stext, Qt::MatchContains);

    QString stext = search string;

    this gives me a list of "QTableWidgetItem*" which have the text "stext". now i need to clear all the items in "tableWidget" and insert only the items in "pItemlst".

    When i try to insert give this error
    "QTableWidget: cannot insert an item that is already owned by another QTableWidget"

    All the "QTableWidgetItem*" items in pItemlst are owned by "tableWidget"...
    how can i remove the ownership of those items???\

    thank you

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Remove ownership of QTableWidgetitem from Qtablewidget

    Firstly, how are you going to empty your table widget? If you call clear() to empty your QTableWidget then all the owned items are deleted. This means all your pItemList pointers will be invalid.

    To get the QTableWidgetItems out of the QTableWidget and break the ownership then you need to use QTableWidget::takeItem().

  3. The following user says thank you to ChrisW67 for this useful post:

    deepal_de (5th May 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Remove ownership of QTableWidgetitem from Qtablewidget

    thanks for the reply.....

    then i have to user a "for" loop to go through each item in my "tableWidget" and crosscheck it with my "pItemlst" know?
    the problem is i have about 5000 records to search.

    this is a search function that runs on "QlineEdit text change event",
    so i was thinking a way to avoid searching all the records since it will take a long time to search and the UI will not respond during this time.

    I did consider using a thread.
    Q1) is there any other way than thread??

    Q2) Is there a way to break the ownership of the QTableWidget and keep the pointers in "pItemlst" intact?
    Last edited by deepal_de; 5th May 2011 at 12:31.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Remove ownership of QTableWidgetitem from Qtablewidget

    Quote Originally Posted by deepal_de View Post
    then i have to user a "for" loop to go through each item in my "tableWidget" and crosscheck it with my "pItemlst" know?
    the problem is i have about 5000 records to search.
    Don't worry about this too much. How do you think findItems() does it?

    this is a search function that runs on "QlineEdit text change event",
    so i was thinking a way to avoid searching all the records since it will take a long time to search and the UI will not respond during this time.
    Sounds like you are trying to implement a completer. Have you considered QCompleter?

    Have you considered using QTableView with a separate data model fed through a QSortFilterProxyModel?

    With your QTableWidget approach: Have you considered what happens when the user shortens the string that is being searched for?

    I did consider using a thread.
    Q1) is there any other way than thread??
    See above. Don't complicate things with threads unless you must.

    Q2) Is there a way to break the ownership of the QTableWidget and keep the pointers in "pItemlst" intact?
    How about:
    Qt Code:
    1. QList<QTableWidgetItem*> pItemlst = ui->tableWidget->findItems(stext, Qt::MatchContains);
    2. for (int i =0; i < pItemList.count(); ++i) {
    3. QTableWidgetItem* p = pItemlst.at(i);
    4. pItemList[i] = ui->tableWidget->takeItem(p->row(), p->column());
    5. }
    6. // now your list contains the same pointers but owns the items - don't forget to delete our reparent them.
    7. /// You can clear() the table widget.
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    deepal_de (7th May 2011)

  7. #5
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Remove ownership of QTableWidgetitem from Qtablewidget

    Thanks....

    and a little follow up question...

    "ui->tableWidget->takeItem" copy the item in the Qtablewidget to a new pointer right???
    in other words...

    Qt Code:
    1. QTableWidgetItem* pItem = ui->tableWidget->takeItem(p->row(), p->column());
    To copy to clipboard, switch view to plain text mode 
    is this same as
    Qt Code:
    1. pItem->setText( ui->tableWidget->takeItem(p->row(), p->column())->text() );
    2. pItem->setCheckState( ui->tableWidget->takeItem(p->row(), p->column())->text()->checkState() );
    To copy to clipboard, switch view to plain text mode 
    does this two have a big difference?? or is it like the same thing....
    Last edited by deepal_de; 7th May 2011 at 08:29.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Remove ownership of QTableWidgetitem from Qtablewidget

    Please use [code] [/code] tags around code

    No. The first option unparents the item and gives you a pointer to it. There is no copy. After this it is yours to do with as you please.

    Your second option fails for several reasons:
    Qt Code:
    1. // ^^^ will not compile, but you intend to allocate a new item on the heap
    2.  
    3. pItem->setText( ui->tableWidget->takeItem(p->row(), p->column())->text() );
    4. // ^^^ Copy the text of item pointed to by p into new item.
    5. // Remove item pointed to by p from table.
    6.  
    7. pItem->setCheckState( ui->tableWidget->takeItem(p->row(), p->column())->text()->checkState() );
    8. // ^^^ Intended to copy the check state of the item pointed at by p: fails
    9. // The row and column of p no longer return the original values because
    10. // *p is no longer in the table. They return -1 and takeItem(-1, -1) returns 0.
    11. // You will get a segfault because you attempt to dereference that null pointer.
    To copy to clipboard, switch view to plain text mode 
    In the end you have removed the item from the table, created a corrupt copy, and crashed the program. If you really want to create another copy of the item then use the QTableWidgetItem copy constructor and leave the original in the table.

  9. The following user says thank you to ChrisW67 for this useful post:

    deepal_de (7th May 2011)

Similar Threads

  1. QTableWidgetItem for a QTableWidget
    By Archa4 in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 11:11
  2. Replies: 0
    Last Post: 6th October 2010, 06:35
  3. QTableWidget or QTableWidgetItem CSS
    By ajayajgdeva in forum Newbie
    Replies: 0
    Last Post: 5th February 2010, 13:47
  4. QTableWidget+QTableWidgetItem trouble
    By Fastman in forum Qt Programming
    Replies: 13
    Last Post: 10th November 2007, 12:19
  5. QTableWidget QTableWidgetItem
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2006, 15:03

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.