Results 1 to 5 of 5

Thread: How to subclass QTableWidgetItem

  1. #1
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to subclass QTableWidgetItem

    Hi everyone.
    I've managed to compile a program that uses a subclass to QTableWidgetItem, but there are still some issues that needs to be sorted out.

    First here is the code
    Qt Code:
    1. class QInt64Item : public QTableWidgetItem
    2. {
    3. public:
    4. QInt64Item(qint64 data=0);
    5. QVariant data(int role) const;
    6. bool operator< ( const QInt64Item& other ) const;
    7. protected:
    8. qint64 m_data;
    9. };
    To copy to clipboard, switch view to plain text mode 
    And here is the rest of the code
    Qt Code:
    1. #include "qint64item.h"
    2.  
    3. QInt64Item::QInt64Item(qint64 data)
    4. {
    5. m_data = data;
    6. this->setFlags(Qt::NoItemFlags);
    7. this->setFlags(Qt::ItemIsEnabled);
    8. this->setSelected(false);
    9. }
    10.  
    11. QVariant QInt64Item::data(int role) const
    12. {
    13. if (role==Qt::EditRole)
    14. return m_data;
    15. else
    16. return QString::number(m_data);
    17. }
    18.  
    19. bool QInt64Item::operator< ( const QInt64Item& other ) const
    20. {
    21. return m_data < other.m_data;
    22. }
    To copy to clipboard, switch view to plain text mode 

    And here I use it
    Qt Code:
    1. QInt64Item* new64Item;
    2. new64Item = new QInt64Item(my_int64_variable);
    3. new64Item->setForeground(QBrush(Qt::black));
    4. m_ui->MyTable->setItem(row, 0, new64Item);
    To copy to clipboard, switch view to plain text mode 

    I've attached what it lookslike (dump.jpeg) but I should add that I want all text to be aligned to the right, no checkboxes, and sorting does not sort numerically when clicking in the header.

    So I'm very grateful for any hints on this issue
    Attached Images Attached Images
    Last edited by codemonkey; 4th October 2009 at 09:05. Reason: Email notification

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to subclass QTableWidgetItem

    Hi,

    You might want to start with revising Int64Item::data(). It makes no sense to return a QString for all the other roles than Qt::EditRole. There are such roles as Qt::SizeHintRole, Qt::CheckStateRole, Qt:ecorationRole, Qt::AlignmentRole etc... returning a QString for those might confuse Qt.
    J-P Nurmi

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

    codemonkey (4th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to subclass QTableWidgetItem

    Thanks, it starts to look much better now that I've changed the code to this:

    Qt Code:
    1. QVariant QInt64Item::data(int role) const
    2. {
    3. if (role==Qt::EditRole)
    4. return m_data;
    5. else if (role==Qt::CheckStateRole)
    6. return Qt::Unchecked;
    7. else if (role==Qt::DisplayRole)
    8. return m_data;
    9. else if (role==Qt::TextAlignmentRole)
    10. return Qt::AlignRight;
    11. else
    12. return QTableWidgetItem::data(role);
    13. }
    To copy to clipboard, switch view to plain text mode 

    But I cant find anything on how to get rid of the check box. My best guess is that it's a call to setFlags() but how?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to subclass QTableWidgetItem

    Try returning QVariant() for Qt::CheckStateRole (or just remove the Qt::CheckStateRole block). Also, notice that setFlags() doesn't work this way:
    Qt Code:
    1. item->setFlags(A);
    2. item->setFlags(B); // overrides whatever was previously set
    To copy to clipboard, switch view to plain text mode 
    But it takes a combination flags:
    Qt Code:
    1. item->setFlags(A | B);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    codemonkey (4th October 2009)

  7. #5
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to subclass QTableWidgetItem

    Thanks, removing the block solved it.

Similar Threads

  1. QListWidgetItem subclass - Crash program in drag/drop
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 09:24
  2. QSqlQueryModel write subclass
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:18
  3. Replies: 1
    Last Post: 21st August 2007, 16:25
  4. Interesting little Segfault w/r to signal/slot connection
    By Hydragyrum in forum Qt Programming
    Replies: 24
    Last Post: 12th September 2006, 19:22
  5. QFrame subclass does not reparent
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2006, 22:15

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.