PDA

View Full Version : Using Fusion Style with QtableWidget tweaks widgets placed in cells



chmedly
8th August 2017, 01:23
I'm working on a Python app that is intended to be run on both Mac and Windows. I'm using Python 3 and PyQt5.

In an effort to create a suitable QPushButton that can turn red when checked, I started experimenting with using the Fusion Style instead of the default system styles. The red background pushbutton works great with Fusion but it breaks my table layouts. I'm finding that the layout within in each cell of a QTableWidget is doing weird things. Here's some example code for the main window and the imported "cells". Un-commenting, the setStyle line at the bottom will show how it tweaks with the widgets. It seems like the cell wants to have a lot of extra space vertically and will crush or displace the widget if necessary. I want to know what is happening that's causing this and why it's so different from the system styles of both mac and Windows 10.



from PyQt5 import QtCore, QtGui, QtWidgets
from formmatrix import Ui_FormMatrixCell as formmatrix

class Ui_BankDialog(object):
def setupUi(self, BankDialog):
self.groupBox = QtWidgets.QGroupBox(BankDialog)
self.groupBox.setMinimumSize(QtCore.QSize(250, 380))
self.groupBox.setTitle("Choose which teller to rob in each Bank.")
self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout.setContentsMargins(1, 1, 1, 1)
self.tbBank1 = QtWidgets.QTableWidget(self.groupBox)
self.tbBank1.setFocusPolicy(QtCore.Qt.StrongFocus)
self.tbBank1.setColumnCount(8)
self.tbBank1.setRowCount(8)
self.gridLayout.addWidget(self.tbBank1, 0, 0, 1, 1)
self.buttonBox = QtWidgets.QDialogButtonBox(BankDialog)
self.buttonBox.setFocusPolicy(QtCore.Qt.StrongFocu s)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal )
self.buttonBox.setStandardButtons(QtWidgets.QDialo gButtonBox.Ok)
self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)

for w in range(8):
for l in range (8):
box = QtWidgets.QWidget()
box.ui = formmatrix()
box.ui.setupUi(box)
box.ui.gridLayout.setContentsMargins(1, 1, 1, 1)
self.tbBank1.setCellWidget((w),(l),box)
self.tbBank1.resizeColumnsToContents()
self.tbBank1.resizeRowsToContents()
self.buttonBox.accepted.connect(BankDialog.accept)

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
#app.setStyle(QtWidgets.QStyleFactory.create('Fusi on'))
BankDialog = QtWidgets.QDialog()
ui = Ui_BankDialog()
ui.setupUi(BankDialog)
BankDialog.show()
sys.exit(app.exec_())

And the widgets that get placed in cells. This file is named "formmatrix.py".



from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_FormMatrixCell(object):
def setupUi(self, FormMatrixCell):
FormMatrixCell.resize(62, 40)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
FormMatrixCell.setSizePolicy(sizePolicy)
FormMatrixCell.setMinimumSize(QtCore.QSize(0, 35))
FormMatrixCell.setMaximumSize(QtCore.QSize(1677721 5, 35))
FormMatrixCell.setFocusPolicy(QtCore.Qt.StrongFocu s)
self.gridLayout = QtWidgets.QGridLayout(FormMatrixCell)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QtWidgets.QGroupBox(FormMatrixCell)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
self.groupBox.setSizePolicy(sizePolicy)
self.groupBox.setMinimumSize(QtCore.QSize(0, 35))
font = QtGui.QFont()
font.setPointSize(10)
self.groupBox.setFont(font)
self.groupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout_2.setContentsMargins(2, 2, 2, 2)
self.gridLayout_2.setSpacing(0)
self.spinBox = QtWidgets.QSpinBox(self.groupBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
self.spinBox.setSizePolicy(sizePolicy)
self.spinBox.setMinimumSize(QtCore.QSize(42, 0))
self.spinBox.setMaximumSize(QtCore.QSize(52, 22))
font = QtGui.QFont()
font.setPointSize(12)
self.spinBox.setFont(font)
self.spinBox.setFocusPolicy(QtCore.Qt.WheelFocus)
self.spinBox.setFrame(True)
self.gridLayout_2.addWidget(self.spinBox, 0, 0, 2, 1)
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FormMatrixCell = QtWidgets.QWidget()
ui = Ui_FormMatrixCell()
ui.setupUi(FormMatrixCell)
FormMatrixCell.show()
sys.exit(app.exec_())

chmedly
8th August 2017, 18:23
Here are some example images of what I'm seeing on MacOS.

This is without the Fusion style applied.
12540

This is with the Fusion style applied. Everything else is the same.
12541

On Windows the spinboxes completely disappear (I think they're hidden below the lower edge of each cell).