Search code examples
pythonpyqt5

How to change the scroll bar style in QTextBrowser?


I cant change th e style of this scroll bar which is in QTextBrowser:

img of app

also code from other answers doesnt work

*I use ui file and big part of css is in it

Code:

from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QFile, QTextStream
import datetime
import keyboard
import pyautogui

app = QtWidgets.QApplication([])
ui = uic.loadUi("data/design.ui")

ui.setWindowIcon(QIcon("data/img.jpg"))
ui.setWindowTitle("color_picker")


def change_bind():
    global hotkey_
    keyboard.remove_hotkey(hotkey_)
    hotkey = keyboard.read_event(suppress=True)
    keyboard.add_hotkey(hotkey.name, change_color)

    ui.current_key.setText(hotkey.name)
    hotkey_ = hotkey.name


def change_color():
    x, y = pyautogui.position()
    color = pyautogui.pixel(x, y)
    hex_color = '#{:02x}{:02x}{:02x}'.format(*color)
    font_size = "12px"
    text = f"<span style='color: #1B2223; font-size: {font_size};'>[{datetime.datetime.now().strftime('%H:%M')}]</span>\
             <span style='color: #0EF6CC; font-size: {font_size};'> {hex_color}\n</span>"
    ui.history.insertHtml(text)


css_file = QFile("data/style.css")
if css_file.open(QFile.ReadOnly | QFile.Text):
    stream = QTextStream(css_file)
    css_content = stream.readAll()
    css_file.close()
    ui.setStyleSheet(css_content)

hotkey_ = 'alt'
keyboard.add_hotkey(hotkey_, change_color)
ui.change_key.clicked.connect(change_bind)
ui.show()
app.exec()

Css:

QMainWindow {
    background-color: #1B2223;
}

Solution

  • lmao, PyQt never had css, it has QSS, which is something Qt invented that looks very similar to css.

    in your case, you just want to change QScrollBar:vertical (which is not something that exists in css). unless you plan on making your window dynamic, just using

    QScrollBar:vertical { (add the changes you want here) }
    

    inside data/style.css should fix it.

    btw, if you want to learn more, this video should hopefully explain the basics of qss, and you can just use chatgpt for the rest