简体   繁体   中英

qt treewidget widget item QCheckBox alignment

I have a QTreeWidget where I insert different widgets ( QDoubleSpinBox , QSpinBox , QCheckBox ...)

QTreeWidget *t = ui->treeWidget;
QTreeWidgetItem *item = new QTreeWidgetItem();

int c = 0;
QDoubleSpinBox *dspb = new QDoubleSpinBox();
t->setItemWidget(item, c++, dspb);

QSpinBox *spb = new QSpinBox();
t->setItemWidget(item, c++, spb);

QCheckBox *cb = new QCheckBox();
t->setItemWidget(item, c++, cb);

t->addTopLevelItem(item);

However, the cb widget looks wired since the checkbox is aligned to the left. I would like to see it aligned in the center.

Q: How can I change the checkbox to appear in the middle of the TreeWidget cell?

I need to be able to access the cb item again later. Currently, I use the following code:

QTreeWidgetItem *itm = t->topLevelItem(0);
bool checked  = qobject_cast<QCheckBox *>(t->itemWidget(itm,c++))->checkState() == Qt::Checked;

If I need to do some workaround to get the central alignment going, how can I access the cb object then?

Found it:

cb->setStyleSheet("margin-left:50%; margin-right:50%;");

works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM