I want to refresh the scrollAreaWidget so that new buttons added appear on the list by the side. Buttons that I manually set in before running the code work fine, but new ones don't appear.
Here is the entire code, The area of interest should be the function SetList
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1500, 1000)
MainWindow.setWindowTitle("Test")
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEditName = QLineEdit(self.centralwidget)
self.lineEditName.setGeometry(QRect(20, 90, 860, 40))
self.lineEditName.setFont(QFont("Times new roman",15))
self.lineEditName.setText("")
self.lineEditName.setToolTip("Name")
self.lineEditName.setPlaceholderText("Name")
self.pushButtonAdd = QPushButton(self.centralwidget)
self.pushButtonAdd.setGeometry(QRect(20, 800, 120, 50))
self.pushButtonAdd.setFont(QFont("Times new roman",20))
self.pushButtonAdd.setText("Add")
self.pushButtonAdd.clicked.connect(self.AddContact)
MainWindow.setCentralWidget(self.centralwidget)
def AddContact(self):
if self.lineEditName.text() != "":
Names.append(self.lineEditName.text())
self.SetList()
print(f"Success - Added {self.lineEditName.text()}")
else:
print("Error")
def SetList(self):
self.scrollArea = QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QRect(1000, 90, 400, 680))
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
self.scrollAreaWidgetContents.setGeometry(QRect(0, 0, 400, 680))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
Names.sort()
for name in Names:
self.pushButtonList = QPushButton(self.scrollAreaWidgetContents)
self.pushButtonList.setText(name)
self.pushButtonList.setFont(QFont("Times new roman",14))
self.verticalLayout.addWidget(self.pushButtonList)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
if __name__ == "__main__":
import sys
Names = ["Thor", "Loki"]
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
ui.SetList()
MainWindow.show()
sys.exit(app.exec_())