wNakano
wNakano

Reputation: 39

QCheckbox to add and remove items from a list

I'm building a user interface with PyQt5 (and python 3.7) which I'm using a check box to add elements in a general list (input_columns_general) when it's checked and remove/delete then when unchecked. Every time when I check a check box the added elements must follow a default position order in the specific list.

With this task I'm having two problems:

  1. For each one of the check boxes that I have I need to build up a function which call the separately the "isChecked()" class and the correspond list that I want to add to "input_columns_general". In the code below I put all the possible situations;

  2. When I check a check box, the main idea: the correlated list will receive the correspondent list (check box "microreactor_checkbox" will add to "input_columns_general" the elements in "input_columns_microreactor") however it need to be always at the correct postion of the general. As example: should "input_columns_microreactor" be always the first elements and "input_columns_mixer" be after "input_columns_micrreactor" without considering which one was checked first. Each one of the desired positions are described ain the code.

In the code showed below, regard a try that consider how can I add and remove the elements of the general list (but it doesn't work properly due it's not deleting all the desired elements in the list), and I didn't figure a way how can I always place in the correct position new list elements.

Do someone has a hint about this case?


from PyQt5 import QtCore, QtGui, QtWidgets

input_columns_general = []
input_columns_microreactor = ['a1', 'b1', 'c1', 'd1', 'e1']
input_columns_mixer = ['a2']
input_columns_mixture_process = ['a3', 'b3', 'c3', 'd3','e3', 'f3', 'g3', 'h3']
input_columns_reaction_process = ['a4', 'b4', 'c4', 'd4', 'e4']
input_columns_kinect_constants = ['a5', 'b5', 'c5', 'd5', 'e5', 'f5']
input_columns_chemical_species = ['a6', 'b6', 'c6', 'd6', 'e6',
                                  'f6', 'g6', 'h6', 'i6', 'j6', 'k6', 'l6']

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1386, 839)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(0, 0, 1361, 781))
        self.tabWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.tabWidget.setObjectName("tabWidget")

        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")

        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()

        self.tab_2.setObjectName("tab_2")
        self.label = QtWidgets.QLabel(self.tab_2)
        self.label.setGeometry(QtCore.QRect(30, 90, 111, 21))

        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)

        self.label.setFont(font)
        self.label.setTextFormat(QtCore.Qt.PlainText)
        self.label.setObjectName("label")

        self.microreactor_checkbox = QtWidgets.QCheckBox(self.tab_2)
        self.microreactor_checkbox.setGeometry(QtCore.QRect(30, 130, 101, 21))
        self.microreactor_checkbox.setObjectName("microreactor_checkbox")
        self.microreactor_checkbox.stateChanged.connect(lambda: self.state_changed_microreactor(self.microreactor_checkbox))

        self.mixer_checkbox = QtWidgets.QCheckBox(self.tab_2)
        self.mixer_checkbox.setGeometry(QtCore.QRect(160, 130, 71, 21))
        self.mixer_checkbox.setObjectName("mixer_checkbox")
        self.mixer_checkbox.stateChanged.connect(lambda: self.state_changed_mixer(self.mixer_checkbox))

        self.mixture_process_checkbox = QtWidgets.QCheckBox(self.tab_2)
        self.mixture_process_checkbox.setGeometry(QtCore.QRect(270, 130, 121, 21))
        self.mixture_process_checkbox.setObjectName("mixture_process_checkbox")
        self.mixture_process_checkbox.stateChanged.connect(lambda: self.state_changed_mixture_process(self.mixture_process_checkbox))

        self.reaction_process_checkbox = QtWidgets.QCheckBox(self.tab_2)
        self.reaction_process_checkbox.setGeometry(QtCore.QRect(420, 130, 131, 21))
        self.reaction_process_checkbox.setObjectName("reaction_process_checkbox")
        self.reaction_process_checkbox.stateChanged.connect(lambda: self.state_changed_reaction_process(self.reaction_process_checkbox))
        self.input_table =  QtWidgets.QTableWidget(self.tab_2)
        self.input_table.setGeometry(QtCore.QRect(20, 370, 1261, 371))
        self.input_table.setObjectName("input_table")

        self.input_table.setColumnCount(10)
        self.input_table.setRowCount(10)

        # self.createTableElements(input_table)
        # item = QtWidgets.QTableWidgetItem()

        self.tabWidget.addTab(self.tab_2, "")
        self.tab_3 = QtWidgets.QWidget()
        self.tab_3.setObjectName("tab_3")
        self.tabWidget.addTab(self.tab_3, "")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1386, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def state_changed_microreactor(self, int):
        if self.microreactor_checkbox.isChecked():
            input_columns_general[0:4] = input_columns_microreactor
            print(input_columns_general)
        else:
            for element in input_columns_microreactor:
                input_columns_general.remove(element)
                print(input_columns_general)

    def state_changed_mixer(self, int):
        if self.mixer_checkbox.isChecked():
            input_columns_general[4:5] = input_columns_mixer
            print(input_columns_general)
        else:
            for element in input_columns_mixer:
                input_columns_general.remove(element)
            print(input_columns_general)

    def state_changed_mixture_process(self, int):
        if self.mixture_process_checkbox.isChecked():
            input_columns_general[5:12] = input_columns_mixture_process
            print(input_columns_general)
        else:
            for element in input_columns_mixture_process:
                input_columns_general.remove(element)
            print(input_columns_general)

    def state_changed_reaction_process(self, int):
        if self.reaction_process_checkbox.isChecked():
            input_columns_general[12:23] = input_columns_reaction_process
            print(input_columns_general)
        else:
            for element in input_columns_reaction_process:
                input_columns_general.remove(element)
            print(input_columns_general)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Main"))
        self.label.setText(_translate("MainWindow", "Type of Data"))
        self.microreactor_checkbox.setText(_translate("MainWindow", "Microreactor"))
        self.mixer_checkbox.setText(_translate("MainWindow", "Mixer"))
        self.mixture_process_checkbox.setText(_translate("MainWindow", "Mixture Process"))
        self.reaction_process_checkbox.setText(_translate("MainWindow", "Reaction Process"))

        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Insert Data"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Load Data"))

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_())

