Search code examples
pythonpython-3.xqtpyqt5qtreewidget

how change QTreewidget header background when hover


i am making a program with QTreeWidget and i dont know how to change its header background color when hover i tried but hover is not working.This is my code.

self.mytreeview.setStyleSheet('''
QHeaderView::section {
    background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                      stop:0 #616161, stop: 0.5 #505050,
                                      stop: 0.6 #434343, stop:1 #656565);
    color: white;
    padding-left: 4px;
    border: 1px solid #6c6c6c;
}
QHeaderView::section:hover {
background-color:blue;
    border: 2px solid red;
}
QTreeView {
    show-decoration-selected: 1;
    
    outline:0;
}

QTreeView::item {
    color:#f5f6f7;
    
}

QTreeView::item:hover {
    background: rgba(80, 120, 242, 100);
    border-top:1px solid #002cf2;
    border-bottom:1px solid #002cf2;
    
    
}

QTreeView::item:selected {
    background: rgb(80, 120, 242)
}


''')
self.mytreeview.verticalScrollBar().setStyleSheet(" QScrollBar:vertical\n"
" {\n"
"     background-color: #2A2929;\n"
"     width: 15px;\n"
"     margin: 15px 3px 15px 3px;\n"
"     border: 1px transparent #2A2929;\n"
"     border-radius: 4px;\n"
" }\n"
"\n"
" QScrollBar::handle:vertical\n"
" {\n"
"     background-color: rgb(119, 139, 255);         /* #605F5F; */\n"
"     min-height: 5px;\n"
"     border-radius: 4px;\n"
" }\n"
" QScrollBar::handle:vertical:hover\n"
" {\n"
"     background-color:rgb(60, 73, 255);         /* #605F5F; */\n"
"     min-height: 5px;\n"
"     border-radius: 4px;\n"
" }\n"
"\n"
" QScrollBar::sub-line:vertical\n"
" {\n"
"     margin: 3px 0px 3px 0px;\n"
"     border-image: url(:/newPrefix/Simple_PySide_Base-master/icons/sort-up.png);\n"
"     height: 10px;\n"
"     width: 10px;\n"
"     subcontrol-position: top;\n"
"     subcontrol-origin: margin;\n"
" }\n"
"\n"
" QScrollBar::add-line:vertical\n"
" {\n"
"     margin: 3px 0px 3px 0px;\n"
"     border-image: url(:/newPrefix/Simple_PySide_Base-master/icons/sort-down.png);\n"
"     height: 10px;\n"
"     width: 10px;\n"
"     subcontrol-position: bottom;\n"
"     subcontrol-origin: margin;\n"
" }\n"
"\n"
" QScrollBar::sub-line:vertical:hover,QScrollBar::sub-line:vertical:on\n"
" {\n"
"\n"
"     border-image:url(:/newPrefix/Simple_PySide_Base-master/icons/sort-up-hover.png);\n"
"     height: 10px;\n"
"     width: 10px;\n"
"     subcontrol-position: top;\n"
"     subcontrol-origin: margin;\n"
" }\n"
"\n"
"\n"
" QScrollBar::add-line:vertical:hover, QScrollBar::add-line:vertical:on\n"
" {\n"
"     border-image: url(:/newPrefix/Simple_PySide_Base-master/icons/sort-down-hover.png);\n"
"     height: 10px;\n"
"     width: 10px;\n"
"     subcontrol-position: bottom;\n"
"     subcontrol-origin: margin;\n"
" }\n"
"\n"
" QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical\n"
" {\n"
"     background: none;\n"
" }\n"
"\n"
"\n"
" QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical\n"
" {\n"
"     background: none;\n"
" }")

As per @musicamante,Now i have edited this with my complete stylesheet. Everything works fine the only problem is hover does not affect .this might work on Qtabwidget or QtabelWidget but i want it in Qtreewidget.


Solution

  • It seems that a mandatory requirement for the :hover pseudo in QHeaderView is to have the sections clickable, which is not enabled by default for the header of QTreeView.

    Then, the solution is quite simple:

        self.mytreeview.header().setSectionsClickable(True)