PDA

View Full Version : QItemDelegate with QRadioButton improper behaviour if QStandarditemModel has one row



toglia3d
29th September 2010, 23:59
This is somewhat hard to explain. This is the deal:
I have a QStandarditemModel that needs a single selection some specific column, therefore I inserted a radio button on every row. Only one row must always be selected, just like the radio button group behavior.

I did a QItemDelegate with a QRadioButton editor and magically I got the radio button behavior in the column, just like I needed. But If I only have one row on the model, I get a check box behavior, meaning its possible to select and unselect the radio button each time I click it. I don't really know if this is a normal, in that case what can I do to always have one radio button selected even if its alone.


class TextureRadioDelegate : public QItemDelegate{
Q_OBJECT

public:
TextureRadioDelegate(QObject *parent = 0) : QItemDelegate(parent){
};

virtual ~TextureRadioDelegate(){

};

QWidget *createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const {
Q_UNUSED(option);
Q_UNUSED(index);
QRadioButton* editor = new QRadioButton(parent);
editor->setAutoFillBackground(true);
connect(editor, SIGNAL(toggled(bool)), this, SLOT(emitCommitData()));
return editor;
};

void setEditorData(
QWidget *editor,
const QModelIndex &index
) const {
if(qVariantCanConvert<bool>(index.data())){
QRadioButton* radio = static_cast<QRadioButton*>(editor);
bool value = index.data().toBool();
radio->setChecked(value);
}
};

void setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index
) const {
Q_UNUSED(model);
QRadioButton* radio = static_cast<QRadioButton*>(editor);
qDebug() << radio->isChecked(); // returns true and false per click
model->setData(index, radio->isChecked());
};

private slots:
void emitCommitData(){
emit commitData(qobject_cast<QWidget *>(sender()));
}

};

Could it be a bug?