PDA

View Full Version : QTableWidgetItem: Headers and centering items



thebra
2nd August 2007, 07:13
Hi all,
I have a QTableWidget (QT 4.2) which contains checkboxs. I have two questions:
1) Is there a way of centering the checkboxes in each cell.By default they are all aligned to the left.
2) How can I add a combobox, checkbox to the horizontal headers of the table?
Thanks in advance for any help.

thebra
3rd August 2007, 20:52
Any help please?

thebra
5th August 2007, 22:20
Hi all,
I have a QTableWidget (QT 4.2) which contains checkboxs. I have two questions:
1) Is there a way of centering the checkboxes in each cell.By default they are all aligned to the left.
2) How can I add a combobox, checkbox to the horizontal headers of the table?
Thanks in advance for any help.

marcel
6th August 2007, 01:00
1) I assume the checkboxes don't have any text.
Do the following:


/*******/
QWidget *w = new QWidget(ui.tableWidget);
QHBoxLayout *l = new QHBoxLayout(w);
l->setSpacing(0);
l->setMargin(0);
l->addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed));
QCheckBox* check = new QCheckBox(w);
check->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
l->addWidget(check);
l->addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed));
w->setLayout(l);
/*******/

ui.tableWidget->setIndexWidget(ui.tableWidget->model()->index(0,0), w);

So instead of just a checkbox you should add a custom widget, consisting of a horizontal layout a left spacer, the checkbox and a right spacer. The spacers are there to keep the checkbox centered.

2) No answer. I'm afraid that is pretty hard to do.
AFAIK there is a custom widget(commercial) that does that. I've seen it mentioned in a post(here, on QtCentre) some time a go.


BTW, regarding 1, the checkbox is not 100% centered because even with no text it still keeps a few pixels at its right side. You can notice that if you call setAutofillBackground(true) for the checkbox.

Regards