顶上去
--
今天的任务是显示彩色的树,比如 频道A(10/100) ,频道A 和 (10/100) 用不同的颜色标志.
暂时不知道怎么画,就先找了个用QLabel来渲染的例子.
附件上不了,就贴代码吧
Ui生成的头文件就不贴了,就一个QTreeView
主程序一如既往的简单,main.cpp如下:
#include <QApplication>
#include "DlgColorTreeView.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DlgColorTreeView dialog;
dialog.show();
return app.exec();
}
-----------------
我自己的窗体类,代码如下
#ifndef H_DLGCOLORTREEVIEW_H
#define H_DLGCOLORTREEVIEW_H
#include <QDialog>
#include <QLabel>
#include <QMessageBox>
#include <QStandardItemModel>
#include <QItemDelegate>
#include "ui_qtTreeView.h"
class MyItemDelegation: public QItemDelegate
{
public:
MyItemDelegation(QObject *parent = 0):QItemDelegate(parent){}
//我不理解这个构造函数是在什么地方调用的,
//他的parent是在什么时候传过去的,
//问题就出在这里,后面的Dynamic_cast出错
//晕~只怪自己C++忘了太多 virtual void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
//QMessageBox::information(0, "", parent()->objectName());
QAbstractItemView *view = dynamic_cast<QAbstractItemView*>(parent());
if (view->indexWidget(index)==0)
{
QLabel *label = new QLabel;
label->setAutoFillBackground(true);
label->setText("<font color=red>aaa</font>");
view->setIndexWidget(index,label);
}
else
QItemDelegate::paint(painter,option,index);
}
};
class DlgColorTreeView: public QDialog
{
Q_OBJECT
private:
Ui_Dialog dialog;
// MyItemDelegation delegate;
MyItemDelegation
*delegate;
public:
DlgColorTreeView():QDialog()
{
dialog.setupUi(this);
QStandardItemModel *model = new QStandardItemModel();
QModelIndex parent;
for (int i = 0; i < 4; ++i) {
parent = model->index(0, 0, parent);
model->insertRows(0, 1, parent);
model->insertColumns(0, 1, parent);
QModelIndex index = model->index(0, 0, parent);
model->setData(index, "<h2><font color=blue>1</font><font color=blue>2</font><h2>");
}
dialog.treeView->setModel(model);
delegate = new (dialog.treeView);// dialog.treeView->setItemDelegate(
&delegate);
dialog.treeView->setItemDelegate(delegate); }
};
#endif
-------------------------------
挖了两个坑了,不过不急,明天一并解决!
如果有人知道错误,还望指出来.
谢谢!
源代码
10.TreeView.rar (576 K) 下载次数:59 [ 此贴被wvins在2008-09-27 18:42重新编辑 ]