Python PyQt5 QTreeView set row Background Colour - python

I am trying to set the background colour (color) for a) a whole QTreeView, and b) for specific rows in a QTreeView within Python.
I have found setColor and setBackgroundColor methods, but neither seem to work for me with QTreeView nor QStandardItem.
Lots of googling shows many conversations about it, but I have not been able to relate those to my code below.
Full Code is below, but two attempts to set the colour are:
InGate = QTreeView()
InGate.setColor(QtGui.QColor(255, 100, 0, 255))
and
for i, d in enumerate(data):
model.setItem(i, QStandardItem(d))
model.setBackgroundColor(QtGui.QColor(255, 100, i, 255))
Any help appreciated.
Thanks very much
Kevin
Sorry the code example is fairly long, but I have cut it down to what I think is a minimal working example:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QGridLayout, QWIDGETSIZE_MAX
from PyQt5.QtWidgets import QTreeView, QApplication
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QFont, QFontMetrics
import sys
class StartMarshall(QMainWindow):
def __init__(self):
super().__init__()
self.data = ['XXX' for _ in range(8)]
# initialize the UI
self.initUI()
def initUI(self):
self.setWindowTitle('Start')
# Build Central Widget
self.widget = QWidget()
self.setCentralWidget(self.widget)
# Labels
lblInGate = QLabel('In Gate:', self)
lblInQueue = QLabel('In Queue:', self)
grid = QGridLayout()
grid.setSpacing(10)
# intialise view of data
InGate = QTreeView()
self.InQueue = InQueue = QTreeView()
# Tried to set colour of whole QTreeView here.
#InGate.setColor(QtGui.QColor(255, 100, 0, 255))
fontSize = 12
# Fixed Font
font = QFont("monospace",fontSize)
font.setStyleHint(QFont.TypeWriter)
fontMet = QFontMetrics(font)
padd = 4
oneLineHeight = fontMet.lineSpacing() + padd
lblInGate.setFont(font)
lblInQueue.setFont(font)
InGate.setFont(font)
InQueue.setFont(font)
MinWidth = 500
# set max size of QTree Views
InGate.setMaximumSize(QWIDGETSIZE_MAX, oneLineHeight)
InQueue.setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
# set min size of QTree Views
InGate.setMinimumSize(MinWidth, oneLineHeight)
InQueue.setMinimumSize(MinWidth, oneLineHeight)
InQueue.setRootIsDecorated(False)
InQueue.setAlternatingRowColors(True)
# Setup View Models
self.InGateModel = self.prepModel(InGate)
self.InQueueModel = self.prepModel(InQueue)
# include the widgets
grid.addWidget(lblInGate, 2, 0)
grid.addWidget(InGate, 2, 1)
grid.addWidget(lblInQueue, 3, 0)
grid.addWidget(InQueue, 3, 1, -1, -1)
self.widget.setLayout(grid)
# Show QMainWindow
self.show()
self.displayRacers()
def prepModel(self, widget):
# initialize a model
model = QStandardItemModel()
# remove indentation and headers
widget.setIndentation(0)
widget.setHeaderHidden(1)
# add (data) model to widget
widget.setModel(model)
return model
def fillModel(self, model, data):
# for refilling model data
for i, d in enumerate(data):
model.setItem(i, QStandardItem(d))
#model.setBackgroundColor(QtGui.QColor(255, 100, i, 255))
return
def displayRacers(self):
self.fillModel(self.InGateModel, self.data[1:2])
# show the full queue (-1 doesnt show last racer?)
self.fillModel(self.InQueueModel, self.data[2:len(self.data)])
return
# Main
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = StartMarshall()
sys.exit(app.exec_())

To set the whole of a QTreeView Background Colour this works for me:
IG = QTreeView()
IG.setStyleSheet("background-color: green");
To set a specific QStandardItemModel Item Background Colour this works for me:
self.IQModel.setData(self.IQModel.index(0, 0), QBrush(QColor(255, 0, 0)), QtCore.Qt.BackgroundRole)
And for completeness, to set the font color, this works for me:
self.InGateModel.setData(self.InQueueModel.index(0, 0), QBrush(Qt.white), QtCore.Qt.ForegroundRole)
Thanks very much to all those that have guided me in finding the answer.
Kevin

Related

