在QTextEdit上设置滚动条的样式(使用自定义图像)

时间:2018-11-05 09:31:03

标签: python pyqt pyqt5

我下面有一个简单的UI,我想在其中自定义所有内容。我设法通过透明的PNG使用自定义窗口,并且我还尝试制作具有不同状态的自定义按钮,它们都可以正常工作。有谁知道如何在QTextEdit上设置滚动条的样式?

我到处搜索,找不到关于QTextEdit滚动条的任何信息。

#!usr/bin/env python
#-*- coding: utf-8 -*-

import sys
import os.path #file check
from PyQt5 import QtWidgets, QtCore, QtGui #pyqt stuff
from PyQt5.QtCore import QPoint #dragable window
from gui.guiMain import Ui_main

class mainUi(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        self.ver = '2.0.0'
        QtWidgets.QMainWindow.__init__(self, None)
        self.setAcceptDrops(True) #accept file drops
        self.ui = Ui_main()
        self.ui.setupUi(self)
        self.oldPos = None
        self.setFixedSize(401, 286) #no resize
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #frameless window
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
        self.setStyleSheet("background:transparent;")
        self.ui.exitBtn.setStyleSheet("""
                QPushButton {
                    qproperty-iconSize: 32px 32px;
                    image: url(":/res/icons/close.png");
                }
                QPushButton:hover {
                    image: url(":/res/icons/close-hover.png");
                }
                QPushButton:pressed {
                    image: url(":/res/icons/close-focus.png");
                }
            """)
        self.ui.btnBtn.setStyleSheet("""
                QPushButton {
                    qproperty-iconSize: 32px 32px;
                    image: url(":/res/icons/main.png");
                }
                QPushButton:hover {
                    image: url(":/res/icons/main-hover.png");
                }
                QPushButton:pressed {
                    image: url(":/res/icons/main-focus.png");
                }
            """)
        #ui functions
        self.ui.exitBtn.clicked.connect(self._exit)
        self.ui.btnBtn.clicked.connect(self._btnBtn)

        ##########################
        ####### Variables ########
        ##########################
        title = 'Test Window'
        self.setWindowTitle(title)
        self.ui.titleLbl.setText(title)
        self.ui.titleLbl.setStyleSheet('QLabel {color: white; font-size:35px; font-family:IranNastaliq;}')
        text = '<p style="text-align:center; color:white;">'
        sher = """12345
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text..."""
        sher = sher.replace('\n', '<br />')
        text += sher
        text += '</p>'
        self.ui.textEdit.setText(text)
        self.ui.textEdit.setStyleSheet("""
            QTextEdit {
                font-size:15px;
                font-family:Vazir FD;
            }
        """)

    ##############################
    ####### Functions ############
    ##############################
    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        if self.oldPos:
            delta = QPoint (event.globalPos() - self.oldPos)
            #print(delta)
            self.move(self.x() + delta.x(), self.y() + delta.y())
            self.oldPos = event.globalPos()

    def _btnBtn(self):
        print('btnBtn')

    def _exit(self):
        self.close()

    def closeEvent(self, event):
        self.close()

def main():
    app = QtWidgets.QApplication(sys.argv)
    QtGui.QFontDatabase.addApplicationFont(':/res/fonts/Vazir-FD.ttf') #add custom font no install
    QtGui.QFontDatabase.addApplicationFont(':/res/fonts/IranNastaliq.ttf') #add custom font no install
    main = mainUi()
    main.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

为此,您必须先添加一个滚动条,然后使用setVerticalScrollBar将其附加到QTextEdit。然后,您可以自定义QScrollBar本身。这是一个例子。在我的UI文件中,我添加了一个名为sbar的滚动条,该滚动条已附加到textEdit上:

#!usr/bin/env python
#-*- coding: utf-8 -*-

import sys
import os.path #file check
from PyQt5 import QtWidgets, QtCore, QtGui #pyqt stuff
from PyQt5.QtCore import QPoint #dragable window
from gui.guiMain import Ui_main

class mainUi(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        self.ver = '2.0.0'
        QtWidgets.QMainWindow.__init__(self, None)
        self.setAcceptDrops(True) #accept file drops
        self.ui = Ui_main()
        self.ui.setupUi(self)
        self.oldPos = None
        self.setFixedSize(401, 286) #no resize
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #frameless window
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
        self.setStyleSheet("background:transparent;")
        self.ui.exitBtn.setStyleSheet("""
                QPushButton {
                    qproperty-iconSize: 32px 32px;
                    image: url(":/res/icons/close.png");
                }
                QPushButton:hover {
                    image: url(":/res/icons/close-hover.png");
                }
                QPushButton:pressed {
                    image: url(":/res/icons/close-focus.png");
                }
            """)
        self.ui.btnBtn.setStyleSheet("""
                QPushButton {
                    qproperty-iconSize: 32px 32px;
                    image: url(":/res/icons/main.png");
                }
                QPushButton:hover {
                    image: url(":/res/icons/main-hover.png");
                }
                QPushButton:pressed {
                    image: url(":/res/icons/main-focus.png");
                }
            """)
        #ui functions
        self.ui.exitBtn.clicked.connect(self._exit)
        self.ui.btnBtn.clicked.connect(self._btnBtn)

        ##########################
        ####### Variables ########
        ##########################
        title = 'Test Window'
        self.setWindowTitle(title)
        self.ui.titleLbl.setText(title)
        self.ui.titleLbl.setStyleSheet('QLabel {color: white; font-size:35px; font-family:IranNastaliq;}')
        self.ui.sbar.setStyleSheet("""
QScrollBar:vertical {
    border: 1px solid #999999;
    background:white;
    width:10px;
    margin: 0px 0px 0px 0px;
}
QScrollBar::handle:vertical {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));
    min-height: 0px;
}
QScrollBar::add-line:vertical {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));
    height: 0px;
    subcontrol-position: bottom;
    subcontrol-origin: margin;
}
QScrollBar::sub-line:vertical {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop: 0  rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));
    height: 0 px;
    subcontrol-position: top;
    subcontrol-origin: margin;
}
        """)
        self.ui.textEdit.setVerticalScrollBar(self.ui.sbar)
        text = '<p style="text-align:center; color:white;">'
        sher = """12345
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text...
Sample text..."""
        sher = sher.replace('\n', '<br />')
        text += sher
        text += '</p>'
        self.ui.textEdit.setText(text)
        self.ui.textEdit.setStyleSheet("""
            QTextEdit {
                font-size:15px;
                font-family:Vazir FD;
            }
        """)

    ##############################
    ####### Functions ############
    ##############################
    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        if self.oldPos:
            delta = QPoint (event.globalPos() - self.oldPos)
            #print(delta)
            self.move(self.x() + delta.x(), self.y() + delta.y())
            self.oldPos = event.globalPos()

    def _btnBtn(self):
        print('btnBtn')

    def _exit(self):
        self.close()

    def closeEvent(self, event):
        self.close()

def main():
    app = QtWidgets.QApplication(sys.argv)
    QtGui.QFontDatabase.addApplicationFont(':/res/fonts/Vazir-FD.ttf') #add custom font no install
    QtGui.QFontDatabase.addApplicationFont(':/res/fonts/IranNastaliq.ttf') #add custom font no install
    main = mainUi()
    main.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()