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.

9 Comments:

thanks for code very useful for me

how to make cell size bigger ?

Themes blogger

Anonymous
December 10, 2013 at 10:04 AM comment-delete

¡Gracias!

Anonymous
December 27, 2013 at 8:18 AM comment-delete

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

Anonymous
February 14, 2014 at 12:43 AM comment-delete

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

February 15, 2014 at 2:12 AM comment-delete

You guess wrong: the method is unavailable sadly.

Anonymous
March 13, 2014 at 6:39 AM comment-delete

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 :)

Anonymous
July 14, 2014 at 3:23 PM comment-delete

Thank you!

Anonymous
September 10, 2014 at 6:36 AM comment-delete

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

October 19, 2014 at 8:48 AM comment-delete

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

October 19, 2014 at 11:08 AM comment-delete

Post a Comment