Added an QStyledItemDelegate to create the methodwidget. This might fix some bugs.

This commit is contained in:
Micke Prag 2010-06-16 14:35:35 +00:00
parent 679cd847a8
commit d69bf5c5f8
2 changed files with 33 additions and 5 deletions

View file

@ -2,25 +2,36 @@
#include "methodwidget.h"
#include "devicemodel.h"
#include <QHeaderView>
#include <QStyledItemDelegate>
#include <QDebug>
class MethodDelegate : public QStyledItemDelegate {
public:
MethodDelegate( QWidget *parent = 0 );
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
DeviceView::DeviceView(QWidget *parent)
: QTableView(parent)
{
setAlternatingRowColors( true );
setShowGrid( false );
setSelectionBehavior( QAbstractItemView::SelectRows );
setSelectionMode( QAbstractItemView::SingleSelection );
horizontalHeader()->setStretchLastSection( true );
verticalHeader()->hide();
setItemDelegate(new MethodDelegate(this));
}
void DeviceView::setModel ( QAbstractItemModel * model ) {
QTableView::setModel( model );
rowsInserted(QModelIndex(), 0, model->rowCount()-1);
connect( model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)) );
rowsUpdated(QModelIndex(), 0, model->rowCount()-1);
connect( model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsUpdated(const QModelIndex &, int, int)) );
connect( model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(rowsUpdated(const QModelIndex &, int, int)) );
}
void DeviceView::rowsInserted ( const QModelIndex & /*parent*/, int start, int end ) {
void DeviceView::rowsUpdated ( const QModelIndex & /*parent*/, int start, int end ) {
DeviceModel *model = qobject_cast<DeviceModel*>( this->model() );
if (!model) {
return;
@ -28,6 +39,23 @@ void DeviceView::rowsInserted ( const QModelIndex & /*parent*/, int start, int e
for (int i = start; i <= end; ++i) {
QModelIndex index = model->index( i, 2, QModelIndex() );
this->setIndexWidget( index, new MethodWidget( model->device(index), this ) );
this->openPersistentEditor(index);
}
}
MethodDelegate::MethodDelegate( QWidget *parent )
:QStyledItemDelegate(parent)
{}
QWidget *MethodDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex &index) const {
DeviceView *p = qobject_cast<DeviceView *>(this->parent());
if (!p) {
return 0;
}
DeviceModel *model = qobject_cast<DeviceModel*>( p->model() );
if (!model) {
return 0;
}
MethodWidget *widget = new MethodWidget(model->device(index), parent);
return widget;
}

View file

@ -11,7 +11,7 @@ public:
virtual void setModel ( QAbstractItemModel * model );
private slots:
void rowsInserted ( const QModelIndex & parent, int start, int end );
void rowsUpdated ( const QModelIndex & parent, int start, int end );
};
#endif // DEVICEVIEW_H