This is what i tried to do:
import PyQt5
import sys
from PyQt5 import QtWidgets
from testing import Ui_MainWindow
class App(QtWidgets.QMainWindow):
def __init__(self):
super(App,self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.ui.listWidget.addItem(f'{" ":<50}{"1":<20}{"25":<10}')
self.ui.listWidget.addItem(f'{" ":<50}{"1":<20}{"30":<10}')
self.ui.listWidget.addItem(f'{" ":<50}{"1":<20}{"150":<10}')
self.ui.listWidget.addItem(f'{" ":<50}{"1":<20}{"70":<10}')
self.ui.listWidget.addItem(f'{"Burger":<50}{"1":<20}{"25":<10}')
self.ui.listWidget.addItem(f'{"Waffle":<50}{"1":<20}{"30":<10}')
self.ui.listWidget.addItem(f'{"Coffee":<50}{"1":<20}{"150":<10}')
self.ui.listWidget.addItem(f'{"Coke":<50}{"1":<20}{"70":<10}')
print(f'{"Burger":<50}{"1":<20}{"25":<10}')
print(f'{"Waffle":<50}{"1":<20}{"25":<10}')
print(f'{"Coffee":<50}{"1":<20}{"25":<10}')
print(f'{"Coke":<50}{"1":<20}{"25":<10}')
app=QtWidgets.QApplication(sys.argv)
win=App()
win.show()
app.exec()
This is my interface file (testing.py) that has been created by Qt Designer:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(592, 325)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(120, 20, 47, 13))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(490, 160, 77, 101))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.verticalLayout.addWidget(self.pushButton_4)
self.pushButton_13 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_13.setObjectName("pushButton_13")
self.verticalLayout.addWidget(self.pushButton_13)
self.pushButton_14 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_14.setObjectName("pushButton_14")
self.verticalLayout.addWidget(self.pushButton_14)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(490, 10, 77, 83))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.pushButton = QtWidgets.QPushButton(self.layoutWidget1)
self.pushButton.setObjectName("pushButton")
self.verticalLayout_2.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget1)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout_2.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.layoutWidget1)
self.pushButton_3.setObjectName("pushButton_3")
self.verticalLayout_2.addWidget(self.pushButton_3)
self.label_12 = QtWidgets.QLabel(self.centralwidget)
self.label_12.setGeometry(QtCore.QRect(310, -1, 121, 31))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(False)
font.setWeight(50)
self.label_12.setFont(font)
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.layoutWidget2 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget2.setGeometry(QtCore.QRect(270, 40, 91, 121))
self.layoutWidget2.setObjectName("layoutWidget2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.pushButton_9 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_9.setText("")
self.pushButton_9.setObjectName("pushButton_9")
self.verticalLayout_3.addWidget(self.pushButton_9)
self.pushButton_10 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_10.setText("")
self.pushButton_10.setObjectName("pushButton_10")
self.verticalLayout_3.addWidget(self.pushButton_10)
self.pushButton_11 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_11.setText("")
self.pushButton_11.setObjectName("pushButton_11")
self.verticalLayout_3.addWidget(self.pushButton_11)
self.pushButton_12 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_12.setText("")
self.pushButton_12.setObjectName("pushButton_12")
self.verticalLayout_3.addWidget(self.pushButton_12)
self.layoutWidget3 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget3.setGeometry(QtCore.QRect(366, 40, 77, 121))
self.layoutWidget3.setObjectName("layoutWidget3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.layoutWidget3)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget3)
self.pushButton_5.setObjectName("pushButton_5")
self.verticalLayout_4.addWidget(self.pushButton_5)
self.pushButton_6 = QtWidgets.QPushButton(self.layoutWidget3)
self.pushButton_6.setObjectName("pushButton_6")
self.verticalLayout_4.addWidget(self.pushButton_6)
self.pushButton_7 = QtWidgets.QPushButton(self.layoutWidget3)
self.pushButton_7.setObjectName("pushButton_7")
self.verticalLayout_4.addWidget(self.pushButton_7)
self.pushButton_8 = QtWidgets.QPushButton(self.layoutWidget3)
self.pushButton_8.setObjectName("pushButton_8")
self.verticalLayout_4.addWidget(self.pushButton_8)
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(10, 40, 251, 181))
self.listWidget.setObjectName("listWidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(170, 230, 41, 16))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(220, 230, 51, 16))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setEnabled(True)
self.label_3.setGeometry(QtCore.QRect(20, 20, 51, 20))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setEnabled(True)
self.label_4.setGeometry(QtCore.QRect(140, 20, 51, 16))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setEnabled(True)
self.label_6.setGeometry(QtCore.QRect(210, 20, 41, 16))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 592, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Finish Order"))
self.pushButton_13.setText(_translate("MainWindow", "Return"))
self.pushButton_14.setText(_translate("MainWindow", "Checkout"))
self.pushButton.setText(_translate("MainWindow", "Burger"))
self.pushButton_2.setText(_translate("MainWindow", "Drink"))
self.pushButton_3.setText(_translate("MainWindow", "Waffle"))
self.pushButton_5.setText(_translate("MainWindow", "1"))
self.pushButton_6.setText(_translate("MainWindow", "2"))
self.pushButton_7.setText(_translate("MainWindow", "3"))
self.pushButton_8.setText(_translate("MainWindow", "4"))
self.label.setText(_translate("MainWindow", "Total ="))
self.label_2.setText(_translate("MainWindow", "0"))
self.label_3.setText(_translate("MainWindow", "Name"))
self.label_4.setText(_translate("MainWindow", "Amount"))
self.label_6.setText(_translate("MainWindow", "Price"))
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_())
i put print functions to check whether my approaching is mistaken this is my output when i print those strings with reserved widths:
a screenshot my ui and how the strings look like
As you notice string fits to the row as i want when i leave the name column empty as shown below:
self.ui.listWidget.addItem(f'{" ":<50}{"1":<20}{"25":<10}')
But when i try to replace blank with the actual string as shown below:
self.ui.listWidget.addItem(f'{"Burger":<50}{"1":<20}{"25":<10}')
it doesn't look exactly like i want.
All the helps are appreciated. Let me know if it is a duplicate.
CodePudding user response:
Based on your screenshot, it seems that you are trying to build a table using a list. Whilst it is certainly possible, it would feel more natural to use a table view or widget instead.
You could replace your QListWidget
with QTableWidget. Then, you could insert new rows using insertRow. Or just initialise the table widget like this:
self.tableWidget = QTableWidget(10, 5, this);
And you could set the items like this:
self.tableWidget.setItem(row, column, QTableWidgetItem("foo"))