`
qimo601
  • 浏览: 3416358 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Qt实现表格内进度条展示数据

    博客分类:
  • Qt
阅读更多

 

Qt 中用代理处理Model中的数据展示形式。

 

The QAbstractItemDelegate class is used to display and edit data items from a model.

A QAbstractItemDelegate provides the interface and common functionality for delegates in the model/view architecture. Delegates display individual items in views, and handle the editing of model data.

The QAbstractItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework.

To render an item in a custom way, you must implement paint() and sizeHint(). The QItemDelegate class provides default implementations for these functions; if you do not need custom rendering, subclass that class instead.

We give an example of drawing a progress bar in items; in our case for a package management program.

We create the WidgetDelegate class, which inherits from QStyledItemDelegate. We do the drawing in the paint() function:

 void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,

                            const QModelIndex &index) const

 {

     if (index.column() == 1) {

         int progress = index.data().toInt();

 

         QStyleOptionProgressBar progressBarOption;

         progressBarOption.rect = option.rect;

         progressBarOption.minimum = 0;

         progressBarOption.maximum = 100;

         progressBarOption.progress = progress;

         progressBarOption.text = QString::number(progress) + "%";

         progressBarOption.textVisible = true;

 

         QApplication::style()->drawControl(QStyle::CE_ProgressBar,

                                            &progressBarOption, painter);

     } else

         QStyledItemDelegate::paint(painter, option, index);

Notice that we use a QStyleOptionProgressBar and initialize its members. We can then use the current QStyle to draw it.

To provide custom editing, there are two approaches that can be used. The first approach is to create an editor widget and display it directly on top of the item. To do this you must reimplementcreateEditor() to provide an editor widget, setEditorData() to populate the editor with the data from the model, and setModelData() so that the delegate can update the model with data from the editor.

The second approach is to handle user events directly by reimplementing editorEvent().

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics