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(){
};
{
Q_UNUSED(option);
Q_UNUSED(index);
editor->setAutoFillBackground(true);
connect(editor, SIGNAL(toggled(bool)), this, SLOT(emitCommitData()));
return editor;
};
{
if(qVariantCanConvert<bool>(index.data())){
bool value = index.data().toBool();
radio->setChecked(value);
}
};
{
Q_UNUSED(model);
qDebug() << radio->isChecked(); // returns true and false per click
model->setData(index, radio->isChecked());
};
delegate.h
{
Q_OBJECT
public:
RadioButtonDelegate
(QObject *parent
= 0) ;
virtual ~RadioButtonDelegate();
// private slots:
// void emitCommitData(){
// emit commitData(qobject_cast<QWidget *>(sender()));
// }
};
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()));
// }
};
To copy to clipboard, switch view to plain text mode
Dialog.h
class dialog
: public QDialog,
public Ui
::Dialog{
Q_OBJECT
public:
explicit dialog
(QWidget *parent
= 0);
signals:
public slots:
};
Dialog.h
class dialog : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
explicit dialog(QWidget *parent = 0);
QStandardItemModel * _model;
signals:
public slots:
};
To copy to clipboard, switch view to plain text mode
dialog.cpp
{
setupUi(this);
RadioButtonDelegate* itemDelegate = new RadioButtonDelegate(listView);
listView->setItemDelegate( itemDelegate );
listView->setModel(_model);
//creating and adding data to model
item->setText("testing");
item1->setText("testing1");
_model->appendRow(item);
_model->appendRow(item1);
}
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);
}
To copy to clipboard, switch view to plain text mode
Thank u all in advance.
Bookmarks