Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >QTreeview custom

QTreeview custom

作者头像
全栈程序员站长
发布于 2022-08-31 09:19:24
发布于 2022-08-31 09:19:24
82700
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;     } }; 

  
  
  
   
    
    
      class MyDelegate 
     : 
     public 
     QItemDelegate 
    
   
    
     
     { 
       
    
   
    
     
     protected 
     : 
    
   
    
     
     void paint 
     ( 
     QPainter 
     * painter, 
     const QStyleOptionViewItem 
     & option, 
     const QModelIndex 
     & index 
     ) 
     const; 
    
   
    
     
     }; 
    
   
    
    
        
    
   
    
     
     void MyDelegate 
     :: 
     paint 
     ( 
     QPainter 
     * painter, 
     const 
     QStyleOptionViewItem 
     & option, 
     const 
     QModelIndex 
     & index 
     ) 
     const 
    
   
    
     
     { 
       
    
   
    
     
     QStyleOptionViewItem itemOption 
     (option 
     ); 
    
   
    
    
        
    
   
    
     
     if 
     (itemOption. 
     state 
     & 
     QStyle 
     :: 
     State_HasFocus 
     ) 
    
   
    
     
     { 
       
    
   
    
    
      qDebug 
     ( 
     "FOCUS" 
     ); 
    
   
    
    
      itemOption. 
     state 
     ^= 
     QStyle 
     :: 
     State_HasFocus; 
    
   
    
     
     } 
    
   
    
    
        
    
   
    
     
     QItemDelegate 
     :: 
     paint 
     (painter, itemOption, index 
     ); 
    
   
    
     
     } 
    
  
  

  
  
  
   
    
     
     void Style 
     :: 
     drawControl 
     (ControlElement element, 
     const 
     QStyleOption 
     * option, 
     QPainter 
     * painter, 
     const 
     QWidget 
     * widget 
     ) 
     const 
    
   
    
     
     { 
       
    
   
    
     
     if 
     (element 
     == CE_ItemViewItem 
     ) 
    
   
    
     
     { 
       
    
   
    
     
     const 
     QStyleOptionViewItem 
     *b 
     = qstyleoption_cast<const 
     QStyleOptionViewItem 
     *> 
     (option 
     ); 
    
   
    
     
     QStyleOptionViewItem 
     *btn 
     = 
     ( 
     QStyleOptionViewItem 
     * 
     )b; 
    
   
    
    
        
    
   
    
     
     if 
     (btn 
     ) 
    
   
    
     
     { 
       
    
   
    
     
     if 
     (btn 
     ->state 
     & State_HasFocus 
     ) 
    
   
    
     
     { 
       
    
   
    
    
      btn 
     ->state 
     = btn 
     ->state 
     ^ State_HasFocus; 
    
   
    
     
     } 
    
   
    
     
     } 
    
   
    
    
        
    
   
    
     
     QWindowsStyle 
     :: 
     drawControl 
     (element, btn, painter, widget 
     ); 
    
   
    
    
        
    
   
    
     
     } 
    
   
    
     
     else 
    
   
    
     
     { 
       
    
   
    
     
     QWindowsStyle 
     :: 
     drawControl 
     (element, option, painter, widget 
     ); 
    
   
    
     
     } 
    
   
    
     
     } 
    
  
  
 
 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:

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

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:

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:  

  
  
  
   
    
     
     #include <QtGui> 
    
   
    
    
        
    
   
    
     
     // NOTE: you may use the ProxyStyle is available at Qt Centre wiki but make it inherit QCommonStyle instead of QStyle 
    
   
    
     
     #include "proxystyle.h" 
    
   
    
    
        
    
   
    
    
      class MyProxyStyle 
     : 
     public ProxyStyle 
    
   
    
     
     { 
       
    
   
    
     
     public 
     : 
    
   
    
     
     explicit MyProxyStyle 
     ( 
     const QString 
     & baseStyle 
     ) 
     : ProxyStyle 
     (baseStyle 
     ) 
    
   
    
     
     { 
       
    
   
    
     
     } 
    
   
    
    
        
    
   
    
     
     void drawPrimitive 
     (PrimitiveElement element, 
     const 
     QStyleOption 
     * option, 
     QPainter 
     * painter, 
     const 
     QWidget 
     * widget 
     = 
     0 
     ) 
     const 
    
   
    
     
     { 
       
    
   
    
     
     if 
     (element 
     == 
     QStyle 
     :: 
     PE_IndicatorBranch 
     ) 
    
   
    
     
     { 
       
    
   
    
    
      painter 
     ->save 
     ( 
     ); 
    
   
    
    
      painter 
     ->setPen 
     ( 
     QPen 
     (Qt 
     :: 
     blue, 
     1, Qt 
     :: 
     DashLine 
     ) 
     ); 
    
   
    
     
     QCommonStyle 
     :: 
     drawPrimitive 
     (element, option, painter, widget 
     ); 
    
   
    
    
      painter 
     ->restore 
     ( 
     ); 
    
   
    
     
     } 
    
   
    
    
      ProxyStyle 
     :: 
     drawPrimitive 
     (element, option, painter, widget 
     ); 
    
   
    
     
     } 
    
   
    
     
     }; 
    
   
    
    
        
    
   
    
     
     int main 
     ( 
     int argc, 
     char 
     * argv 
     [ 
     ] 
     ) 
    
   
    
     
     { 
       
    
   
    
     
     QApplication app 
     (argc, argv 
     ); 
    
   
    
     
     QTreeWidget tree; 
    
   
    
    
      tree. 
     setStyle 
     ( 
     new MyProxyStyle 
     (app. 
     style 
     ( 
     ) 
     ->objectName 
     ( 
     ) 
     ) 
     ); 
    
   
    
     
     for 
     ( 
     int i 
     = 
     0; i < 
     10; 
     ++i 
     ) 
    
   
    
     
     new 
     QTreeWidgetItem 
     ( 
     &tree, 
     QStringList 
     ( 
     ) << 
     QString 
     :: 
     number 
     (i 
     ) 
     ); 
    
   
    
    
      tree. 
     show 
     ( 
     ); 
    
   
    
     
     return app. 
     exec 
     ( 
     ); 
    
   
    
     
     } 
    
  
  
 
 






                                                        发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143000.html原文链接:https://javaforall.cn
