Painting QCheckbox into QTableView
Hello, I believe I have read through most of the posts on QCheckbox's and QTableviews. It seems most people prefer to use a custom delegate which makes sense. I have used the spinboxdelegate as an example and I was successful in getting the QCheckbox to draw when entering the editrole of the table view, however when not in the edit role (i.e. paint()) function I have not yet figured out how to successfully draw/render the QCheckbox. Here are my two questions with code to follow:
1. How can i make it so that a single click will toggle the checkbox as one would expect, instead of a double click to start editing and then a third click to toggle the checkbox?
2. What am I doing wrong in my paint function?
Thanks in advance!
Code:
{
bool Checked = index.model()->data(index, Qt::EditRole).toBool();
CheckBox.setChecked(Checked);
CheckBox.render(painter);
}
{
return CheckBox.sizeHint();
}
{
return editor;
}
void cCheckboxDelegate
::setEditorData(QWidget *editor,
{
bool checked = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox *checkBox
= static_cast<QCheckBox
*>
(editor
);
checkBox->setChecked(checked);
}
{
QCheckBox *checkBox
= static_cast<QCheckBox
*>
(editor
);
bool checked = checkBox->isChecked();
model->setData(index, checked, Qt::EditRole);
}
void cCheckboxDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
Re: Painting QCheckbox into QTableView
Ok I got it to display the checkbox using the following code. However like I mentioned previous how would i make it so you don't have to triple click the checkbox to change the state?
Thanks
Code:
{
BtnStyle.
state = QStyle::State_Enabled;
if(index.model()->data(index, Qt::DisplayRole).toBool() == true)
BtnStyle.
state |
= QStyle::State_On;
else
BtnStyle.
state |
= QStyle::State_Off;
BtnStyle.rect = option.rect;
}
Re: Painting QCheckbox into QTableView
Re: Painting QCheckbox into QTableView
Yes however I'm not using the StandItem, I'm subclassing QAbstractTableModel.
Anyways I think I found the best solution as far as user interface goes. If I edit my Model and set the flag to Qt::ItemIsUserCheckable and then respond to the Qt::CheckStateRole in the setData and data() functions the checkbox works as expected. I.E. you don't have to first double click into the cell before the editor widget pops up, you simply just click the checkbox and your done.
Here is some good sample code I used
http://www.qtcentre.org/threads/2965...sUserCheckable