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

  • Home
  • SEARCH
  • 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 760835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:45:56+00:00 2026-05-14T15:45:56+00:00

I have a custom subclass of a QTreeView in a pyqt application. I’m trying

  • 0

I have a custom subclass of a QTreeView in a pyqt application. I’m trying to give the user the ability to highlight and “lowlight” (for lack of a better term) rows. Highlighted rows should have bold text and (optionally) a different background color. Any ideas?
I’m considering StyleSheets as an option, but have so far been unable to get that to work. If I set the QTreeView’s stylesheet:

self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")

I can’t figure out how to manually enable ‘states’ that would keep only the desired rows at a particular state. If I try setting an individual item’s stylesheet:

#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()

I get a segfault.
I’m not convinced stylesheets are the way to go, I’m open to all ideas. Thanks!

  • 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-05-14T15:45:57+00:00Added an answer on May 14, 2026 at 3:45 pm

    From what you’ve said it looks like the easiest solution would be to define a custom item delegate for your treeview and set items font weight to bold whenever it’s needed. Pls, check if an example below would work for you, it should create a treeview with custom item delegate which would change item’s font style.

    import sys
    from PyQt4 import QtGui, QtCore
    
    class BoldDelegate(QtGui.QStyledItemDelegate):
        def paint(self, painter, option, index):
            # decide here if item should be bold and set font weight to bold if needed 
            option.font.setWeight(QtGui.QFont.Bold)
            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
    
    
    class MainForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainForm, self).__init__(parent)
    
            model = QtGui.QStandardItemModel()
    
            for k in range(0, 4):
                parentItem = model.invisibleRootItem()
                for i in range(0, 4):
                    item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                    parentItem.appendRow(item)
                    parentItem = item
    
            self.view = QtGui.QTreeView()
            self.view.setModel(model)
            self.view.setItemDelegate(BoldDelegate(self))
    
            self.setCentralWidget(self.view)
    
    def main():
        app = QtGui.QApplication(sys.argv)
        form = MainForm()
        form.show()
        app.exec_()
    
    if __name__ == '__main__':
        main()
    

    hope this helps, regards

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

Sidebar

Related Questions

I have created a custom subclass of UITableViewCell. There, I allow the user to
I have a custom UIView subclass that i'm trying to use as a header
I have a custom NSWindow subclass that the user can toggle the display of
I am trying to implement the UIInputViewAudioFeedback protocol. I have created a custom subclass
I have custom view in my application which can be scrolled by the user.
Similar to this question I have a custom subclass of UITableViewCell that has a
I have a custom UITableViewCell subclass. I have set the contentView of my cell
I have a custom UIViewController subclass, which gets pushed on a UINavigationController stack. I
I have a custom NSWindowController subclass that loads a NIB file during initialization like
I have a custom UIViewController subclass that handles all the view initialization by itself

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.