2

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.