QTreeview custom

QTreeview custom设定字体virtualQVariantMyModel::data(constQModelIndex&index,introle)const{if(index.isValid()&&role==Qt::ForegroundRole){if(index.column()==2){

大家好,又见面了,我是你们的朋友全栈君。设定字体

virtual QVariant MyModel::data( const QModelIndex &index, int role ) const
{
    if ( index.isValid() && role == Qt::ForegroundRole )
    {
        if ( index.column() == 2 )
        {
            return QVariant( QColor( Qt::red ) );
        }
        return QVariant( QColor( Qt::black ) );
    }

    return QAbstractItemModel::data( index, role );
}


显示图标

QVariant QTreeModel::data(const QModelIndex& index, int role) const
{
if (role == Qt::DecorationRole.)
{
// return QVariant();
return QIcon("act.png");
}
QTreeNode* node = nodeFromIndex(index);

if (node == 0)
return QVariant();

if (index.column() == 0) {
return node->name();
}

return QVariant();
}


//

设置行背景图, 以及Branch图片

      iCustomTree->setStyleSheet(   \

        “QTreeView { \

            show-decoration-selected: 1; \

        } \

        \

        QTreeView::item { \

            border: none; \

            background-image: url(images/list_bg01.png); \

        } \

        \

QTreeView::branch:closed:has-children:has-siblings { \

        image: url(images/icon_add.png); \

} \

 \

QTreeView::branch:has-children:!has-siblings:closed { \

        image: url(images/icon_add.png); \

} \

 \

QTreeView::branch:open:has-children:has-siblings { \

        image: url(images/icon_reduce.png); \

} \

 \

QTreeView::branch:open:has-children:!has-siblings { \

        image: url(images/icon_reduce.png); \

} \

“);

/

class ListViewDelegate : public QStyledItemDelegate { 
    protected:     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const     { 
            QStyleOptionViewItemV4 opt = option;         initStyleOption(&opt, index);         QString line0 = index.model()->data(index.model()->index(index.row(), 1)).toString();         QString line1 = index.model()->data(index.model()->index(index.row(), 2)).toString();         // draw correct background         opt.text = "";         QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();         style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);         QRect rect = opt.rect;         QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;         if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))             cg = QPalette::Inactive;         // set pen color         if (opt.state & QStyle::State_Selected)             painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));         else             painter->setPen(opt.palette.color(cg, QPalette::Text));         // draw 2 lines of text         painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()/2),                           opt.displayAlignment, line0);         painter->drawText(QRect(rect.left(), rect.top()+rect.height()/2, rect.width(), rect.height()/2),                           opt.displayAlignment, line1);     }     QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const     { 
            QSize result = QStyledItemDelegate::sizeHint(option, index);         result.setHeight(result.height()*2);         return result;     } }; 

  
  
  
  1. class MyDelegate : public QItemDelegate
  2. {
  3. protected :
  4. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
  5. };
  6.  
  7. void MyDelegate :: paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  8. {
  9. QStyleOptionViewItem itemOption (option );
  10.  
  11. if (itemOption. state & QStyle :: State_HasFocus )
  12. {
  13. qDebug ( "FOCUS" );
  14. itemOption. state ^= QStyle :: State_HasFocus;
  15. }
  16.  
  17. QItemDelegate :: paint (painter, itemOption, index );
  18. }

  
  
  
  1. void Style :: drawControl (ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget ) const
  2. {
  3. if (element == CE_ItemViewItem )
  4. {
  5. const QStyleOptionViewItem *b = qstyleoption_cast<const QStyleOptionViewItem *> (option );
  6.  
  7. if (btn )
  8. {
  9. if (btn ->state & State_HasFocus )
  10. {
  11. btn ->state = btn ->state ^ State_HasFocus;
  12. }
  13. }
  14.  
  15. QWindowsStyle :: drawControl (element, btn, painter, widget );
  16.  
  17. }
  18. else
  19. {
  20. QWindowsStyle :: drawControl (element, option, painter, widget );
  21. }
  22. }

 using a QTreeView and a QItemDelegate to reimplement most of the paint routine.

the expand/collapse buttons and the sibling/child lines are drawn automatically by some other paint routine.

Qt draws a QTreeView item in this order:

[Expand button] — [Checkbox] — [Rest of treeitem stuff]

I want to draw it in this order:

[Checkbox] — [Expand button] — [Rest of treeitem stuff]

You can change those using a style sheet. This is from the Customizing QTreeView example in the style sheet reference:

QTreeView::branch:has-siblings:!adjoins-item {

     border-image: url(vline.png) 0;
 }

 QTreeView::branch:has-siblings:adjoins-item {

     border-image: url(branch-more.png) 0;
 }

 QTreeView::branch:!has-children:!has-siblings:adjoins-item {

     border-image: url(branch-end.png) 0;
 }

 QTreeView::branch:has-children:!has-siblings:closed,
 QTreeView::branch:closed:has-children:has-siblings {

         border-image: none;
         image: url(branch-closed.png);
 }

 QTreeView::branch:open:has-children:!has-siblings,
 QTreeView::branch:open:has-children:has-siblings  {

         border-image: none;
         image: url(branch-open.png);
 }

where the png filenames are the images you want to use.

The rows are painted by QTreeView.drawRow, the branches and expand icons are drawn in drawBranches.

定制树形视图。

可通过重写QTreeView的虚拟方法(drawRow()drawBranches())来定制。

QStyledItemDelegate – Styling Item views

It all started as a small feature request – Adding style sheet support for Item views. I quickly found out that our default delegate, QItemDelegate, doesn’t use QStyle the way it is supposed to. Jens had faced the same problem when trying to provide native look and feel for Vista and ended up writing his own delegate. This is quite a limitation because currently authors of custom QStyles can customize the look of everything in Qt except item views.

So was born QStyledItemDelegate – The default delegate for Item views starting 4.4. To let that sink in – all our views now delegate painting to QStyledItemDelegate instead of QItemDelegate. QStyledItemDelegate prompty plays its part by delegating everything to QStyle -) The cool thing is that this delegate uses the QStyle to determine the sizeHint, the layout of the text, indicators and icon. And of course, it paints everything through QStyle. Complete control to QStyle.

Jens has already incorporated this new feature into QWindowsVistaStyle. So, by default, your views will look all fancy in 4.4 like below:

vistatree2.png

We are unsure about how selection in table view must be handled. We are open to suggestions:

Chart example in vista style

On other platforms, they should exactly like before. If you had written your own delegate using QItemDelegate, it is completely unaffected by this change. If having to write a custom QStyle or a custom delegate sounds daunting, just use style sheets. Try something like this,

QTreeView::item {
    border: 1px solid #d9d9d9;
    border-top-color: transparent;
    border-bottom-color: transparent;
}

QTreeView::item:hover {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
    border: 1px solid #bfcde4;
}

QTreeView::item:selected {
    border: 1px solid #567dbc;
}

QTreeView::item:selected:active{
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
}

QTreeView::item:selected:!active {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
}

Here’s how it looks:

stylesheet-treeview.png

See Customizing QTreeView, Customizing QListView for more information.

We merged the new delegate into our main branch last week, so if use the latest snapshots, you already have it. If you have any specific requirements to styling of tree widgets, list views and tables in mind and are wondering if it is possible now using the new delegate, please leave behind a note!

the QTreeView doesn’t draw the branch indicator itself but passes required information to the current style to draw it. It’s up to the style to decide the color… Anyway, QCommonStyle uses the pen color directly so you could even do it like this:
Qt Code:

  
  
  
  1. #include <QtGui>
  2.  
  3. // NOTE: you may use the ProxyStyle is available at Qt Centre wiki but make it inherit QCommonStyle instead of QStyle
  4. #include "proxystyle.h"
  5.  
  6. class MyProxyStyle : public ProxyStyle
  7. {
  8. public :
  9. explicit MyProxyStyle ( const QString & baseStyle ) : ProxyStyle (baseStyle )
  10. {
  11. }
  12.  
  13. void drawPrimitive (PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget = 0 ) const
  14. {
  15. if (element == QStyle :: PE_IndicatorBranch )
  16. {
  17. painter ->save ( );
  18. painter ->setPen ( QPen (Qt :: blue, 1, Qt :: DashLine ) );
  19. QCommonStyle :: drawPrimitive (element, option, painter, widget );
  20. painter ->restore ( );
  21. }
  22. ProxyStyle :: drawPrimitive (element, option, painter, widget );
  23. }
  24. };
  25.  
  26. int main ( int argc, char * argv [ ] )
  27. {
  28. QApplication app (argc, argv );
  29. QTreeWidget tree;
  30. tree. setStyle ( new MyProxyStyle (app. style ( ) ->objectName ( ) ) );
  31. for ( int i = 0; i < 10; ++i )
  32. new QTreeWidgetItem ( &tree, QStringList ( ) << QString :: number (i ) );
  33. tree. show ( );
  34. return app. exec ( );
  35. }



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143000.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 静态代理和动态代理详解[通俗易懂]

    静态代理和动态代理详解[通俗易懂]1代理生活中的代理:比如我们生活中比较熟悉的代购、中介、商家等都是属于代理2什么是代理模式代理模式是指:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户类和目标对象之间起到中介的作用。换句话说,使用代理对象,是为了在不修改目标对象的基础上,增强主业务逻辑。客户类真正想要访问的对象是目标对象,但客户类真正可以访问的对象是代理对象。客户类对目标对象的访问是通过访问代理对象来实现的。当然,代理类与目标类要实现同一个接.

    2022年10月10日
    3
  • WINDOWS XP安装SQL2000方法

    一.在SQL服务器的安装盘中找到MSDE这个目录,并且点击setup.exe安装它,过程简单直接下一步就OK了。二.重启系统WINDOWSXP,这下就可以看到SQL服务的图标出现了。三.再拿出SQL服务器版的安装光盘,直接安装客户端工具(最简单的方法就是直接点击光盘根目录下的autorun.exe)根据提示安装,自检过程中知道系统不是SERVER版,会提示只安装客户端工具。四.打开企业管理器

    2022年4月13日
    129
  • linux清除文件内容 >,Linux清除文件内容的几种方法[通俗易懂]

    linux清除文件内容 >,Linux清除文件内容的几种方法[通俗易懂]#清空或删除大文件内容的五种方法:#法一:通过重定向到Null来清空文件内容$>test.sh#法二:使用‘true’命令重定向来清空文件$true>test.sh#可通过下列命令来查看文件容量是否为0$du-htest.sh0test.sh#法三:使用cat/cp/dd实用工具及/dev/null设备来清空文件$cat/dev/null…

    2022年7月26日
    6
  • 分布式事务处理技术之LCN

    分布式事务处理技术之LCN分布式事务 LCN 第一章分布式事务介绍一 什么是分布式事务二 XA 的两阶段提交方案三 TCC 解决方案四 分布式事务中间件解决方案第二章 LCN 分布式事务处理框架介绍一 什么是 LCN 框架二 LCN 框架原理及执行步骤三 什么是 LCN 的事务协调机制四 LCN 的事务补偿机制第三章 LCN 分布式事务框架应用一 LCN 分布式事务框架应用环境搭建创建入口项目 springcloud porta

    2025年9月25日
    0
  • CentOS 7安装教程(图文详解)

    CentOS 7安装教程(图文详解)CentOS7安装教程: 准备:软件:VMwareWorkstation镜像文件:CentOS-7-x86_64-bin-DVD1.iso(附:教程较为详细,注释较多,故将操作的选项进行了加粗字体显示。) 1、文件–新建虚拟机–自定义2、…

    2022年6月13日
    63
  • java与python-如何对比Python和Java,只需三分钟告诉你!

    java与python-如何对比Python和Java,只需三分钟告诉你!原标题:如何对比Python和Java,只需三分钟告诉你!Java和Python两门语言都是目前非常热门的语言,可谓北乔峰南慕容,二者不分上下,棋逢对手。但是对于初学者来说,非常困惑,因为时间和精力有限,上手必须要先学一门,到底选那个好呢,今天3分钟带你透彻了解。1.运行速度java是静态语言静态编译的,速度上要比Python快的很多,而Python动态类型语言,一边执行一边编译,速度要上慢一些…

    2022年7月7日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号