PDA

View Full Version : adding radio button in listview



phillip_Qt
5th July 2013, 08:41
Dear experts,

i ve created a sample application to display radio button in list view. each item in list view should contain a radio button, an icon and text. I'm using a delegate class to do so.
hut it is not displaying in the view.

please tell em what i need to correct in below code.


delegate.cpp
#include "radiobtndelegates.h"


RadioButtonDelegate::RadioButtonDelegate(QObject *parent) : QItemDelegate(parent)
{
};

RadioButtonDelegate::~RadioButtonDelegate(){

};

QWidget* RadioButtonDelegate::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 RadioButtonDelegate::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 RadioButtonDelegate::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());
};



delegate.h

class RadioButtonDelegate : public QItemDelegate
{
Q_OBJECT

public:
RadioButtonDelegate(QObject *parent = 0) ;

virtual ~RadioButtonDelegate();

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;



void setEditorData(QWidget *editor, const QModelIndex &index) const;

void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;

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

};


Dialog.h
class dialog : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
explicit dialog(QWidget *parent = 0);

QStandardItemModel * _model;
signals:

public slots:

};



dialog.cpp
dialog::dialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);

_model= new QStandardItemModel ();

RadioButtonDelegate* itemDelegate = new RadioButtonDelegate(listView);
listView->setItemDelegate( itemDelegate );

listView->setModel(_model);

//creating and adding data to model
QStandardItem* item = new QStandardItem;
item->setText("testing");

QStandardItem* item1 = new QStandardItem;
item1->setText("testing1");

_model->appendRow(item);
_model->appendRow(item1);
}


Thank u all in advance.

anda_skoa
5th July 2013, 12:19
The editor is only used when the given cell is being edited.

You might have to overwrite protected void drawCheck() to delegate to the style's radio button drawing instead of the checkbox drawing.
(assuming your on/off value is presented using the check state role)

Cheers,
_

phillip_Qt
5th July 2013, 13:33
The editor is only used when the given cell is being edited.

You might have to overwrite protected void drawCheck() to delegate to the style's radio button drawing instead of the checkbox drawing.
(assuming your on/off value is presented using the check state role)

Cheers,
_

Thanks Anda_skoa.

Do u mean i should override drawCheck() instead of createEditor()? Do i have to implement paint() method also inside my delegate class?

anda_skoa
5th July 2013, 19:11
Maybe in addition to createEditor, not sure.

I don't think you need to implement paint itself, I think it calls the draw methods so to make it easier to only customize certain things.

Haven't tried anything myself, this was mostly where I would look first :)

Cheers,
_

phillip_Qt
8th July 2013, 09:07
Dear All,

Please help me. I'm stuck with this problem. I'm not sure whether I've to draw a radio button in delegate class paint event or to create in seteditor event.

I want to create radiobutton in listview dynamically accoring to the input. E.g. If i gie a input string "row1" listview should add a row with raoibutton having text "row1".

Please help me.