首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Qt报表中以旋转框的形式数据交互

2012-08-25 
Qt表格中以旋转框的形式数据交互?The Spin Box Delegate example shows how tocreate an editor for a cus

Qt表格中以旋转框的形式数据交互

?

The Spin Box Delegate example shows how tocreate an editor for a custom delegate in the model/view framework by reusing astandard Qt editor widget.

The model/view framework provides astandard delegate that is used by default with the standard view classes. Formost purposes, the selection of editor widgets available through this delegateis sufficient for editing text, boolean values, and other simple data types.However, for specific data types, it is sometimes necessary to use a customdelegate to either display the data in a specific way, or allow the user toedit it with a custom control.

Qt报表中以旋转框的形式数据交互

This concepts behind this example arecovered in the?Delegate Classes?chapter of the?Model/View Programming?overview.

SpinBoxDelegate Class Definition

The definition of the delegate is asfollows:

?classSpinBoxDelegate : 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;

?};

The delegate class declares only thosefunctions that are needed to create an editor widget, display it at the correctlocation in a view, and communicate with a model. Custom delegates can alsoprovide their own painting code by reimplementing the?paintEvent()?function.

SpinBoxDelegate Class Implementation

Since the delegate is stateless, theconstructor only needs to call the base class's constructor with theparent?QObject?as its argument:

?SpinBoxDelegate::SpinBoxDelegate(QObject*parent)

???? :QItemDelegate(parent)

?{

?}

Since the delegate is a subclass of?QItemDelegate, the data it retrieves from the model isdisplayed in a default style, and we do not need to provide a custom?paintEvent().

The?createEditor()?function returns an editor widget, in this case aspin box that restricts values from the model to integers from 0 to 100inclusive.

?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;

?}

We install an event filter on the spin boxto ensure that it behaves in a way that is consistent with other delegates. Theimplementation for the event filter is provided by the base class.

The?setEditorData()?function reads data from the model, converts it toan integer value, and writes it to the editor widget.

?voidSpinBoxDelegate::setEditorData(QWidget *editor,

???????????????????????????????????? constQModelIndex &index) const

?{

????int value = index.model()->data(index, Qt::EditRole).toInt();

?

????QSpinBox *spinBox = static_cast<QSpinBox*>(editor);

????spinBox->setValue(value);

?}

Since the view treats delegates asordinary?QWidget?instances, we have to use a staticcast before we can set the value in the spin box.

The?setModelData()?function reads the contents of the spin box, andwrites it to the model.

?voidSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,

???????????????????????????????????constQModelIndex &index) const

?{

????QSpinBox *spinBox = static_cast<QSpinBox*>(editor);

????spinBox->interpretText();

????int value = spinBox->value();

?

????model->setData(index, value, Qt::EditRole);

?}

We call?interpretText()?to make sure that we obtain the mostup-to-date value in the spin box.

The?updateEditorGeometry()?function updates the editor widget'sgeometry using the information supplied in the style option. This is theminimum that the delegate must do in this case.

?voidSpinBoxDelegate::updateEditorGeometry(QWidget *editor,

????const QStyleOptionViewItem &option, const QModelIndex &/* index*/) const

?{

????editor->setGeometry(option.rect);

?}

More complex editor widgets may divide therectangle available in?option.rect?between different child widgets ifrequired.

The Main Function

This example is written in a slightlydifferent way to many of the other examples supplied with Qt. To demonstrate theuse of a custom editor widget in a standard view, it is necessary to set up amodel containing some arbitrary data and a view to display it.

We set up the application in the normalway, construct a standard item model to hold some data, set up a table view touse the data in the model, and construct a custom delegate to use for editing:

?intmain(int argc, char *argv[])

?{

????QApplication app(argc, argv);

?

????QStandardItemModel model(4, 2);

????QTableView tableView;

????tableView.setModel(&model);

?

????SpinBoxDelegate delegate;

????tableView.setItemDelegate(&delegate);

The table view is informed about thedelegate, and will use it to display each of the items. Since the delegate is asubclass of?QItemDelegate, each cell in the table will be renderedusing standard painting operations.

We insert some arbitrary data into themodel for demonstration purposes:

????for (int row = 0; row < 4; ++row) {

????????for (int column = 0; column < 2; ++column) {

????????????QModelIndex index = model.index(row, column, QModelIndex());

????????????model.setData(index, QVariant((row+1) * (column+1)));

????????}

???? }

Finally, the table view is displayed witha window title, and we start the application's event loop:

????tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));

????tableView.show();

????return app.exec();

?}

Each of the cells in the table can now beedited in the usual way, but the spin box ensures that the data returned to themodel is always constrained by the values allowed by the spin box delegate.

热点排行