Upvotes: 1

Views: 952

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

First of all it is recommended not to modify the class generated by Qt Designer so you must regenerate the file and assume that it is called mainwindow.py: pyuic5 your_file.ui -o mainwindow.py -x.

Going to the problem, as each time a QCheckBox is changed, the result will be regenerated, so the list can be created by iterating and verifying the status of the QCheckBox, adding it or not depending on each case.

main.py

from PyQt5 import QtCore, QtGui, QtWidgets

from mainwindow import Ui_MainWindow


input_columns_microreactor = ["a1", "b1", "c1", "d1", "e1"]
input_columns_mixer = ["a2"]
input_columns_mixture_process = ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"]
input_columns_reaction_process = ["a4", "b4", "c4", "d4", "e4"]


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        for checkbox in (
            self.microreactor_checkbox,
            self.mixer_checkbox,
            self.mixture_process_checkbox,
            self.reaction_process_checkbox,
        ):
            checkbox.stateChanged.connect(self.update_input_columns_general)

    @QtCore.pyqtSlot()
    def update_input_columns_general(self):
        input_columns_general = []

        checkboxes = (
            self.microreactor_checkbox,
            self.mixer_checkbox,
            self.mixture_process_checkbox,
            self.reaction_process_checkbox,
        )

        input_columns = (
            input_columns_microreactor,
            input_columns_microreactor,
            input_columns_mixture_process,
            input_columns_reaction_process,
        )

        for checkbox, columns in zip(checkboxes, input_columns):
            if checkbox.isChecked():
                input_columns_general.extend(columns)
        print(input_columns_general)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions