Two Colours Text in Qpushbutton

Two colours text in QPushButton

You can derive from QPushButton and draw text yourself via QPainter in paintEvent.

class Button : public QPushButton
{
Q_OBJECT

public:
Button(QWidget *parent = 0)
: QPushButton(parent)
{ }

void paintEvent(QPaintEvent *p)
{
QPushButton::paintEvent(p);
QPainter paint(this);
paint.drawText(QPoint(10,10),"Hello");
}
};

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

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)

Sample Image

How to change QPushButton text and background color

Apart from some inconsistencies with your code example setting the background color and text color of a QPushButton works just fine with:

setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')

Example (using PySide):

from PySide import QtGui

app = QtGui.QApplication([])

button = QtGui.QPushButton()
button.setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')
button.setText('Press Me')
menu = QtGui.QMenu()
menuItem1 = menu.addAction('Menu Item1')
menuItem2 = menu.addAction('Menu Item2')

button.setMenu(menu)
button.show()

app.exec_()

results in:

Sample Image

How to create a two or more Color Custom QPushButton in PyQt5?

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class MyButton(QPushButton):
def __init__(self, Text, parent = None):
super(MyButton, self).__init__()

mydocument = QTextDocument()
mydocument.setDocumentMargin(0)
mydocument.setHtml(Text)

mypixmap = QPixmap(mydocument.size().toSize())
mypixmap.fill(Qt.transparent)
mypainter = QPainter(mypixmap)
mydocument.drawContents(mypainter)
mypainter.end()

myicon = QIcon(mypixmap)
self.setIcon(myicon)
self.setIconSize(mypixmap.size())

class mainwindow(QWidget):
def __init__(self , parent = None):
super(mainwindow, self).__init__()
self.setupgui()
def setupgui(self):
self.resize(800,600)
self.setWindowTitle('Custom Button With two Color Text')
newLayout = QHBoxLayout()

self.dashboard = MyButton("<h2><i>Dash Board</i> <font color=red>Qt!</font></h2>",self)
self.transcation = MyButton('<font color="red"><u>T</u></font><font color="black">ranscation</font>',self)
newLayout.addWidget(self.dashboard)
newLayout.addWidget(self.transcation)
self.setLayout(newLayout)
self.show()

def main():
app = QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

Sample Image

display two color font on QToolButton

The QPainter methods are not executed immediately since for efficiency reasons what these methods do is accumulate instructions. And that's what happens in your case since the QPainter is still active and has the property of the QPixmap, that's why you get that error, so in this case the solution is to invoke the end() method:

text = QtGui.QTextDocument()
text.setHtml('<b>'
'<font color="#0085C8" size="6" face="Avenir">'
'Select Phase'
'</font>'
'<font color="#d8d8d8" size="6">'
'▼'
'</font>'
'</b>')

pixmap = QtGui.QPixmap(text.size().toSize())
pixmap.fill(QtCore.Qt.transparent)

painter = QtGui.QPainter(pixmap)
text.drawContents(painter, QtCore.QRectF(pixmap.rect()))
painter.end() # <-- this line

icon = QtGui.QIcon(pixmap)
btn.setIcon(icon)
btn.setIconSize(pixmap.size())

Set the colour of text in a QPushButton in PyQt4 using Python

You can use a Style Sheet.

testbutton.setStyleSheet('QPushButton {color: blue}')

QPushbutton: Two different Font size

Derive from QPushButton and draw the text yourself. You can refer this post for reference.
Two colours text in QPushButton

how to create a qt QTButton with multiple colors?

Use a gradient:

button->setStyleSheet(R"(
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 102, 255), stop:0.55 rgba(0, 148, 61, 255), stop:0.98 rgba(0, 255, 0, 255), stop:1 rgba(0, 0, 0, 0));
border-radius: 10px;
)");

Sample Image

Cleaner way for changing button's text color on signal pressed & released

This is the way to do it with Qt Style Sheet, the ones that handle the states like the pressed button.

self.ui.pButton_save.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: green}")
self.ui.pButton_cancel.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: red}")


Related Topics



Leave a reply



Submit