Python Forum
PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary
#3
This should get you started:
#!/usr/bin/python3
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = QtWidgets.QWidget(self)
        self.setCentralWidget(self.ui)
        self.ui.tree = Tree()
        self.ui.button = QtWidgets.QPushButton("Validate")
        self.ui.button.clicked.connect(self.validate)
        self.ui.layout = QtWidgets.QVBoxLayout()
        self.ui.layout.addWidget(self.ui.tree)
        self.ui.layout.addWidget(self.ui.button)
        self.ui.setLayout(self.ui.layout)
        self.show()

    def validate(self):
        print("Checked:")
        for item in range(self.ui.tree.topLevelItemCount()):
            item = self.ui.tree.topLevelItem(item)
            if bool(item.checkState(0)):
                print(item.text(0), item.text(1))

class Tree(QtWidgets.QTreeWidget):
    def __init__(self):
        super().__init__()
        self.setHeaderLabels(["Name", "Path"])
        self.fill()

    def fill(self):
        apps = \
        {
            'Nexus Mod Manager': 'C:\\Games\\Nexus Mod Manager\\NexusClient.exe',
            'CCleaner': 'E:\\Program Files\\CCleaner\\CCleaner.exe'
        }
        for key, value in iter(apps.items()):
            item = QtWidgets.QTreeWidgetItem()
            item.setCheckState(0, QtCore.Qt.Unchecked)
            item.setText(0, key)
            item.setText(1, value)
            self.addTopLevelItem(item)


if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
Reply


Messages In This Thread
RE: PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary - by Alfalfa - Oct-02-2018, 02:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 Relative Layout Unkovic 1 675 Nov-10-2023, 01:52 PM
Last Post: Axel_Erfurt
  [PyGUI] How to reuse a window layout based on mysql rowcount? retroisbest 2 2,510 Nov-09-2021, 09:02 AM
Last Post: retroisbest
Question Python 3.10 and PyQt5(6) zazen000 2 7,766 Jun-14-2021, 05:02 PM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 2,278 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  pyqt5 layout Nickd12 8 3,659 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  [PyQt] Control a combobox from another python file PyQt5 prath 0 2,319 May-05-2020, 03:22 PM
Last Post: prath
  PyQt5: Iterate through rows to check for a value keegan_010 2 1,814 Mar-23-2020, 11:30 PM
Last Post: keegan_010
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,851 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 11,953 Dec-26-2019, 03:18 PM
Last Post: Denni
  Find active PyQT5 textline and add result to dictionary mart79 3 2,274 Jul-31-2019, 03:25 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020