Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:53:29+00:00 2026-06-04T20:53:29+00:00

Problem : I have QTreeView object, and a QStandardItemModel as model to view widget;

  • 0

Problem:

  • I have QTreeView object, and a QStandardItemModel as model to view widget;
  • For some item’s I have set data with setData method to split them with a parameter;
  • So I need to draw different background pixmap for QStandardItem items, which are with icon’s and some text data;
  • And don’t want to redraw all the items objects, I mean icon and text. Just change background.

First, I was thinking that:

  • I could set CSS stylesheets in Qt Designer for the object with 2 different background pictures, BUT QStandardItem doesn’t have setProperty method…

Example:

QTreeView#treeView::item[ROLE="AAA"],
QTreeView#treeView::branch[ROLE="AAA"]
{
    height: 25px;
    border: none;
    color: #564f5b;
    background-image: url(:/backgrounds/images/row1.png);
    background-position: top left;
}

QTreeView#treeView::item[ROLE="BBB"],
QTreeView#treeView::branch[ROLE="BBB"]
{
    height: 25px;
    border: none;
    color: #564f5b;
    background-image: url(:/backgrounds/images/row2.png);
    background-position: top left;
}
  • then I created my own delegate, inherited from QStyledItemDelegate class, and reimplement paint method, BUT I can’t just change background, because QStyledItemDelegate::paint( painter, opt, index ); code will overdraw my drawPixmap…

Example:

QStyleOptionViewItemV4 opt = option; // Для обхода QTBUG-4310
opt.state &= ~QStyle::State_HasFocus; // Чтобы не рисовался прямоугольник фокуса 

QStyledItemDelegate::paint( painter, opt, index );    

// HERE I WANT TO CHANGE BACKGROUND (DEFAULT IS ALREADY SET IN DESIGNER WITH ABOVE CODE)
if( index.data( SORT_ROLE ).toBool() )
{
    const QPixmap pixmap( ":/backgrounds/images/backgrounds/contractor_row__high_priority.png" );
    painter->drawPixmap( option.rect, pixmap, pixmap.rect() );

    QStyledItemDelegate::paint( painter, opt, index );
}

So I’m stuck…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:53:31+00:00Added an answer on June 4, 2026 at 8:53 pm

    Here is my trick:

    The Designer‘s stylesheet part:

    QTreeView#treeView
    {
        border: none;
        background-color:#f0f0f1;
    }   
    
    QTreeView#treeView::item,
    QTreeView#treeView::branch
    {
        height: 25px;
        border: none;
        color: #564f5b;
    }
    
    QTreeView#treeView::item:selected,
    QTreeView#treeView::branch:selected
    {
        border-bottom: none;
        color: #ffffff;    
        background-image: url(:/backgrounds/images/backgrounds/kontragents_row_selection.png);
        background-position: top left;  
    
    }
    
    QTreeView#treeView::item:selected:!active,
    QTreeView#treeView::branch:selected:!active
    {
        color: #ffffff;
    }
    

    Delegate reimplemented paint() method:

    void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
     {
          QStyleOptionViewItemV4 opt = option; // Для обхода QTBUG-4310
          opt.state &= ~QStyle::State_HasFocus; // Чтобы не рисовался прямоугольник фокуса
    
          QBrush brush = opt.backgroundBrush;
          brush.setTexture( QPixmap( index.data( SORT_ROLE ).toBool()
               ? BACKGROUND_HIGH_PRIORITY
               : BACKGROUND_STANDARD ) );
    
          // FILL BACKGROUND     
          painter->save();
          painter->fillRect( opt.rect, brush );
          painter->restore();
    
          // DRAW ICON & TEXT
          QStyledItemDelegate::paint( painter, opt, index );
    
          // IF ( CHILD ) THEN PAINT OVER ONLY! BRANCH RECT
          bool isIndexParent = !index.parent().isValid();
          if( !isIndexParent )
          {
               QRect rect( 0, opt.rect.y(), 20, opt.rect.height() );
    
               if( opt.state & QStyle::State_Selected )
               {
                    brush.setTexture( QPixmap( BACKGROUND_SELECTED ) );
               }
    
               painter->save();
               painter->fillRect( rect, brush );
               painter->restore();
          }
     }
    

    Resulting QTreeView view:

    enter image description here

    Have a nice day! 🙂

    PS: no need redrawing icons, text, selection…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have: QTreeView class with table data And connected QAbstractTableModel model Question :
Problem I have a User model, and the Volunteer model that inherits from the
I'm trying to implement QAbstractItemModel for QTreeView. I have problem with inserting rows. I
So I have a QTreeView witdget connected to a model, and the same model
I have a hierarchical TreeView object with some structure: Main level second level 1
I have a view where I've got an object bound to a treeview. The
I have a problem with treeview nodes. When I click on some nodes, it
i have Observable collection object bounded to a treeView: MainTreeView.ItemsSource = ((App)Application.Current).TucOC; the problem
And another TreeView problem , should've used qt ;) I have a TreeView in
Hi The problem am having is that I have multiple TreeView control and each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.