前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QTreeview custom

QTreeview custom

作者头像
全栈程序员站长
发布2022-08-31 17:19:24
7300
发布2022-08-31 17:19:24
举报
文章被收录于专栏:全栈程序员必看

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

代码语言:javascript
复制
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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档