这是我正在尝试设计的单个项目的弹性布局:
请在下面找到我用来在 QlistWidget 中显示该自定义 QListWidgetItem 的代码。但布局看起来不像预期的那样:
- 自定义小部件不适合列表的宽度,我不想要水平滚动条
- 即使将 setWordWrap 设置为 True,长描述也不会显示整个文本并在其高度上被剪切
编码 :
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(400, 500)
self.centralwidget = QWidget(MainWindow)
self.gridLayout = QGridLayout(self.centralwidget)
self.listWidget = QListWidget(self.centralwidget)
self.gridLayout.addWidget(self.listWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
class customWidget(QWidget):
def __init__ (self, name, date, desc, parent = None):
super(customWidget, self).__init__(parent)
# feed labels
self.labelName = QLabel(name)
self.labelName.setFrameShape(QFrame.Box)
self.labelName.setStyleSheet('''font: bold;''')
self.labelDate = QLabel(date)
self.labelDate.setFrameShape(QFrame.Box)
self.labelDesc = QLabel(desc)
self.labelDesc.setFrameShape(QFrame.Box)
self.labelDesc.setWordWrap(True)
# layout
self.Hlayout = QHBoxLayout()
self.Hlayout.addWidget(self.labelName)
self.Hlayout.addStretch()
self.Hlayout.addWidget(self.labelDate)
self.Vlayout = QVBoxLayout()
self.Vlayout.addLayout(self.Hlayout)
self.Vlayout.addWidget(self.labelDesc)
self.setLayout(self.Vlayout)
def feed_list():
myArray = [
("paul meneck", "21/09/1969", "blond whith red hat"),
("boby la pointe", "16/08/1972", "brown shoes"),
("julius con-meraz de la fuego", "07/12/1980","this is a very long description"
"with more and more bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
"bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
"bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
"and finaly the END OF THAT LONG DESCRIPTION"),
("roberto umportes", "14/01/1957", "another description for that roberto")
]
for var in myArray:
item = QListWidgetItem()
widget = customWidget(var[0], var[1], var[2])
item.setSizeHint(widget.sizeHint())
ui.listWidget.addItem(item)
ui.listWidget.setItemWidget(item, widget)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
feed_list()
sys.exit(app.exec_())