나는 여러개의 확인란을 가지고있는 그리드 레이 아웃을 가지고있다. 확인란뿐만 아니라 일부 텍스트에 이미지를 추가하려고했습니다. 내가 겪고있는 문제는 확인란의 레이아웃이 왼쪽에서 오른쪽 (확인란, 아이콘, 텍스트)입니다.아이콘 위에 QCheckBox 텍스트를 삽입 할 수 있습니까?
아이콘 위에 텍스트를 삽입하는 방법이 있습니까? 스타일 시트를 사용하면 효과가 있을지, 그렇지 않으면 어떻게 보이는지 알 수 없습니다.
감사합니다.
나는 여러개의 확인란을 가지고있는 그리드 레이 아웃을 가지고있다. 확인란뿐만 아니라 일부 텍스트에 이미지를 추가하려고했습니다. 내가 겪고있는 문제는 확인란의 레이아웃이 왼쪽에서 오른쪽 (확인란, 아이콘, 텍스트)입니다.아이콘 위에 QCheckBox 텍스트를 삽입 할 수 있습니까?
아이콘 위에 텍스트를 삽입하는 방법이 있습니까? 스타일 시트를 사용하면 효과가 있을지, 그렇지 않으면 어떻게 보이는지 알 수 없습니다.
감사합니다.
답변 : PyQt4. 아니, 네가 할 수 없어.
왜? 나는 QCheckBox
Qt4 (C++) here과 here의 소스 코드를 읽었다. 체크 박스, 텍스트 및 아이콘을 표시하려면 기본값 QStyleOptionButton
을 사용하는 것을 보았습니다. QStyleOptionButton
에 지정된 구성으로 QStyleOptionButton
에 모든 요소를 그릴 때는 drawControl
을 사용합니다. 또한 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_())