How to make a Multi color or two color with different font size QPush button?

Viewed 127

How to make a QPushButton with different text colors. For example, In my case, I need every first letter of the Button Text in Red colour and the remaining will be in blue colour, And Also the font of the first letter will be a little bit bigger than the remaining Letter ( all in PyQt5, through Googling, I will found code in C++ format, I am Not able to convert it into PyQt5)?

import sys, os
from PyQt5.QtWidgets import *

class Navigate_Between(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Mulit Color Text In Buttons")

        self.dashboard = QPushButton()
        self.dashboard.setText("DashBoard")
        # text = QTextDocument()
        # text.setHtml("<h2><i>Dash Board</i>""<font color=red>Qt!</font></h2>");
        # pixmap = QPixmap()
        # textimage  = pixmap.scaled(self.width(),self.height())
        # textimage.fill(Qt.transparent)
        # painter = QPainter(textimage)
        #
        # icon = QIcon()
        # self.dashboard.setIcon(icon)
        # self.dashboard.setIconSize(textimage.rect().size())

        self.file = QPushButton("File")
        self.master = QPushButton("Master")
        self.transcation = QPushButton("Transcation")
        self.reports = QPushButton("Reports")
        self.others = QPushButton("Others")

        layout = QHBoxLayout()
        layout.addWidget(self.dashboard)
        layout.addWidget(self.file)
        layout.addWidget(self.master)
        layout.addWidget(self.transcation)
        layout.addWidget(self.reports)
        layout.addWidget(self.others)
        self.setLayout(layout)

def main():
    app = QApplication(sys.argv)
    mainwindow = Navigate_Between()
    mainwindow.show()
    sys.exit(app.exec_())
if __name__ =="__main__":
    main()
1 Answers

You have to create a QPixmap where the content of the QTextDocument is rendered.

class Navigate_Between(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Mulit Color Text In Buttons")

        self.dashboard = QPushButton()

        document = QTextDocument()
        document.setDocumentMargin(0)
        document.setHtml("<h2><i>Dash Board</i> <font color=red>Qt!</font></h2>")
        
        pixmap = QPixmap(document.size().toSize())
        pixmap.fill(Qt.transparent)
        painter = QPainter(pixmap)
        document.drawContents(painter)
        painter.end()

        icon = QIcon(pixmap)
        self.dashboard.setIcon(icon)
        self.dashboard.setIconSize(pixmap.size())

        self.file = QPushButton("File")
        self.master = QPushButton("Master")
        self.transcation = QPushButton("Transcation")
        self.reports = QPushButton("Reports")
        self.others = QPushButton("Others")

        layout = QHBoxLayout(self)
        layout.addWidget(self.dashboard)
        layout.addWidget(self.file)
        layout.addWidget(self.master)
        layout.addWidget(self.transcation)
        layout.addWidget(self.reports)
        layout.addWidget(self.others)

enter image description here

Related