×
Namespaces

Variants
Actions
Revision as of 04:15, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:定制QTreeView

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

Article Metadata

代码示例
兼容于
文章
dougcn 在 04 Apr 2009 创建
最后由 hamishwillee 在 11 Oct 2012 编辑
  • TSQ001336
  • 平台:S60 5th Edition
  • 设备:Nokia 5800 XPressMusic
  • 类别:Qt
  • 子类别:GUI
  • 关键字:QTreeView


Contents

概述

本文解释如何在Qt中定制树形视图。

描述

可通过重写QTreeView的虚拟方法(drawRow()drawBranches())来定制。本文演示如何定制树形视图在每个树形视图项的右边显示图形。

方案

QTreeView继承创建自定义类QCustomTreeView

 class QCustomTreeView : public QTreeView
{
Q_OBJECT
public:
QCustomTreeView( QWidget *parent = 0 );
~QCustomTreeView();
protected:
// QTreeView’s virtual member
void drawRow( QPainter *painter,
const QStyleOptionViewItem &options,
const QModelIndex &index ) const;
// We can also override drawBranches() member
// void drawBranches( QPainter *painter,
const QRect &rect,
const QModelIndex &index ) const;
};

QCustomTreeView类定义

 QCustomTreeView::QCustomTreeView( QWidget *parent )
: QTreeView( parent )
{
}
 
// Overriding the QTreeView’s drawRow() member
void QCustomTreeView::drawRow( QPainter *painter,
const QStyleOptionViewItem &options,
const QModelIndex &index ) const
{
// Calling the base class method for drawing the normal
// parts of the item of the QTreeView.
QTreeView::drawRow( painter, options, index );
 
// Calculate a rect for the image on the right side of the item
// We can retrieve the parent and child index of the model index
// QModelIndex parent(index.parent());
// QModelIndex child(index.child(0,0));
// Use your own logic to decide for which items the image on the
// right side should be drawn
if(index.row() == 0) {
QImage image;
image.load("c:\\data\\red.bmp");
image.scaledToHeight(options.rect.height());
image.scaledToWidth(15);
 
// Drawing the image on the right side of the item
painter->drawImage( options.rect.topRight().x() - 50,
options.rect.topRight().y(), image );
}
if(index.row() == 1) {
QImage image;
image.load("c:\\data\\blue.bmp");
image.scaledToHeight(options.rect.height());
image.scaledToWidth(15);
 
// Drawing the image on the right side of the item
painter->drawImage( options.rect.topRight().x() - 50,
options.rect.topRight().y(), image );
}
}

使用QCustomTreeView

   // Create an instance of the custom tree view
QCustomTreeView* customTree;
customTree = new QCustomTreeView( this );
 
// Populate the model for tree view
QStandardItemModel* treeModel = new QStandardItemModel( this );
QStandardItem* item1 = new QStandardItem( QIcon("c:\\data\\610x.bmp"), "Item1" );
QStandardItem* item2 = new QStandardItem( QIcon("c:\\data\\Nokia-logo.bmp"), "SubItem1" );
QStandardItem* item3 = new QStandardItem( QIcon("c:\\data\\5.bmp"), "Item2" );
QStandardItem* item4 = new QStandardItem( "SubItem2" );
QStandardItem* item5 = new QStandardItem( "SubItem3" );
item1->appendRow( item2 );
item1->appendRow( item4 );
item1->appendRow( item5 );
treeModel->appendRow( item1 );
treeModel->appendRow( item3 );
 
customTree->setModel( treeModel );
 
customTree->setHeaderHidden( true );
customTree->setAnimated( true );
customTree->setGeometry( QRect( 0, 0, 400, 500 ) );


例子应用

File:CustomTreeView.zip

This page was last modified on 11 October 2012, at 04:15.
196 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×