我从here获得了此代码,并希望为我的项目修改它。不同之处在于我的项目需要图标在另一个QLabel上。使用Layouts无法实现这一点,所以我决定在QWidget上放置3个QLabel(一个用于图标或图片,另一个用于名称,最后一个用于消息),但我得到一个空窗口。这是我的代码。请不要介意使用的尺寸,因为我只想看到显示的小部件并编辑尺寸。
import sys
from PyQt4 import QtGui, QtCore
from PIL import Image, ImageOps
from PIL.ImageQt import ImageQt
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
# self.textQVBoxLayout = QtGui.QVBoxLayout()
self.widget = QtGui.QWidget()
self.widget.setMinimumSize(151, 231)
self.chatMessage = QtGui.QLabel(self.widget)
# self.chatMessage.setGeometry(QtCore.QRect(50, 80, 411, 161))
font = QtGui.QFont()
font.setFamily('Comic Sans MS')
font.setPointSize(11)
self.chatMessage.setFont(font)
self.chatMessage.setMinimumHeight(40)
self.chatMessage.setStyleSheet('''
color: rgb(255, 0, 0);
border: 1px solid #0089ff;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
border-bottom-left-radius: 20px;
padding-left: 60px;
padding-top: 3px
''')
self.userName = QtGui.QLabel(self.widget)
font = QtGui.QFont()
font.setFamily('Arial Narrow')
font.setBold(True)
font.setPointSize(11)
self.userName.setFont(font)
self.userName.setFixedHeight(30)
self.userName.setAlignment(QtCore.Qt.AlignTop)
self.userName.setStyleSheet('''
color: rgb(255, 255, 255);
background-color: #0089ff;
border: 1px solid #0089ff;
border-bottom: 0px;
border-top-right-radius: 20px;
border-top-left-radius: 20px;
padding-left: 60px;
''')
# self.textQVBoxLayout.addWidget(self.userName)
# self.textQVBoxLayout.addWidget(self.chatMessage)
# self.allQHBoxLayout = QtGui.QHBoxLayout()
self.iconQLabel = QtGui.QLabel(self.widget)
#self.allQHBoxLayout.setSpacing(0)
#self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
#self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
#self.setLayout(self.allQHBoxLayout)
# self.iconQLabel.setFixedSize(60, 60)
self.userName.setFixedWidth(190)
def chat_messages (self, text):
self.chatMessage.setText(text)
def setTextUp (self, text):
self.userName.setText(text)
def setIcon (self, imagePath):
mask = Image.open('Images\\Mask.png').convert('L')
image = Image.open('Images\\pro.jpg')
output = ImageOps.fit(image, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('pic.png')
im = Image.open('pic.png')
size =60, 60
im.thumbnail(size, Image.LANCZOS)
im = im.convert('RGBA')
image1 = ImageQt(im)
image2 = QtGui.QImage(image1)
pixmap = QtGui.QPixmap.fromImage(image2)
self.iconQLabel.setPixmap(QtGui.QPixmap(pixmap))
class exampleQMainWindow (QtGui.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
self.showMaximized()
self.myQListWidget = QtGui.QListWidget(self)
for icon, name, message in [
('pic.png', 'Name', 'Message'),
(pic.png', 'Name', 'Message')]:
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(name)
myQCustomQWidget.chat_messages(message)
myQCustomQWidget.setIcon(icon)
myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget)
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setCurrentRow(-1)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
self.myQListWidget.scrollToBottom()
self.myQListWidget.setAutoScrollMargin(15)
self.myQListWidget.setSelectionRectVisible(False)
self.myQListWidget.setUniformItemSizes(False)
self.setCentralWidget(self.myQListWidget)
self.setMinimumSize(660, 500)
app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
您的代码有2个错误:
小部件相对于父级定位,如果他们没有父级,则除非他们是topLevel,否则他们将不会被显示,例如exampleQMainWindow
,其他要显示的小部件必须具有家长。如果您的QCustomQWidget
所有标签的父级都为self.widget
,则self.widget
没有父级,因此不会显示,也不会显示子级。简单的解决方案是将他作为self.widget
的父级传递给self
,但是检查他的代码不是必须使用self.widget
所以解决方案是消除它并作为父级传递所有QLabel
到self
。
当不使用布局时,sizeHint()
会返回值QSize(-1, -1)
,因此它不会显示任何内容,您有责任在我的解决方案中设置合适的sizeHint()
将放置一个您应该更改的值。
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
# self.textQVBoxLayout = QtGui.QVBoxLayout()
self.setMinimumSize(151, 231)
self.chatMessage = QtGui.QLabel(self)
# self.chatMessage.setGeometry(QtCore.QRect(50, 80, 411, 161))
font = QtGui.QFont()
font.setFamily('Comic Sans MS')
font.setPointSize(11)
self.chatMessage.setFont(font)
self.chatMessage.setMinimumHeight(40)
self.chatMessage.setStyleSheet('''
color: rgb(255, 0, 0);
border: 1px solid #0089ff;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
border-bottom-left-radius: 20px;
padding-left: 60px;
padding-top: 3px
''')
self.userName = QtGui.QLabel(self)
font = QtGui.QFont()
font.setFamily('Arial Narrow')
font.setBold(True)
font.setPointSize(11)
self.userName.setFont(font)
self.userName.setFixedHeight(30)
self.userName.setAlignment(QtCore.Qt.AlignTop)
self.userName.setStyleSheet('''
color: rgb(255, 255, 255);
background-color: #0089ff;
border: 1px solid #0089ff;
border-bottom: 0px;
border-top-right-radius: 20px;
border-top-left-radius: 20px;
padding-left: 60px;
''')
# self.textQVBoxLayout.addWidget(self.userName)
# self.textQVBoxLayout.addWidget(self.chatMessage)
# self.allQHBoxLayout = QtGui.QHBoxLayout()
self.iconQLabel = QtGui.QLabel(self)
#self.allQHBoxLayout.setSpacing(0)
#self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
#self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
#self.setLayout(self.allQHBoxLayout)
# self.iconQLabel.setFixedSize(60, 60)
self.userName.setFixedWidth(190)
def chat_messages (self, text):
self.chatMessage.setText(text)
def setTextUp (self, text):
self.userName.setText(text)
def setIcon (self, imagePath):
mask = Image.open('Images\\Mask.png').convert('L')
image = Image.open('Images\\pro.jpg')
output = ImageOps.fit(image, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('pic.png')
im = Image.open('pic.png')
size =60, 60
im.thumbnail(size, Image.LANCZOS)
im = im.convert('RGBA')
image1 = ImageQt(im)
image2 = QtGui.QImage(image1)
pixmap = QtGui.QPixmap.fromImage(image2)
self.iconQLabel.setPixmap(QtGui.QPixmap(pixmap))
def sizeHint(self):
return QtCore.QSize(60, 60)