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.

Qt Code:
  1. from PyQt5 import QtCore, QtGui, QtWidgets
  2. from formmatrix import Ui_FormMatrixCell as formmatrix
  3.  
  4. class Ui_BankDialog(object):
  5. def setupUi(self, BankDialog):
  6. self.groupBox = QtWidgets.QGroupBox(BankDialog)
  7. self.groupBox.setMinimumSize(QtCore.QSize(250, 380))
  8. self.groupBox.setTitle("Choose which teller to rob in each Bank.")
  9. self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
  10. self.gridLayout.setContentsMargins(1, 1, 1, 1)
  11. self.tbBank1 = QtWidgets.QTableWidget(self.groupBox)
  12. self.tbBank1.setFocusPolicy(QtCore.Qt.StrongFocus)
  13. self.tbBank1.setColumnCount(8)
  14. self.tbBank1.setRowCount(8)
  15. self.gridLayout.addWidget(self.tbBank1, 0, 0, 1, 1)
  16. self.buttonBox = QtWidgets.QDialogButtonBox(BankDialog)
  17. self.buttonBox.setFocusPolicy(QtCore.Qt.StrongFocus)
  18. self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
  19. self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Ok)
  20. self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
  21.  
  22. for w in range(8):
  23. for l in range (8):
  24. box = QtWidgets.QWidget()
  25. box.ui = formmatrix()
  26. box.ui.setupUi(box)
  27. box.ui.gridLayout.setContentsMargins(1, 1, 1, 1)
  28. self.tbBank1.setCellWidget((w),(l),box)
  29. self.tbBank1.resizeColumnsToContents()
  30. self.tbBank1.resizeRowsToContents()
  31. self.buttonBox.accepted.connect(BankDialog.accept)
  32.  
  33. if __name__ == "__main__":
  34. import sys
  35. app = QtWidgets.QApplication(sys.argv)
  36. #app.setStyle(QtWidgets.QStyleFactory.create('Fusion'))
  37. BankDialog = QtWidgets.QDialog()
  38. ui = Ui_BankDialog()
  39. ui.setupUi(BankDialog)
  40. BankDialog.show()
  41. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 

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

Qt Code:
  1. from PyQt5 import QtCore, QtGui, QtWidgets
  2.  
  3. class Ui_FormMatrixCell(object):
  4. def setupUi(self, FormMatrixCell):
  5. FormMatrixCell.resize(62, 40)
  6. sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
  7. sizePolicy.setHorizontalStretch(0)
  8. sizePolicy.setVerticalStretch(0)
  9. FormMatrixCell.setSizePolicy(sizePolicy)
  10. FormMatrixCell.setMinimumSize(QtCore.QSize(0, 35))
  11. FormMatrixCell.setMaximumSize(QtCore.QSize(16777215, 35))
  12. FormMatrixCell.setFocusPolicy(QtCore.Qt.StrongFocus)
  13. self.gridLayout = QtWidgets.QGridLayout(FormMatrixCell)
  14. self.gridLayout.setContentsMargins(0, 0, 0, 0)
  15. self.gridLayout.setSpacing(0)
  16. self.gridLayout.setObjectName("gridLayout")
  17. self.groupBox = QtWidgets.QGroupBox(FormMatrixCell)
  18. sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
  19. sizePolicy.setHorizontalStretch(0)
  20. sizePolicy.setVerticalStretch(0)
  21. self.groupBox.setSizePolicy(sizePolicy)
  22. self.groupBox.setMinimumSize(QtCore.QSize(0, 35))
  23. font = QtGui.QFont()
  24. font.setPointSize(10)
  25. self.groupBox.setFont(font)
  26. self.groupBox.setFocusPolicy(QtCore.Qt.NoFocus)
  27. self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
  28. self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
  29. self.gridLayout_2.setContentsMargins(2, 2, 2, 2)
  30. self.gridLayout_2.setSpacing(0)
  31. self.spinBox = QtWidgets.QSpinBox(self.groupBox)
  32. sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
  33. sizePolicy.setHorizontalStretch(0)
  34. sizePolicy.setVerticalStretch(0)
  35. self.spinBox.setSizePolicy(sizePolicy)
  36. self.spinBox.setMinimumSize(QtCore.QSize(42, 0))
  37. self.spinBox.setMaximumSize(QtCore.QSize(52, 22))
  38. font = QtGui.QFont()
  39. font.setPointSize(12)
  40. self.spinBox.setFont(font)
  41. self.spinBox.setFocusPolicy(QtCore.Qt.WheelFocus)
  42. self.spinBox.setFrame(True)
  43. self.gridLayout_2.addWidget(self.spinBox, 0, 0, 2, 1)
  44. self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
  45.  
  46. if __name__ == "__main__":
  47. import sys
  48. app = QtWidgets.QApplication(sys.argv)
  49. FormMatrixCell = QtWidgets.QWidget()
  50. ui = Ui_FormMatrixCell()
  51. ui.setupUi(FormMatrixCell)
  52. FormMatrixCell.show()
  53. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode