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.
Re: Checkbox QTableWidgetItem
Quote:
Originally Posted by
Wasabi
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:
Code:
// table is your QTableWidget* object
QCheckBox *cellCheckBox
= qobject_cast<QCheckBox
*>
(table
->cellWidget
());
if (cellCheckBox)
{
Qt::CheckState state = cellCheckBox->checkState();
}
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?
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:
Code:
//MyItemDelegate is subclass of QStyledItemDelegate
{
opt.decorationAlignment = Qt::AlignCenter;
QStyledItemDelegate::paint(painter, opt, index );
}
//your app body
table->setItemDelegate(new MyItemDelegate);