2

I am building a small application using pyQt5. Till now I've built a UI using QtCreator, converted that to a Python file using pyuic and now on the push of a button, it should dynamically populate the tabWidget's tabs with checkboxes having labels pulled from a string list. I can't seem to figure out the best way to go about this problem.

Till now I've done the following.

  1. Link the UI file with my main python code.
  2. Handle a button click to call a function to insert checkboxes.
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.auth_select.clicked.connect(self.handle_auth)
        self.show()
    def populate_tabs(self):
        self.tabWidget.addWidget(QtWidgets.QCheckBox())

I cannot seem to figure out how to dynamically insert the checkboxes into a certain tab. Let's say my tab name is main_tab; I want to insert 5 checkboxes whose labels and ids come from 2 lists labels[] and ids[].

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Raghav Kukreti
  • 552
  • 5
  • 18
  • okay, we already know what you want. What have you tried to solve your problem? – eyllanesc Jul 01 '19 at 10:44
  • Alright. So I've tried selecting the tabWidget and adding a checkbox like `self.tabWidget.addWidget(QtWidgets.QCheckBox())`. But this just throws `AttributeError: 'QTabWidget' object has no attribute 'addWidget'`. Should I be selected a specific tab? If so, how should I go about that? Apologies for the ambiguity. – Raghav Kukreti Jul 01 '19 at 10:46
  • I agree with your logic. How do I insert a widget into the tab once I access it? – Raghav Kukreti Jul 01 '19 at 10:59
  • Alright, if the name of the tab is `tab_main`, should I access the tab like; `self.tab_main.function()`? – Raghav Kukreti Jul 01 '19 at 11:08
  • 1) I have pointed out the way and I think it is a good start 2) I can not point to any resource because I do not use them. 3) I have been patient, I do not think that is pedantic. 4) I can comment, and you will see if you answer me, everyone has their responsibility. – eyllanesc Jul 01 '19 at 11:21
  • I'll be more clear, I've seen thousands of examples on the internet, but I can not recommend any since I have not used them, I've learned Qt first and then PyQt. For my learning I have only used the C++ examples. But if you do the following search: https://www.google.com/search?q=QTabWidget+examples+PyQt , you will surely find examples of what you want. – eyllanesc Jul 01 '19 at 11:27

1 Answers1

0

Alright, for anyone who stumbles upon this. I went about this as described below.

  1. Create a layout to house inner elements
  2. Dynamically insert QWidgets into this layout
  3. Finally set the layout of the tab/widget you need to, to the above.

A small example is [extended from above]

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.auth_select.clicked.connect(self.handle_auth)
        self.show()
        self.populate_tabs(connection_string)
    def populate_tabs(self, connection_string):
        layout = QGridLayout()
        for i in range(1, 5):
            for j in range(1, 10):  
                layout.addWidget(QCheckBox("Check #{}".format(i)) , i, j)

        self.tabWidget.widget(2).setLayout(layout)

Raghav Kukreti
  • 552
  • 5
  • 18