Results 1 to 5 of 5

Thread: Checkbox QTableWidgetItem

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Checkbox QTableWidgetItem

    I've been banging my head against a wall of QTableWidget problems, but I'm thankfully very hardheaded and the wall is starting to give. I've figured out most of my issues, but now I have a small issue:

    How can I make a QTableWidgetItem have no text? All I want is a checkbox. The first thing I tried was to not use Items, but a QCheckBox inserted with QTableWidget::setCellWidget(). That worked quite well, but I came upon a few issues with it. The main being that I couldn't find a way of retrieving the box's state through the QTableWidget, since the QTableWidget::cellWidget() function returns a QWidget*and I can't figure out how to get a QCheckBox's state having only access to QWidget functions.

    The other issue was the same as I now have with QTableWidgetItem. I can't make it stay centralized in its parent cell, because it has an obligatory empty text-box next to it (in QTableWidgetItem's case. With QCheckbox, there simply isn't a relevant function to do so). As well, QTableWidgetItem::setTextAlignment() is, as the name implies, only in regards to the text, not the checkbox itself.

    So, is there a way of setting a ItemHasNoText flag or something? And is it possible to centralize the checkbox? This isn't a huge issue, really more aesthetic than anything, but still, a solution would be more than appreciated.
    Last edited by Wasabi; 23rd November 2010 at 14:05.

  2. #2
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Checkbox QTableWidgetItem

    Quote Originally Posted by Wasabi View Post
    I've been banging my head against a wall of QTableWidget problems, but I'm thankfully very hardheaded and the wall is starting to give. I've figured out most of my issues, but now I have a small issue:

    How can I make a QTableWidgetItem have no text? All I want is a checkbox. The first thing I tried was to not use Items, but a QCheckBox inserted with QTableWidget::setCellWidget(). That worked quite well, but I came upon a few issues with it. The main being that I couldn't find a way of retrieving the box's state through the QTableWidget, since the QTableWidget::cellWidget() function returns a QWidget*and I can't figure out how to get a QCheckBox's state having only access to QWidget functions.
    you can get checked state of QCheckBox object with something like this:
    Qt Code:
    1. // table is your QTableWidget* object
    2. QCheckBox *cellCheckBox = qobject_cast<QCheckBox *>(table->cellWidget());
    3. if (cellCheckBox)
    4. {
    5. Qt::CheckState state = cellCheckBox->checkState();
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to srazi for this useful post:

    ahmetturan (25th October 2011), Wasabi (24th November 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checkbox QTableWidgetItem

    Ah. Touché. Yes, that would work. However, I've changed it to a checkable QTableWidgetItem with no text, which achieves the same function while keeping things simpler. But knowing I can simply cast a parent class into a child class is mighty handy. So thanks.

    The only problem that remains is that with both an inserted QCheckBox and a purely checkable QTableWidgetItem, I can't centralize them in the cell. Any hints for that?

  5. #4
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Checkbox QTableWidgetItem

    I think this works (I don't try it):
    You need to subclassing "QStyledItemDelegate" (or "QItemDelegate") and reimplementing its "paint" function, example:
    Qt Code:
    1. //MyItemDelegate is subclass of QStyledItemDelegate
    2. void MyItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    3. {
    4. QStyleOptionViewItem opt = option;
    5. opt.decorationAlignment = Qt::AlignCenter;
    6. QStyledItemDelegate::paint(painter, opt, index );
    7. }
    8.  
    9. //your app body
    10. table->setItemDelegate(new MyItemDelegate);
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jul 2017
    Posts
    2
    Qt products
    Platforms
    Windows

    Default Re: Checkbox QTableWidgetItem

    // table is your QTableWidget* object
    QCheckBox *cellCheckBox = qobject_cast<QCheckBox *>(table->cellWidget());
    if (cellCheckBox)
    {
    Qt::CheckState state = cellCheckBox->checkState();
    }

Similar Threads

  1. integer as a QTableWidgetItem ?
    By tommy in forum Qt Programming
    Replies: 7
    Last Post: 29th May 2009, 17:44
  2. help on QList<QTableWidgetItem*>
    By roncriss in forum Newbie
    Replies: 2
    Last Post: 24th November 2008, 09:19
  3. QTableWidgetItem Text
    By pytro in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2007, 21:44
  4. Im became fool with QTableWidgetItem
    By zorro68 in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2007, 12:17
  5. Reference QTableWidgetItem
    By regenbiegen in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2007, 11:34

Tags for this Thread

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.