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();

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

You can see the result below:

The checkbox is centered. Hope that this will help.


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

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

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

  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();
    QHBoxLayout pLayout = new QHBoxLayout(pWidget);
    pLayout.setAlignment(new Alignment(Qt.AlignmentFlag.AlignCenter));

    It also place editor in center :)

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

  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:


  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!

  8. QTableView version:

    QModelIndex index = tableView->model()->index(0, 0);
    QWidget *centerdCheckBoxWidget = new QWidget();
    QCheckBox *checkBox = new QCheckBox();
    QHBoxLayout *checkBoxLayout = new QHBoxLayout(centerdCheckBoxWidget);
    checkBoxLayout->setContentsMargins(0, 0, 0, 0);
    tableView->setIndexWidget(index, centerdCheckBoxWidget);

    Thanks for the tip.


Post a Comment

Popular posts from this blog

Access GPIO from Linux user space

Launch an app from Android shell terminal

Qt 5 and " Failed to load platform plugin 'windows' " error