How to convert QLabel to QTableWidgetItem to put into QTableWidget

I want to add an icon to my QTableWidget. However, the icon being added is pretty small, so I try to find a way to resize the icon
I have tried using setSizeHint(), but it didn't work. So I thought of creating a pixmap and set the pixmap in QLabel, but I couldn't figure out to convert the QLabel into QTabelWidgetItem.
this is the code in two different approaches
##this is when I try to use setSizeHint()
class test_UI(Ui_MainWindow,QtWidgets.QMainWindow)
def set_icon(self):
icon_item=QtWidgets.QTableWidgetItem()
icon_item.setSizeHint(QtCore.QSize(100,100))
icon_item.setIcon(QtGui.QIcon("Kevin_test.png"))
self.tableWidget.setItem(0,1,icon_item)
##this is when I try to use pixmap to put it inside the table
class test_UI(Ui.MainWindow,QtWidgets.QMainWindow)
def set_icon(self):
icon_item=QtWidgets.QTableWidgetItem(self.label)
icon_item.setFlags(QtCore.Qt.ItemIsEditable)
self.tableWidget.setItem(0,1,icon_item)
def build_icon(self):
self.icon = QtGui.QIcon("Kevin_test.png")
self.label=QtWidgets.QLabel('pic',self)
self.label.setFixedSize(300,300)
pixmap1=self.icon.pixmap(100,100,QtGui.QIcon.Active,QtGui.QIcon.On)
self.label.setPixmap(pixmap1)
For the first approach, I expect the size of the icon to change but it did not.
For the second approach, my program crash because there is no overload call to make QTableWidgetItem with a QLabel.
There are at least the following methods:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Delegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
option.decorationSize = index.data(QtCore.Qt.SizeHintRole)
def main(args):
app = QtWidgets.QApplication(args)
# sol1
widget_1 = QtWidgets.QTableWidget(1, 1)
it1 = QtWidgets.QTableWidgetItem()
widget_1.setItem(0, 0, it1)
it1.setIcon(QtGui.QIcon("so-logo.png"))
it1.setSizeHint(QtCore.QSize(100, 100))
widget_1.setIconSize(QtCore.QSize(100, 100))
# sol2
widget_2 = QtWidgets.QTableWidget(1, 1)
it2 = QtWidgets.QTableWidgetItem()
widget_2.setItem(0, 0, it2)
label = QtWidgets.QLabel()
pixmap = QtGui.QPixmap("so-logo.png")
""" scaled
pixmap = pixmap.scaled(
QtCore.QSize(400, 400),
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation,
)"""
size = pixmap.size()
label.setPixmap(pixmap)
it2.setSizeHint(size)
label.setFixedSize(size)
widget_2.setCellWidget(0, 0, label)
# sol3
widget_3 = QtWidgets.QTableWidget(1, 1)
it3 = QtWidgets.QTableWidgetItem()
widget_3.setItem(0, 0, it3)
it3.setIcon(QtGui.QIcon("so-logo.png"))
it3.setSizeHint(QtCore.QSize(100, 100))
delegate = Delegate(widget_3)
widget_3.setItemDelegate(delegate)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
lay.addWidget(widget_1)
lay.addWidget(widget_2)
lay.addWidget(widget_3)
w.show()
ret = app.exec_()
return ret
if __name__ == "__main__":
sys.exit(main(sys.argv))
Explanation:
By default the icon size is taken based on the iconSize property.
The QLabel can be added using the setCellWidget() method.
You can use a delegate to set the icon size.

QVariantAnimation of opacity : the item appears at the end of the animation

