QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Hi Guys,-
Its pretty simple to add a checbox into a table widget, but the problem is once adding, the checkbox seems to live on the cell side by side with a QlineEdit(which i dont need).
I can actually disable the QLineEdit, but im trying to aling the checkboxes to be centered, and it will simply not change its position because it still "sees" the lineedit beside it.
IIs there a way to properly remove the LineEdit from a cell on QTableWidget?
appreciate any help,
thanks!
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Just a question, why do you want to remove the QLineEdit?
1 Attachment(s)
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Jano,
thanks for your quick reply.
for 2 reasons:
1. because for that cell in specific, i just need a checkbox and no text input.
2. And i would like the checkbox to nicely align to the center of the cell (right now, because of the invisible lineedit it will simply be stuck on the left side)
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
one idea... maybe setting the text as "" could do the trick.
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
cheers for that -
but being a complete newbie, let me just risk asking a dumb question: is this do-able within Qt Designer?
(i 'm a graphic designer and mostly using Designer or occasionaly QtCreator)
thanks,
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
I don't know if there are any other ways, but you can subclass QTableWidgetItem and create your own Items; for instance, one with the checkbox and no text at all.
But, in my opinion, if you can "cheat" a little bit (like setting and empty string) and solve your problems that way.. better :P So far I didn't need to subclass QTableWidgetItem.
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Hi there, I'm a beginner...
I'm searching for how to add a button into a table, in a cell, then I found your post....:o
Could you tell me how you add a checkbox as a QTableWidgetItem ??
Thanks....
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Quote:
Originally Posted by
b_ginner
Hi there, I'm a beginner...
I'm searching for how to add a button into a table, in a cell, then I found your post....:o
Could you tell me how you add a checkbox as a QTableWidgetItem ??
Thanks....
Nevermind, found that already
Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit
Hi Erik,
I don't know if it is the best solution, but I've found a solution!
I have subclassed QItemDelegate and only overwrite the paint()-method.
The implementation of the paint()-method I've "borrowed" from QItemDelegate::paint().
I only commented out a few lines which refer to QItemDelegatePrivate-class and added a rectangle movement to center the checkbox.
class CheckBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
CheckBoxDelegate( QObject* pParent = 0 ) : QItemDelegate( pParent ) {}
virtual ~CheckBoxDelegate() {}
void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
};
void CheckBoxDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
//Q_D(const QItemDelegate);
Q_ASSERT(index.isValid());
QStyleOptionViewItemV4 opt = setOptions(index, option);
const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 * >(&option);
opt.features = v2 ? v2->features
: QStyleOptionViewItemV2::ViewItemFeatures(QStyleOpt ionViewItemV2::None);
const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 * >(&option);
opt.locale = v3 ? v3->locale : QLocale();
opt.widget = v3 ? v3->widget : 0;
// prepare
painter->save();
if( hasClipping() )
{
painter->setClipRect( opt.rect );
}
// get the data and the rectangles
QVariant value;
QPixmap pixmap;
QRect decorationRect;
// IN THIS SPECIAL CASE: no decoration (icon) required -> commented dispayRect calculation
//value = index.data(Qt::DecorationRole);
//if (value.isValid()) {
// // ### we need the pixmap to call the virtual function
// pixmap = decoration(opt, value);
// if (value.type() == QVariant::Icon) {
// d->tmp.icon = qvariant_cast<QIcon>(value);
// d->tmp.mode = d->iconMode(option.state);
// d->tmp.state = d->iconState(option.state);
// const QSize size = d->tmp.icon.actualSize(option.decorationSize,
// d->tmp.mode, d->tmp.state);
// decorationRect = QRect(QPoint(0, 0), size);
// } else {
// d->tmp.icon = QIcon();
// decorationRect = QRect(QPoint(0, 0), pixmap.size());
// }
//} else {
// d->tmp.icon = QIcon();
// decorationRect = QRect();
//}
QString text;
QRect displayRect;
// IN THIS SPECIAL CASE: no text required -> commented dispayRect calculation
//value = index.data(Qt::DisplayRole);
//if (value.isValid() && !value.isNull()) {
// text = QItemDelegatePrivate::valueToText(value, opt);
// displayRect = textRectangle(painter, textLayoutBounds(opt), opt.font, text);
//}
QRect checkRect;
Qt::CheckState checkState = Qt::Unchecked;
value = index.data(Qt::CheckStateRole);
if (value.isValid()) {
checkState = static_cast<Qt::CheckState>(value.toInt());
checkRect = check(opt, opt.rect, value);
}
// do the layout
doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
// move checkBox to the center of the column
checkRect.moveRight( ( opt.rect.width() + checkRect.width() ) / 2.0 );
// draw the item
drawBackground(painter, opt, index);
drawCheck(painter, opt, checkRect, checkState);
// no decoration, no text and no focus -> no need to draw!!!
//drawDecoration(painter, opt, decorationRect, pixmap);
//drawDisplay(painter, opt, displayRect, text);
//drawFocus(painter, opt, displayRect);
// done
painter->restore();
}
To set CheckBoxDelegate() for the first column of the tableWidget,
you only need to add the following line:
tableWidget->setDelegateItemForColumn( 0, new CheckBoxDelegate() );
Good luck!