Results 1 to 4 of 4

Thread: QTableWidgetItem: Headers and centering items

  1. #1
    Join Date
    Nov 2006
    Posts
    14
    Thanks
    1

    Default QTableWidgetItem: Headers and centering items

    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.

  2. #2
    Join Date
    Nov 2006
    Posts
    14
    Thanks
    1

    Wink Re: QTableWidgetItem: Headers and centering items

    Any help please?

  3. #3
    Join Date
    Nov 2006
    Posts
    14
    Thanks
    1

    Default QTableWidgetItem questions

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidgetItem: Headers and centering items

    1) I assume the checkboxes don't have any text.
    Do the following:
    Qt Code:
    1. /*******/
    2. QWidget *w = new QWidget(ui.tableWidget);
    3. QHBoxLayout *l = new QHBoxLayout(w);
    4. l->setSpacing(0);
    5. l->setMargin(0);
    6. l->addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed));
    7. QCheckBox* check = new QCheckBox(w);
    8. check->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
    9. l->addWidget(check);
    10. l->addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed));
    11. w->setLayout(l);
    12. /*******/
    13.  
    14. ui.tableWidget->setIndexWidget(ui.tableWidget->model()->index(0,0), w);
    To copy to clipboard, switch view to plain text mode 
    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
    Attached Images Attached Images

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.