here is an example where the graphicsitem appears at the end of the animation, and not progressively, as it should.
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QEasingCurve
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QGraphicsItem
class QStone(QGraphicsPixmapItem):
def __init__(self):
QGraphicsPixmapItem.__init__(self)
white = QPixmap("white2.png")
self.setPixmap(white.scaled(60, 60, Qt.KeepAspectRatio))
self.w = self.boundingRect().width()
self.h = self.boundingRect().height()
class QBoard(QGraphicsView):
def __init__(self,scene):
QGraphicsView.__init__(self)
self.scene=scene
self.setScene(scene)
def display_stone(self, x, y):
stone = QStone()
stone.setZValue(10)
stone.setOpacity(0)
stone.setPos(x - stone.w / 2, y - stone.h / 2)
self.scene.addItem(stone)
animation = QtCore.QVariantAnimation(self.scene)
animation.setDuration(3000)
animation.valueChanged.connect(stone.setOpacity)
# animation.setStartValue(0)
# animation.setEndValue(1)
animation.setParent(self.scene)
animation.setEasingCurve(QEasingCurve.BezierSpline)
animation.start()
class MainWindow(QMainWindow):
def __init__(self):
#all the usual stuff
QMainWindow.__init__(self)
centralWidget = QtWidgets.QWidget(self)
self.setCentralWidget(centralWidget)
mainLayout = QtWidgets.QGridLayout()
centralWidget.setLayout(mainLayout)
self.scene = QGraphicsScene()
self.view = QBoard(self.scene)
mainLayout.addWidget(self.view,0,0)
self.scene.setSceneRect(-200.0,-150.0,400.0,300.0)
self.view.display_stone(0,0)
app = QtWidgets.QApplication(sys.argv)
main_win = MainWindow()
main_win.show()
sys.exit(app.exec_())
instead of white2.png, plz put any image file.
any idea why it works like this?
all is said, I could also use QPropertyAnimation but it is more work for maybe the same result.
QVariantAnimation generates an animation using the type of data deducted by the value passed in startValue and endValue, in your case by not placing it that implies using integer, or placing 0 and 1 that is the same makes integer values be used in the interpolation. What integer values can be interpolated between 0 and 1? because only 0 and 1, for example for t = 0.5 * T, the opacity value should be 0.5 considering if it is linear but how to use integers then the rounding set it to 0, and it will only be visible when t = T. The solution is to pass it as startValue at 0.0 and endValue at 1.0.
animation = QtCore.QVariantAnimation(self.scene)
animation.setDuration(3000)
animation.valueChanged.connect(stone.setOpacity)
animation.setStartValue(0.0) # <---
animation.setEndValue(1.0) # <---
animation.setParent(self.scene)
animation.setEasingCurve(QEasingCurve.BezierSpline)
animation.start()

How to reduce the size of a QComboBox with PyQt?

