В PyQt4. Нет, ты не можешь этого сделать.
Почему? Я читаю исходный код QCheckBox
Qt4 (C++) here и here. Я видел, что он использует по умолчанию QStyleOptionButton
для отображения флажка, текста и значка. Использовать drawControl
для рисования всего элемента в QStyleOptionButton
по указанной конфигурации в QStyleOptionButton
. Также у него есть LayoutDirection
. И расположение макета в QStyleOptionButton
. Я не знаю в Qt4 C++ и наследует его и значок направления обмена. Но в PyQt4 это невозможно сделать.
Другой способ?: Да, у него есть другой способ решить, но не напрямую. Просто создайте свой собственный виджет точно так же, как QCheckBox
, и отключите значок в QCheckBox
и сделайте свой собственный QLabel
откройте свой значок и установите его с тем же QLayout
.
Пример;
import sys
from PyQt4 import QtGui, QtCore
class QCustomCheckBox (QtGui.QWidget):
stateChanged = QtCore.pyqtSignal(int)
def __init__ (self, text, parentQWidget = None):
super(QCustomCheckBox, self).__init__(parentQWidget)
self.customQCheckBox = QtGui.QCheckBox(text)
self.iconQLabel = QtGui.QLabel()
allQHBoxLayout = QtGui.QHBoxLayout()
allQHBoxLayout.addWidget(self.customQCheckBox)
allQHBoxLayout.addWidget(self.iconQLabel)
allQHBoxLayout.addStretch(1)
self.setLayout(allQHBoxLayout)
self.customQCheckBox.stateChanged.connect(self.stateChanged.emit)
def setPixmap (self, newQPixmap, width = 48, height = 48):
self.iconQLabel.setPixmap(newQPixmap.scaled(width, height, QtCore.Qt.KeepAspectRatio))
def pixmap (self):
return self.iconQLabel.pixmap()
class QCustomWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomWidget, self).__init__(parent)
allQVBoxLayout = QtGui.QVBoxLayout()
firstQCustomCheckBox = QCustomCheckBox('First Check Box')
firstQCustomCheckBox.setPixmap(QtGui.QPixmap('1.jpg'))
allQVBoxLayout.addWidget(firstQCustomCheckBox)
secondQCustomCheckBox = QCustomCheckBox('Second Check Box')
secondQCustomCheckBox.setPixmap(QtGui.QPixmap('2.jpg'))
allQVBoxLayout.addWidget(secondQCustomCheckBox)
self.setLayout(allQVBoxLayout)
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())