私は一見単純に見える何かを理解するのに苦労しています:
QPushButtonからサブクラス化されたカスタムウィジェットがあり、その複数のインスタンスをQGridLayout()にレイアウトしています。paint()関数を追加し、ボタンのrect()を塗りつぶすために背景色を描画した瞬間、レイアウトの間隔は効果がなくなったようです。これが私が何を意味するかを示すためのスクリーンショットです:
これは、レイアウトの間隔に従うデフォルトのQPushButtonと、従わないカスタムの「ボタン」を示しています。CustomButtonに何かを(再)実装する必要があると確信していますが、それが何であるかを見つけることができません。contentMarginsを無駄に設定してみました。
私は何が欠けていますか?たぶん私はself.rect()を埋める必要はありませんが、何か他のものを埋める必要がありますか?上記のスクリーンショットを生成するサンプルコードは次のとおりです。
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class CustomButton(QPushButton):
def __init__(self, tool, icon=None, parent=None):
super(CustomButton, self).__init__(parent)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.setMinimumWidth(200)
self.frameGeometry()
def paintEvent(self, event):
painter = QPainter(self)
bgColor = QColor(60, 60, 60)
painter.fillRect(self.rect(), bgColor)
app = QApplication(sys.argv)
mainWindow = QWidget()
grid = QGridLayout()
grid.setSpacing(10)
mainWindow.setLayout(grid)
for i in xrange(4):
btn1 = CustomButton('A')
btn2 = QPushButton('B')
grid.addWidget(btn1, 0, i)
grid.addWidget(btn2, 1, i)
mainWindow.show()
sys.exit(app.exec_())