I created a small program with PyQt in which I put a QComboBox, but this one contains only a list of 2 letters. The program window being small, to save space, I would like to reduce the width of the QComboBox.
This is what it looks like now. The width is too large.
I searched the internet, but after a lot of searching time, I still haven't found anything. Thank you in advance if you have an idea.
There are several methods to resize a widget's size. Lets say the QComboBox is defined as this:
combo = QComboBox(self)
One way is to use QWidget.resize(width, height)
combo.resize(200,100)
To obtain a proper size automatically, you can use QWidget.sizeHint() or sizePolicy()
combo.resize(combo.sizeHint())
If you want to set fixed size, you can use setFixedSize(width, height), setFixedWidth(width), or setFixedHeight(height)
combo.setFixedSize(400,100)
combo.setFixedWidth(400)
combo.setFixedHeight(100)
Here's an example:
from PyQt5.QtWidgets import (QWidget, QLabel, QComboBox, QApplication)
import sys
class ComboboxExample(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel("Ubuntu", self)
self.combo = QComboBox(self)
self.combo.resize(200,25)
# self.combo.resize(self.combo.sizeHint())
# self.combo.setFixedWidth(400)
# self.combo.setFixedHeight(100)
# self.combo.setFixedSize(400,100)
self.combo.addItem("Ubuntu")
self.combo.addItem("Mandriva")
self.combo.addItem("Fedora")
self.combo.addItem("Arch")
self.combo.addItem("Gentoo")
self.combo.move(25, 25)
self.label.move(25, 75)
self.combo.activated[str].connect(self.onActivated)
# self.setGeometry(0, 0, 500, 125)
self.setWindowTitle('QComboBox Example')
self.show()
def onActivated(self, text):
self.label.setText(text)
self.label.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ComboboxExample()
sys.exit(app.exec_())

Resize multiple labels containing images with changing window size

I have a window that has six symmetrically placed labels, all showing images (designed using qt-designer with the help of layouts). I would like to resize these images according to the changing window size. I have found some help in previous questions like: PyQt: Detect resizing in Widget-window resized signal
At present, using resizeEvent() in my case does not shrink the images according to the resize function. It is already triggered with the display of my form window thereby making the pushButton useless. Above all, the resulting execution is very slow. My images are of 2058x1536 dimension and displayed transparently.
My qt-designer code is given here: https://pastebin.com/TzM6qiKZ
import Ui_ImageCrop_Test
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QImage, QPainter, QColor
from PyQt5.QtCore import Qt
class ImageCrop(Ui_ImageCrop_Test.Ui_MainWindow, QMainWindow):
def __init__(self, parent=None):
super(ImageCrop, self).__init__()
self.setupUi(self)
self.transparency = 220
with open("Img_files.txt") as file:
self.img_files = file.read().splitlines()
self.length = len(self.img_files)
self.pushButton_1.clicked.connect(self.click1)
self.label_1.resizeEvent = self.click1
def click1(self, event):
for i in range(6):
image = QImage(self.img_files[i])
image = image.convertToFormat(QImage.Format_ARGB8565_Premultiplied)
p = QPainter(image)
p.setCompositionMode(QPainter.CompositionMode_DestinationIn)
p.fillRect(image.rect(), QColor(0, 0, 0, self.transparency))
p.end()
pixmap = QPixmap(image)
w = int(self.label_1.width() - 4.0)
h = int(self.label_1.height() - 4.0)
smaller_pixmap = pixmap.scaled(w, h, Qt.IgnoreAspectRatio, Qt.FastTransformation)
if i == 0:
self.label_1.setPixmap(smaller_pixmap)
if i == 1:
self.label_2.setPixmap(smaller_pixmap)
if i == 2:
self.label_3.setPixmap(smaller_pixmap)
if i == 3:
self.label_4.setPixmap(smaller_pixmap)
if i == 4:
self.label_5.setPixmap(smaller_pixmap)
if i == 5:
self.label_6.setPixmap(smaller_pixmap)
def main():
app = QApplication(sys.argv)
form1 = ImageCrop()
form1.show()
app.exec_()
if __name__ == '__main__': main()
Is there any solution to run this code faster? For example, I was thinking to make all my labels turn blank during a mouse click at the edge of my window and then images reappear after the mouse button is released. This does not seem so neat. Also, I am not sure if using paintEvent can reduce my lag. Thank you for your suggestions and comments.
QLabel has the scaledContents property that allows the image to scale automatically:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import Ui_ImageCrop_Test
class ImageCrop(QtWidgets.QMainWindow, Ui_ImageCrop_Test.Ui_MainWindow):
def __init__(self, parent=None):
super(ImageCrop, self).__init__()
self.setupUi(self)
self.pushButton_1.clicked.connect(self.click1)
self.transparency = 220
with open("Img_files.txt") as file:
self.img_files = file.read().splitlines()
#QtCore.pyqtSlot()
def click1(self):
labels = [self.label_1, self.label_2, self.label_3,
self.label_4, self.label_5, self.label_6]
for label, filename in zip(labels, self.img_files):
image = QtGui.QImage(filename)
image = image.convertToFormat(QtGui.QImage.Format_ARGB8565_Premultiplied)
p = QtGui.QPainter(image)
p.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationIn)
p.fillRect(image.rect(), QtGui.QColor(0, 0, 0, self.transparency))
p.end()
pixmap = QtGui.QPixmap(image)
w = int(label.width() - 4.0)
h = int(label.height() - 4.0)
smaller_pixmap = pixmap.scaled(w, h, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.FastTransformation)
label.setPixmap(smaller_pixmap)
label.setScaledContents(True)
def main():
app = QtWidgets.QApplication(sys.argv)
form1 = ImageCrop()
form1.show()
app.exec_()
if __name__ == '__main__': main()

Pyqt5 - grid layout misbehaving

So I'm trying to get a grip on Qt (more specifically, Pyqt), and I want to create a simple feedback form. It should have
a title
a name ('author')
a message
a send and a cancel button
Let's try without the buttons, first (the App class just provides a button to create a popup. the question concerns the Form class below it):
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QDesktopWidget,\
QHBoxLayout, QVBoxLayout, QGridLayout,\
QPushButton, QLabel,QLineEdit, QTextEdit,\
qApp
from PyQt5.QtGui import QIcon
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5 Layout Demo'
self.popup = None
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setWindowIcon(QIcon('imgs/python3.png'))
formButton = QPushButton("show form")
formButton.clicked.connect(self.showPopup)
formBox = QHBoxLayout()
formBox.addWidget(formButton)
formBox.addStretch(1)
vbox = QVBoxLayout()
vbox.addLayout(formBox)
vbox.addStretch(1)
# self.setLayout(vbox) # would work if this was a QWidget
# instead, define new central widget
window = QWidget()
window.setLayout(vbox)
self.setCentralWidget(window)
self.center(self)
self.show()
#staticmethod
def center(w: QWidget):
qr = w.frameGeometry() # get a rectangle for the entire window
# center point = center of screen resolution
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp) # move center of rectangle to cp
w.move(qr.topLeft()) # move top-left point of window to top-let point of rectangle
def showPopup(self):
if self.popup is None:
self.popup = Form(self)
self.popup.setGeometry(10, 10, 300, 400)
self.center(self.popup)
self.popup.show()
class Form(QWidget):
def __init__(self, main):
super().__init__()
self.initUI()
self.main = main
def initUI(self):
self.setWindowTitle('Feedback')
self.setWindowIcon(QIcon('imgs/python3.png'))
title = QLabel('Title')
author = QLabel('Author')
message = QLabel('Message')
titleEdit = QLineEdit()
authorEdit = QLineEdit()
messageEdit = QTextEdit()
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(titleEdit,1, 1)
grid.addWidget(author, 2, 0)
grid.addWidget(authorEdit,2, 1)
grid.addWidget(message, 3, 0)
grid.addWidget(messageEdit, 4, 0, 6, 0)
self.setLayout(grid)
# probably should delegate to self.main, but bear with me
def send(self):
self.main.popup = None
self.hide()
def cancel(self):
self.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Ok, looks about right. There's a bit too much spacing in-between the line edits and the text edit, but since I want to add some buttons below it, that should be be a problem.
So I add:
sendBtn = QPushButton("send")
cancelBtn = QPushButton("cancel")
sendBtn.clicked.connect(self.send)
cancelBtn.clicked.connect(self.cancel)
grid.addWidget(sendBtn, 7, 1)
grid.addWidget(cancelBtn, 7, 2)
which yields
Now obviously, I forgot to stretch the title and author line edits to the newly introduced column 2. Easy enough to fix but what really bothers me is the placement of the buttons.
WHY do they show up in the middle of the text edit? I can see how Qt chooses the column size, and why that would lead to the buttons' being of different size, but since the tutorial doesn't actually add buttons to the form, I have no idea how to fix that.
I could, of course, simply add boxes:
sendBtn = QPushButton("send")
cancelBtn = QPushButton("cancel")
sendBtn.clicked.connect(self.send)
cancelBtn.clicked.connect(self.cancel)
btns = QHBoxLayout()
btns.addStretch(1)
btns.addWidget(sendBtn)
btns.addWidget(cancelBtn)
l = QVBoxLayout()
l.addLayout(grid)
l.addLayout(btns)
self.setLayout(l)
With which the popup then actually starts looking closer to something acceptable:
But is there a way to fix this within the grid layout, instead?
You seem to have misunderstood the signature of addWidget. The second and third arguments specify the row and column that the widget is placed in, whilst the third and fourth specify the row-span and column-span.
In your example, the problems start here:
grid.addWidget(message, 3, 0)
grid.addWidget(messageEdit, 4, 0, 6, 0)
where you make the text-edit span six rows and zero columns - which I doubt is what you intended. Instead, you probably want this:
grid.addWidget(message, 3, 0, 1, 2)
grid.addWidget(messageEdit, 4, 0, 1, 2)
which will make the message label and text-edit span the two columns created by the title and author fields above.
Now when you add the buttons, they must have a layout of their own, since the top two rows are already determining the width of the two columns. If you added the buttons directly to the grid, they would be forced to have the same widths as the widgets in the top two rows (or vice versa). So the buttons should be added like this:
hbox = QHBoxLayout()
sendBtn = QPushButton("send")
cancelBtn = QPushButton("cancel")
sendBtn.clicked.connect(self.send)
cancelBtn.clicked.connect(self.cancel)
hbox.addStretch()
hbox.addWidget(sendBtn)
hbox.addWidget(cancelBtn)
grid.addLayout(hbox, 5, 0, 1, 2)

Categories