I’m trying to style the scroll bar of a QtTableWidget so the part where the “scrolling part” isn’t on the bar is transparent so the scrollbar would be of the color of the background and the scrolling part (where you put your mouse) is the only part whose color is different from the background.
I tried this as a stylesheet:
JavaScript
x
21
21
1
QTableWidget {
2
background-color:rgb(57, 57, 57);
3
color:rgb(255,255,255);
4
border-radius:3px;
5
}
6
7
QScrollBar:vertical {
8
border: none;
9
background: rgb(45, 45, 68);
10
width: 14px;
11
margin: 15px 0 15px 0;
12
border-radius: 0px;
13
}
14
15
/* HANDLE BAR VERTICAL */
16
QScrollBar::handle:vertical {
17
background-color: rgb(80, 80, 122);
18
min-height: 30px;
19
border-radius: 7px;
20
}
21
But when the windows is displayed the part I want to be transparent is filled with a white tilled background like that :
I’m not sure what causes this issue in my Stylesheet.
Here is the code to reproduce the window :
JavaScript
1
74
74
1
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
2
QRect, QSize, QUrl, Qt)
3
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
4
QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
5
QRadialGradient)
6
from PySide2.QtWidgets import *
7
8
9
class Ui_MainWindow(object):
10
def setupUi(self, MainWindow):
11
if MainWindow.objectName():
12
MainWindow.setObjectName(u"MainWindow")
13
MainWindow.resize(800, 600)
14
self.centralwidget = QWidget(MainWindow)
15
self.centralwidget.setObjectName(u"centralwidget")
16
self.tableWidget = QTableWidget(self.centralwidget)
17
if (self.tableWidget.rowCount() < 100):
18
self.tableWidget.setRowCount(100)
19
self.tableWidget.setObjectName(u"tableWidget")
20
self.tableWidget.setGeometry(QRect(30, 30, 721, 461))
21
self.tableWidget.setStyleSheet(u"QTableWidget {n"
22
" background-color:rgb(57, 57, 57);n"
23
" color:rgb(255,255,255);n"
24
" border-radius:3px;n"
25
"}n"
26
"n"
27
"QScrollBar:vertical {n"
28
" border: none;n"
29
" background: rgb(45, 45, 68);n"
30
" width: 14px;n"
31
" margin: 15px 0 15px 0;n"
32
" border-radius: 0px;n"
33
" }n"
34
"n"
35
"/* HANDLE BAR VERTICAL */n"
36
"QScrollBar::handle:vertical { n"
37
" background-color: rgb(80, 80, 122);n"
38
" min-height: 30px;n"
39
" border-radius: 7px;n"
40
"}n"
41
"QScrollBar::handle:vertical:hover{ n"
42
" background-color: rgb(255, 0, 127);n"
43
"}n"
44
"QScrollBar::handle:vertical:pressed { n"
45
" background-color: rgb(185, 0, 92);n"
46
"}")
47
self.tableWidget.setRowCount(100)
48
MainWindow.setCentralWidget(self.centralwidget)
49
self.menubar = QMenuBar(MainWindow)
50
self.menubar.setObjectName(u"menubar")
51
self.menubar.setGeometry(QRect(0, 0, 800, 26))
52
MainWindow.setMenuBar(self.menubar)
53
self.statusbar = QStatusBar(MainWindow)
54
self.statusbar.setObjectName(u"statusbar")
55
MainWindow.setStatusBar(self.statusbar)
56
57
58
QMetaObject.connectSlotsByName(MainWindow)
59
60
61
class MainWindow(QMainWindow):
62
63
def __init__(self):
64
QMainWindow.__init__(self)
65
self.ui = Ui_MainWindow()
66
self.ui.setupUi(self)
67
68
69
import sys
70
app = QApplication(sys.argv)
71
window = MainWindow()
72
window.show()
73
sys.exit(app.exec_())
74
Advertisement
Answer
Try it:
JavaScript
1
123
123
1
'''
2
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
3
QRect, QSize, QUrl, Qt)
4
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
5
QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
6
QRadialGradient)
7
from PySide2.QtWidgets import *
8
'''
9
from PyQt5.Qt import *
10
11
12
class Ui_MainWindow(object):
13
def setupUi(self, MainWindow):
14
if MainWindow.objectName():
15
MainWindow.setObjectName(u"MainWindow")
16
MainWindow.resize(800, 600)
17
self.centralwidget = QWidget(MainWindow)
18
self.centralwidget.setObjectName(u"centralwidget")
19
self.tableWidget = QTableWidget(self.centralwidget)
20
if (self.tableWidget.rowCount() < 100):
21
self.tableWidget.setRowCount(100)
22
self.tableWidget.setObjectName(u"tableWidget")
23
self.tableWidget.setGeometry(QRect(30, 30, 721, 461))
24
25
# self.tableWidget.setStyleSheet('''
26
MainWindow.setStyleSheet('''
27
QTableWidget {
28
background-color:rgb(57, 57, 57);
29
color:rgb(255, 255, 255);
30
border-radius:3px;
31
}
32
33
/* VERTICAL SCROLLBAR */
34
QScrollBar:vertical {
35
border: none;
36
background: rgb(45, 45, 68);
37
width: 14px;
38
margin: 15px 0 15px 0;
39
border-radius: 0px;
40
}
41
/* HANDLE BAR VERTICAL */
42
QScrollBar::handle:vertical {
43
background-color: rgb(80, 80, 122);
44
min-height: 30px;
45
border-radius: 7px;
46
}
47
QScrollBar::handle:vertical:hover {
48
background-color: rgb(255, 0, 127);
49
}
50
QScrollBar::handle:vertical:pressed {
51
background-color: rgb(185, 0, 92);
52
}
53
/* BTN TOP - SCROLLBAR */
54
QScrollBar::sub-line:vertical {
55
border: none;
56
background-color: rgb(59, 59, 90);
57
height: 15px;
58
border-top-left-radius: 7px;
59
border-top-right-radius: 7px;
60
subcontrol-position: top;
61
subcontrol-origin: margin;
62
}
63
QScrollBar::sub-line:vertical:hover {
64
background-color: rgb(255, 0, 127);
65
}
66
QScrollBar::sub-line:vertical:pressed {
67
background-color: rgb(185, 0, 92);
68
}
69
70
/* BTN BOTTOM - SCROLLBAR */
71
QScrollBar::add-line:vertical {
72
border: none;
73
background-color: rgb(59, 59, 90);
74
height: 15px;
75
border-bottom-left-radius: 7px;
76
border-bottom-right-radius: 7px;
77
subcontrol-position: bottom;
78
subcontrol-origin: margin;
79
}
80
QScrollBar::add-line:vertical:hover {
81
background-color: rgb(255, 0, 127);
82
}
83
QScrollBar::add-line:vertical:pressed {
84
background-color: rgb(185, 0, 92);
85
}
86
87
88
/* RESET ARROW vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
89
QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {
90
background: none;
91
}
92
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
93
background: none;
94
}
95
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
96
97
''')
98
self.tableWidget.setRowCount(100)
99
MainWindow.setCentralWidget(self.centralwidget)
100
self.menubar = QMenuBar(MainWindow)
101
self.menubar.setObjectName(u"menubar")
102
self.menubar.setGeometry(QRect(0, 0, 800, 26))
103
MainWindow.setMenuBar(self.menubar)
104
self.statusbar = QStatusBar(MainWindow)
105
self.statusbar.setObjectName(u"statusbar")
106
MainWindow.setStatusBar(self.statusbar)
107
108
QMetaObject.connectSlotsByName(MainWindow)
109
110
111
class MainWindow(QMainWindow):
112
def __init__(self):
113
QMainWindow.__init__(self)
114
self.ui = Ui_MainWindow()
115
self.ui.setupUi(self)
116
117
118
import sys
119
app = QApplication(sys.argv)
120
window = MainWindow()
121
window.show()
122
sys.exit(app.exec_())
123