Issue
Here is my code for changing the item height of items in list
ui.symbolsList.insertItem(ui.symbolsList.count(), ui.addSymbolText.text())
count = ui.symbolsList.count()
for i in range(count):
item = ui.symbolsList.item(i)
item.setSizeHint(QSize(item.sizeHint().width(), 60))
This is not changing the height of items in QListWidget. I also tried creating the item first with custom height and then adding that item to the list. What else do I need to do?
Following is the working code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize
class Ui_MainWindow(object):
def submit(self):
self.listWidget.insertItem(self.listWidget.count(), self.lineEdit.text())
count = self.listWidget.count()
for i in range(count):
item = self.listWidget.item(i)
item.setSizeHint(QSize(item.sizeHint().width(), 60))
self.lineEdit.setText("")
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 110, 256, 192))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(170, 70, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(300, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.submit)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Solution
The problem is that the default sizeHint() is QSize(-1, -1) which is invalid so when creating a new QSize with width -1 makes it invalid causing it not to be used when painting the item. The solution is not to use the width of the default sizeHint() but to set the width to 0:
item.setSizeHint(QSize(0, 60))
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.