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.
- for(int i=0;i<m_pNbAnnees;i++)
- for(int j=0;j<3;j++)
- m_pTab->setIndexWidget(m_pTabModel->item(i,j)->index(), m_pTabFrame);
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]
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:
- From the flags() method, it should return a result that contains the Qt::ItemIsUserCheckable flag, and
- 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).
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.