如果您是在找激活码,但输入激活码后激活失败,最新激活码地址:https://javaforall.cn/127239.html                        
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
26.QT-模型视图之自定义委托
由于模型负责组织数据,而视图负责显示数据,所以当用户想修改显示的数据时,就要通过视图中的委托来完成
诺谦
2018/07/31
2.5K0
26.QT-模型视图之自定义委托
QTableView 添加按钮
这里说一下怎么在QTableView添加一个按钮 效果是点击button弹出一个对话框。 看一下ButtonDelegate的代码 #ifndef BUTTONDELEGATE_H #define B
lpxxn
2018/01/31
2.7K0
QTableView 添加按钮
QTableView 一列添加两个按钮
在QTableView的一列里添加两个按钮,之前添加一个按钮的思路是一样的,只是计算了一下按钮的宽,放两个按钮而已。 本例源代码:QtTowButtons.rar 看一下列的效果 看一下添加两个按钮的
lpxxn
2018/01/31
4K0
QTableView 一列添加两个按钮
QTableView 添加进度条
记录一下QTableView添加进度条 例子很小,仅供学习 使用QItemDelegate做的实现 有自动更新进度 要在.pro文件里添加  CONFIG += c++11 ProgressBar
lpxxn
2018/01/31
2.3K0
QTableView 添加进度条
​Qt QTreeView 使用方法详解
QTreeView 是 Qt 框架中用于展示层次化数据的核心控件,属于 Model/View 架构的重要实现。它能够以树形结构可视化复杂数据关系,并提供丰富的交互能力,是构建文件资源管理器、组织架构图、配置面板等场景的理想选择。
lealc
2025/05/28
2920
​Qt QTreeView 使用方法详解
Qt Model_View教程之Delegate
在之前的文章里主要介绍了Qt Model/View 的一些基本用法,接下来结合Delegate做最后的说明。
用户5908113
2020/02/18
1.6K0
QTreeView实现圆角样式
在QTreeView等继承于QAbstractItemView表格中,定制表格样式通常都是通过设置项目代理(ItemDelegate)来实现。在这种实现方法中,每个项目(Item)基本上是孤立的,无法有效判断己身周遭环境。 如果以此种方法来实现圆角样式,行首或许还能通过方法int QModelindex::column()来判断是否属于第一列来断定,然而行尾难道还要通过QAbstractItemModel QModelindex::*model()获取模型(Model)之后再来判断吗?且视图(View)本身可以隐藏、拖拽指定行、列,所以这种方式并不靠谱。 更正: QStyleOptionViewItem提供了获取可视项目位置的接口,可通过
全栈程序员站长
2022/09/06
1.8K0
QTreeView实现圆角样式
qt tabwidget切换_qtabwidget tab样式
感谢 https://blog.csdn.net/skyztttt/article/details/52448992
全栈程序员站长
2022/10/01
1.4K0
qt tabwidget切换_qtabwidget tab样式
Qt编写自定义控件46-树状导航栏
树状导航栏控件是所有控件中最牛逼最经典最厉害的一个,在很多购买者中,使用频率也是最高,因为该导航控件集合了非常多的展示效果,比如左侧图标+右侧箭头+元素前面的图标设置+各种颜色设置等,全部涵盖了,代码量也比较多,该控件前后完善了三年,还提供了角标展示文字信息,纵观市面上web也好,cs架构的程序也好,这种导航条使用非常多,目前只提供了二级菜单,如果需要三级菜单需要自行更改源码才行。
feiyangqingyun
2019/08/28
3K0
Qt编写自定义控件46-树状导航栏
C/C++ Qt TableDelegate 自定义代理组件
TableDelegate 自定义代理组件的主要作用是对原有表格进行调整,例如默认情况下Table中的缺省代理就是一个编辑框,我们只能够在编辑框内输入数据,而有时我们想选择数据而不是输入,此时就需要重写编辑框实现选择的效果,代理组件常用于个性化定制Table表格中的字段类型。
王 瑞
2022/12/28
6950
C/C++ Qt TableDelegate 自定义代理组件
C/C++ Qt TableDelegate 自定义代理组件
TableDelegate 自定义代理组件的主要作用是对原有表格进行调整,例如默认情况下Table中的缺省代理就是一个编辑框,我们只能够在编辑框内输入数据,而有时我们想选择数据而不是输入,此时就需要重写编辑框实现选择的效果,代理组件常用于个性化定制Table表格中的字段类型。
王 瑞
2022/12/23
9640
C/C++ Qt TableDelegate 自定义代理组件
美化QTabWidget[通俗易懂]
主要原理就是对paintEvent重写,以及QProxyStyle继承重新实现。我也是翻了很多资料,看了实现的源码才知道的。
全栈程序员站长
2022/11/09
1.8K0
美化QTabWidget[通俗易懂]
Qt / Model_View_Delegate
早期Qt:传统的窗口部件,其本身内部包含,用于存储数据的内置容器。(直观,但是同步数据较低效)
Kano365
2022/04/12
1.9K0
用Qt写软件系列三:一个简单的系统工具之界面美化
前言      在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出、进程子模块信息等功能,留到后面再来慢慢实现。这一篇来讲述如何对主界面进行个性化的定制。Qt库提供的只是最基本的组件功能,使用
24K纯开源
2018/01/18
7.5K0
用Qt写软件系列三:一个简单的系统工具之界面美化
【QT】QT模型/视图
MVC(Model-View-Controller)包括了3个组件:模型(model)是应用对象,用来表示数据;视图(View)是模型的用户界面,用来显示数据;控制(Controller)定义了用户界面对用户输入的反应方式。
半生瓜的blog
2023/05/13
3.4K0
【QT】QT模型/视图
Qt-改变tabBar位置并改变文字方向
QTabWidget默认tab页标题(tabBar)在上面,但是有时候我们需要改变它的位置,比如做一个设置页面,想将它放到左边显示,这个比较简单,只需要设置一个属性即可实现:tabPosition:west,但是我们发现它文字的方向是没有改变的,这样完全没有使用体验,所以我们需要改变文字的方向。
kdyonly
2023/03/03
3.8K0
Qt-改变tabBar位置并改变文字方向
Qt QTreeWidget 详解
QTreeWidget详细设置_qtreewidget-C/C++文档类资源-CSDN下载
全栈程序员站长
2022/11/10
1.2K0
Qt QTreeWidget 详解
QTreeView使用总结7,右键菜单
void customContextMenuRequested(const QPoint &pos);
全栈程序员站长
2022/08/25
5.4K0
QTreeView使用总结7,右键菜单
Qt官方示例-拖放机器人
  Graphics View提供了QGraphicsScene类,用于管理从QGraphicsItem类派生的大量定制2D图形项目,并与之交互;还提供了QGraphicsView小部件,用于可视化项目,并支持缩放和旋转。
Qt君
2020/02/24
5.1K1
qt tabwidget使用_word横向表格变竖向
参考文件引用自http://blog.csdn.net/skyztttt/article/details/52448992 你的评论,是我的动力
全栈程序员站长
2022/11/09
1.5K0
qt tabwidget使用_word横向表格变竖向
相关推荐
26.QT-模型视图之自定义委托
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验