Results 1 to 5 of 5

Thread: QTableWidget::itemFromIndex is protected, why? and a workaround?

  1. #1
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget::itemFromIndex is protected, why? and a workaround?

    I'm writing a delegate to do drawing for cells in a table. The delegate is provided the QModelIndex item and I would like to retrieve the QTableWidgetItem that is associated with it. The problem is that the itemFromIndex member of QTableWidget is protected.

    In one case I have a derived table, but in my generic case I use just the standard QTableWidget. Without deriving the class is there another way to get at the QTableWidgetItem from the QModelIndex?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableWidget::itemFromIndex is protected, why? and a workaround?

    what's about using QTableWidget::item()?

  3. #3
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget::itemFromIndex is protected, why? and a workaround?

    I am using "item( index.row(), index.column() )" right now. I'm just not sure if this is always guaranteed to be the same item as itemFromIndex, or whether it is more/less performant.

    I get nervous when something looks like it could be done another way...


    Added after 54 minutes:


    Okay, looking at the source the itemFromIndex validates the index and then just calls item(row,col). So I should be fine.
    Last edited by mortoray; 18th December 2010 at 18:58.

  4. #4
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget::itemFromIndex is protected, why? and a workaround?

    The workaround to this stupidity is:

    On the top:

    Qt Code:
    1. #define protected public
    2. #include <QTableWidget>
    To copy to clipboard, switch view to plain text mode 

    then:

    Qt Code:
    1. void DBs::on_table_doubleClicked(const QModelIndex &index)
    2. {
    3. on_table_itemDoubleClicked(ui->table->itemFromIndex(index));
    4. }
    To copy to clipboard, switch view to plain text mode 

    VOILA! Doing this in production code, by the way! ;] And I really HATE doing this, but....

    And by the way,
    Qt Code:
    1. on_table_itemDoubleClicked(ui->table->item(index.row(),index.column()));
    To copy to clipboard, switch view to plain text mode 

    is also an option, but in my case with a huge database the protected hack did 10 seconds time price from performance point of view (dataset with milliards of records).

  5. #5
    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: QTableWidget::itemFromIndex is protected, why? and a workaround?

    Quote Originally Posted by petersvp View Post
    And I really HATE doing this, but....
    Then don't do this.

    I don't know why you need such conversion but if you really want it, just extract the internal pointer from the index.

    Qt Code:
    1. QTableWidgetItem *item = (QTableWidgetItem*)index.internalPointer();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 9
    Last Post: 12th February 2011, 18:05
  2. workaround for messagebox modality bug
    By mortoray in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2010, 11:07
  3. Workaround for a Qt ui bug
    By eurodatar in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2010, 16:12
  4. Workaround for StandardItemModel drag drop bug
    By onamatic in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2008, 21:50
  5. QWidget::setMask: bug &/or help for workaround
    By ataffard in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 07:34

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.