1

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.

Hacker6914
  • 247
  • 1
  • 9
  • I don't understand your question. Your code works fine, as the border becomes red when the header is hovered, but you're not setting any background in the `:hover` pseudo. What is your problem, exactly? – musicamante Oct 14 '20 at 14:57
  • No actually I set everything ,even tree widget stylesheet ,I just croped it to a small example.I know I don't set background. But everything works except this ,I have also change the header section background.but hover does not work for me. My pyqt version is 5.15.1 . why it won't work in my program?@musicamante – Hacker6914 Oct 14 '20 at 15:30
  • @musicamante Can u please write ur program in answer section.I can't figure out why it happens – Hacker6914 Oct 14 '20 at 15:34
  • If you use a more extended stylesheet then you must show us: as said, your code works as expected, if your doesn't it's probably because you did something wrong in the parts of the stylesheet you didn't provide us. I have not written any program, I just tested it on a standard QTableWidget in Designer by using your stylesheet. – musicamante Oct 14 '20 at 15:46
  • Thank you! But I think it might work on QTabel widget just check and confirm with QTreeWidget not Tree view. I have also edited my post with entire stylesheet @musicamante – Hacker6914 Oct 14 '20 at 16:40
  • Sorry, I got distracted and didn't realize you were using a QTreeView. Does it work if you use `self.mytreeview.header().setSectionsClickable(True)`? – musicamante Oct 14 '20 at 16:52

1 Answers1

3

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)
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thank u! It works fine .I just use your comment. And also u forget to add ```header() ```in the answer section because tree widget has no attribute ```setSectionsClickable(True)```.and I marked it as Correct answer. – Hacker6914 Oct 14 '20 at 17:19
  • Sorry, it was lost in the copy&paste, it's fixed now. Thanks – musicamante Oct 14 '20 at 17:25