Взял делегат из примера:
http://doc.crossplatform.ru/qt/4.5.0/model-view-delegate.htmlВ программе при двойном щелчке мыши, spinbox в первом столбце не появляется, что я делаю неправильно?
MainWindow.h:
#pragma once
#include <QtGui>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include "SpinBoxDelegate.h"
class CMainWindow:public QMainWindow
{
public:
CMainWindow(QString path);
~CMainWindow(void);
private:
QWidget *mainWidget;
QGridLayout *mainLayout;
QSqlQueryModel *querymodel;
QTableView *tableview;
QSqlDatabase *db;
};
MainWindow.cpp
#include "MainWindow.h"
CMainWindow::CMainWindow(QString path)
{
bool ok;
mainWidget=new QWidget();
this->setCentralWidget(mainWidget);
mainLayout=new QGridLayout();
mainWidget->setLayout(mainLayout);
db=new QSqlDatabase();
(*db) = QSqlDatabase::addDatabase("QODBC");
db->setDatabaseName(QString("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=") +path+QString("\\books.mdb"));
ok=db->open();
QSqlQueryModel *model = new QSqlQueryModel(0);
model->setQuery("SELECT num_area,author,bookname from books");
model->setHeaderData(0, Qt::Horizontal, tr("num_area"));
model->setHeaderData(1, Qt::Horizontal, tr("author"));
model->setHeaderData(2, Qt::Horizontal, tr("bookname"));
tableview=new QTableView(0);
tableview->setModel(model);
tableview->show();
SpinBoxDelegate *spinboxdelegate=new SpinBoxDelegate(tableview);
tableview->setItemDelegateForColumn(0,spinboxdelegate);
mainLayout->addWidget(tableview);
}
CMainWindow::~CMainWindow(void)
{
}
SpinBoxDelegate.h
#include <QtGui>
class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
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;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
SpinBoxDelegate.cpp
#include "SpinBoxDelegate.h"
SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : QItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}