#2
powerwheel2014-05-13 18:11
|
各位大神,我在tabview中使用一个代理,但是没起作用,就像没使用代理一样,下面是我的代码。
//创建界面的代码
viewGroup = new QGroupBox;
viewGroup->setFixedHeight( 350 );
view = new QTableView( viewGroup );
QStringList list;
list<<tr( "线路1" )<<tr( "状态1" )<<tr( "状态2" )<<tr( "结论" );
viewModel = new QStandardItemModel( 0, list.count( ), this );
// viewModel = new CustomModel(0,list.count(),this);
view->setModel( viewModel );
SexDelegate comboBoxDelegate1;
// view->setItemDelegate(&comboBoxDelegate);
view->setItemDelegateForColumn(1, &comboBoxDelegate1);
SexDelegate comboBoxDelegate2;
view->setItemDelegateForColumn(2, &comboBoxDelegate2);
SexDelegate comboBoxDelegate3;
view->setItemDelegateForColumn(3, &comboBoxDelegate3);
SexDelegate comboBoxDelegate0;
view->setItemDelegateForColumn(0, &comboBoxDelegate0);
view->setMinimumSize( 665, 350 );
view->horizontalHeader()->setStretchLastSection( true );
view->horizontalHeader()->setDefaultAlignment( Qt::AlignCenter );
viewModel->setHorizontalHeaderLabels( list );
view->verticalHeader( )->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
view->verticalHeader( )->setEnabled( false );
// view->setEditTriggers( QAbstractItemView::NoEditTriggers ); //设置为不可编辑2
view->setSelectionBehavior( QAbstractItemView::SelectRows );
view->setSelectionMode( QAbstractItemView::SingleSelection );
我的代理代码:
class StateDelegate : public QItemDelegate
{
Q_OBJECT
public:
SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("Open");
editor->addItem("Close");
return editor;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int tindex = comboBox->findText(text);
comboBox->setCurrentIndex(tindex);
}
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString text = comboBox->currentText();
model->setData(index, text, Qt::EditRole);
}
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
};