May 4, 2012

Octani Octani
Lab Rat
34 posts

Display problem with QCheckBox

 

Hello world !

I have a display problem. I explain :
I’m working on a QTableView and I want to put a QCheckBox in each cell. So I use the setIndexWidget() method.

The question is : What QWidget do I must to put : a QCheckBox directly or a QFrame which contains the QCheckBox?

Actually, I want to center the widget. So I use a QFrame.

  1. for(int i=0;i<m_pNbAnnees;i++)
  2. {
  3.     //columns
  4.     for(int j=0;j<3;j++)
  5.     {
  6.         QFrame* m_pTabFrame = new QFrame(m_pTab);
  7.         QVBoxLayout* m_pTabLayout = new QVBoxLayout();
  8.         m_pTabFrame->setLayout(m_pTabLayout);
  9.         QCheckBox* m_pTabCheckBox = new QCheckBox();
  10.         m_pTabLayout->addWidget(m_pTabCheckBox,0,Qt::AlignHCenter | Qt::AlignVCenter);
  11.  
  12.         m_pTab->setIndexWidget(m_pTabModel->item(i,j)->index(), m_pTabFrame);
  13.     }
  14. }

The problem is the QCheckBox is not diplayed correctly when I put it in a QFrame. Instead of a simple square, I get a kind of line like if the QCheckBox was in relief.

I really don’t understand what happens !

[Edit: Normalized white space in code snippet; mlong]

3 replies

May 4, 2012

Andre Andre
Area 51 Engineer
6031 posts

Better yet: neither of these. Item views are by default (through the default delegate) capable of displaying checkboxes. Just make sure your model returns the following:

  1. From the flags() method, it should return a result that contains the Qt::ItemIsUserCheckable flag, and
  2. From the data() method, it should return a Qt::CheckState when the Qt::CheckStateRole role is requested.

That will make your items checkable without fidling around with adding widgets to cells in your table. It depends on what model you use how you can best make sure of that. If you use a QStandardItemModel, there are methods exposed on the StandardModelItem that will set these (setCheckable() and setCheckState() respectively).

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 4, 2012

Octani Octani
Lab Rat
34 posts

Thanks for this complete answer Andre.

I tried the setCheckable() method. It works but the QCheckBox is not centered. And this option implies to put an item on cells. It’s not really what i need. The table I mean must be a kind of filter with only a QCheckBox in cell.

Edit : I wonder if it’s not a margin problem which masks a part of the checkbox.

May 4, 2012

Andre Andre
Area 51 Engineer
6031 posts

In that case, I would create a custom delegate that renders (only) the checkbox in the center of the cell instead. That really isn’t all that hard to do.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ how format a c like codes and ...      [SOLVED] multiple using qRegisterMetaType ››

You must log in to post a reply. Not a member yet? Register here!