Results 1 to 5 of 5

Thread: QTableWidgetItem and setCheckState

  1. #1
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTableWidgetItem and setCheckState

    Hi guys,
    I’m using a QTableWidget structure in which the first column is used as a check box.
    Here the code:

    for (int row_i=0; row_i < rows; ++row_i)
    {
    for (int column_j=0; column_j < 15; column_j++)
    {
    QTableWidgetItem *item = new QTableWidgetItem;
    item->setText("");
    item->setTextAlignment (Qt::AlignCenter);
    tableWidget->setItem(row_i, column_j, item);
    }
    }

    Then, for each row:

    tableWidget->item(row, 0)->setCheckState ( Qt::Checked );

    the problem is that when I select with the mouse the cell (first column for each row) besides the little square you can check or uncheck (precisely on the right side) it appears a rectangle about a text area..(see the image attached)
    How can I get rid of this ?
    Many thanks,
    robby77

    Immagine.JPG

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidgetItem and setCheckState

    Hi,

    that rect is drawn because the item has focus. Because of your thread I noticed that I have the same problem with something similar I've done, so thanks
    I tried to get rid of it with item flags, but that didn't work. If you take out ItemIsSelectable the item won't be highlighted when clicking on it, but the rect will still be drawn. Taking out ItemIsEnabled prohibits the user from clicking the checkbox.

    I guess the easiest solution would be to create a QStyledItemDelegate for that column, reimplement initStyleOption and do this:
    Qt Code:
    1. QStyleOptionViewItemV4 *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *>(option);
    2. if (v4->state.testFlag(QStyle::State_HasFocus)) {
    3. v4->state ^= QStyle::State_HasFocus;
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidgetItem and setCheckState

    Hi,
    and thank you for your reply.
    I've never manage with that stuff.. Can you provide me with the example about how to exploit your trick in my case ??
    Have a nice day

  4. #4
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidgetItem and setCheckState

    My solution:
    in .h files:
    class StyledItemDelegate : public QStyledItemDelegate
    {
    public:
    StyledItemDelegate(QObject *parent = NULL) :
    QStyledItemDelegate(parent) {}
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    QStyleOptionViewItem opt = option;
    opt.state &= ~QStyle::State_HasFocus;
    QStyledItemDelegate:aint(painter, opt, index);
    }
    };
    then in your code you have:

    tableWidget->setItemDelegate(new StyledItemDelegate(this));

  5. #5
    Join Date
    Oct 2016
    Posts
    1
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableWidgetItem and setCheckState

    Old post, but here's a simpler solution:

    Qt Code:
    1. tableWidget->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2011, 06:35
  2. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2011, 07:59
  3. QTableWidgetItem no text
    By Aslan in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2011, 03:53
  4. QTableWidgetItem not selectable
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2008, 18:15
  5. Tooltips in QTableWidgetItem
    By Arthur in forum Newbie
    Replies: 2
    Last Post: 26th January 2006, 15:47

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.