Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Checking which checkbox checked on QTableWidget
Qt 6.11 is out! See what's new in the release blog

Checking which checkbox checked on QTableWidget

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nightmaster
    wrote on last edited by
    #1

    Hi everyone

    I am having an issue with checking which checkbox has been chosen on a QTableWidget.

    The purpose of the table is to display a list of available appointments, a checkbox (variable cb) is added to the last column of each row for a user to check. The table can also filter the appointments by date (appt_date_filter method). I had to add a new instance of checkbox for this (cb1) to get this working.

    There is a submit button which isn't working when the table is filtered. When the button is clicked it triggers a method called "submitted".

    The following error occrus : self.checkboxclicked = self.cb.sender()
    RuntimeError: wrapped C/C++ object of type QCheckBox has been deleted

    I guess when the appointments are filtered cb gets deleted as cb1 is being used?

    Also i notice that if self.cb.isChecked(): returns false even when a cb is checked.

    Full code below:

    self.table = QtWidgets.QTableWidget(self)
            self.table.setGeometry(QtCore.QRect(50, 150, 700, 505))
            self.submit_btn = QtWidgets.QPushButton(self)
            self.submit_btn.setGeometry(QtCore.QRect(350,675,75,45))
            self.submit_btn.setText("Submit")
            self.table.show()
            self.tab_row = 0
            #print(self.get_appts)
    
    
    
            self.qb = QButtonGroup(self.table)
            for self.data in self.get_appts:
                self.table.setRowCount(len(self.get_appts))
                self.table.setColumnCount(len(self.get_appts[0]))
                self.table.setItem(self.tab_row, 0, QTableWidgetItem(self.data[0]))
                self.table.setItem(self.tab_row, 1,QTableWidgetItem(self.data[1]))
                self.table.setItem(self.tab_row, 2, QTableWidgetItem(self.data[2]))
                self.table.setItem(self.tab_row, 3, QTableWidgetItem(self.data[3]))
                self.table.setHorizontalHeaderLabels(["Start Time", "End Time", "Location", "Doctor", "Select one"])
                self.table.setColumnWidth(0, 160)
                self.table.setColumnWidth(1,160)
                self.table.setColumnWidth(2,120)
                self.table.setColumnWidth(3, 140)
    
                self.cb = QCheckBox(self.table)
                #self.check_item = (QTableWidgetItem(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled))
                #self.check_item.setCheckState(QtCore.Qt.Unchecked)
    
                self.cb.show()
                self.qb.addButton(self.cb)
                self.table.setCellWidget(self.tab_row, 4, self.cb)
    
                if self.cb.isChecked():
                    print("yes it is checked")
                self.cb.clicked.connect(self.change)
                self.tab_row += 1
    
                self.date_picker.dateChanged.connect(self.appt_date_filter)
                self.table.cellClicked.connect(self.change)
                self.submit_btn.clicked.connect(self.submitted)
    
    
    
        def appt_date_filter(self):
            self.date_filter = self.date_picker.date()
            self.filter_date_py = self.date_filter.toPyDate()
            self.filter_date_string = self.filter_date_py.strftime("%Y-%m-%d")
            print(self.filter_date_string, "filter date")
    
            self.get_filt_apps = main.filter_appts(self.filter_date_string)
    
            self.tab_row1 = 0
    
            for filt_data in self.get_filt_apps:
                self.table.setRowCount(len(self.get_filt_apps))
                self.table.setColumnCount(len(self.get_filt_apps[0]))
                self.table.setItem(self.tab_row1, 0, QTableWidgetItem(filt_data[0]))
                self.table.setItem(self.tab_row1, 1,QTableWidgetItem(filt_data[1]))
                self.table.setItem(self.tab_row1, 2, QTableWidgetItem(filt_data[2]))
                self.table.setItem(self.tab_row1, 3, QTableWidgetItem(filt_data[3]))
    
                self.cb1 = QCheckBox(self.table)
    
                self.cb1.show()
                self.qb.addButton(self.cb1)
                self.table.setCellWidget(self.tab_row1, 4, self.cb1)
    
                self.tab_row1 += 1
    
                self.table.cellClicked.connect(self.change)
                self.submit_btn.clicked.connect(self.submitted)
    
        def change(self):
    
            if self.cb.isChecked():
    
                self.checkboxclicked = self.cb.sender()
                self.index = self.table.indexAt(self.checkboxclicked.pos())
                print(self.index.row(), "row of index in submitted")
    
    
            else:
    
                self.filtered_checkboxclicked = self.cb1.sender()
                self.filtered_index = self.table.indexAt(self.checkboxclicked.pos())
                # print(self.table.item(current_row,2).text(), ' this is the selection')
                # print(self.docid, "doc id in change")
    
    
        def submitted(self):
    
            if self.cb.isChecked():
                print("checkbox clicked")
    
                # print(self.checkboxclicked, "checkbox clicked")
                self.chosen_appt_start = self.table.item(self.index.row(), 0).text()
                self.chosen_appt_location = self.table.item(self.index.row(), 2).text()
                print(self.chosen_appt_start, " ", self.chosen_appt_location)
                self.start_and_doc = main.get_selected_appts_id(self.chosen_appt_start, self.chosen_appt_location)
    
    
            else:
    
                print("cb1 checked")
                self.filtered_appt_start = self.table.item(self.filtered_index.row(), 0).text()
                self.filtered_appt_location = self.table.item(self.filtered_index.row(), 2).text()
                self.start_and_doc = main.get_selected_appts_id(self.chosen_appt_start,self.chosen_appt_location)  # get appointment id from sqllite db
    
            for id  in self.start_and_doc:
             self.start_and_doc_selc = (id)
            print(self.start_and_doc_selc[0])
            main.update_appts(loginScreen.logged_in_patientid,self.start_and_doc_selc[0])
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Don't use widgets for that. They are for static content and do not have any link with the model below.

      Make that item checkable. It will show a checkbox and you'll be have the information in your model.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • N Offline
        N Offline
        Nightmaster
        wrote on last edited by
        #3

        Thanks @SGaist. I changed it to QTableView and ditched the checkboxes. Selecting rows works fine.

        1 Reply Last reply
        0
        • N Nightmaster has marked this topic as solved on
        • N Nightmaster has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved