QTableWidget: center a checkbox inside a cell

QTableWidget is a very good control for show and manage data in a table format. It allow to insert inside cells different type of controls like listbox, checkbox and so on. In this post we'll discuss about the use of a checkbox control inside a cell with no text and the centering problem.



QTableWidget accept as standard cell content manager the QTableWidgetItem object. This object already have a settings for manage a checkbox control. You can insert a checkbox in a standard way by using the following code:

QTableWidgetItem *pItem = new QTableWidgetItem();
pItem->setCheckState(Qt::Checked);
pMyTableWidget->setItem(0,0,pItem);

As you can see in the image below the checkbox is showed but it is on the left side since the original idea is to have a checkbox and some text. If you don't provide any text the space is leaved empty.


Now if you are interested to have a cell with inside a checkbox control only (without any text) this way to paint the cell is not very good. The first solution to fix this problem is to create your own delegate object in charge to manage the centered paint of checkbox inside cell area. Is not a very difficult task and is possible to find a lot of working examples around. However put additional code just for center a control inside an area is not the best. Fortunately is there a workaround allowing to reach the same result but using only standard Qt widgets. The code is the following:

QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
pMyTableWidget->setCellWidget(0,0,pWidget);

You can see the result below:



The checkbox is centered. Hope that this will help.

Comments

  1. Congratulations by the article. Do you know if possible to implement it using QTableView class?

    ReplyDelete
  2. QTableWidget is a derived object of QTableView that I guess it will be the same...

    ReplyDelete
  3. You guess wrong: the method is unavailable sadly.

    ReplyDelete
  4. Thanks alot!

    I use Qt Jambi and here my adaptation for QItemDelegate for QTableView editor

    QWidget pWidget = new QWidget(parent);
    QCheckBox editorcb = new QCheckBox();
    pWidget.setStyleSheet("background-color:white;");
    QHBoxLayout pLayout = new QHBoxLayout(pWidget);
    pLayout.addWidget(editorcb);
    pLayout.setAlignment(new Alignment(Qt.AlignmentFlag.AlignCenter));
    pLayout.setContentsMargins(10,0,0,0);
    pWidget.setLayout(pLayout);

    It also place editor in center :)

    ReplyDelete
  5. Thanks, this method works. However I don't understand how to adress the checkbox. Does anybody know?

    ReplyDelete
  6. Don't understand very well what you mean with "address" but, I guess, you could check this other post. Control used was a button but you can substitute with checkbox easily:

    http://falsinsoft.blogspot.com/2014/06/qt-get-click-event-from-button-inside.html

    ReplyDelete
  7. pWidget->setLayout(pLayout);

    is not needed since you already added the layout to pWidget with:

    QHBoxLayout *pLayout = new QHBoxLayout(pWidget);

    Other than that, thanks!

    ReplyDelete
  8. QTableView version:


    QModelIndex index = tableView->model()->index(0, 0);
    QWidget *centerdCheckBoxWidget = new QWidget();
    QCheckBox *checkBox = new QCheckBox();
    QHBoxLayout *checkBoxLayout = new QHBoxLayout(centerdCheckBoxWidget);
    checkBoxLayout->addWidget(checkBox);
    checkBoxLayout->setAlignment(Qt::AlignCenter);
    checkBoxLayout->setContentsMargins(0, 0, 0, 0);
    centerdCheckBoxWidget->setLayout(checkBoxLayout);
    tableView->setIndexWidget(index, centerdCheckBoxWidget);

    Thanks for the tip.

    ReplyDelete
  9. How do you check if the checkbox has been checked?

    ReplyDelete
    Replies
    1. Best way is to use QSignalMapper object for associate checkbox click event to a generic event function collectiong all checkboxes in the table widget.

      Delete
    2. how to do that ? can you give me an example ? pls

      Delete
    3. Just check the official QSignalMapper documentation. There are examples explanining the basic use.

      Delete
  10. Thanks!!! This works for me into PySide2

    ReplyDelete
  11. how can i check checkbox clicked or not???

    ReplyDelete
  12. Thank you. But really they could make a QTableWidgetItem() alignment possible, it would be a lot simpler...

    ReplyDelete

Post a Comment

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model