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++)
{
//columns
for(int j=0;j<3;j++)
{
QFrame* m_pTabFrame = new QFrame(m_pTab);
QVBoxLayout* m_pTabLayout = new QVBoxLayout();
m_pTabFrame->setLayout(m_pTabLayout);
QCheckBox* m_pTabCheckBox = new QCheckBox();
m_pTabLayout->addWidget(m_pTabCheckBox,0,Qt::AlignHCenter | Qt::AlignVCenter);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 [[doc: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.
-
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.