user14738178
user14738178

Reputation:

Disabling Multiple Buttons in QTableWidget

In my app, we can add items to the QTableWidget and subsequently update buttons and Delete buttons are added to the QTableWidget, Update and delete function represent network operation, So here I have simulated with QTimer which is linked to the progress Bar. So I want to disable all the buttons whenever I clicked the Delete or Update buttons except the button which is clicked and reenable all the buttons whenever the task is completed.

class LoginNew(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.show_database()
        self.pushButton.clicked.connect(self.addToTableWidget)
        
    

    def addToTableWidget(self):
       
        self.row_data = []
        self.val1 =  self.lineEdit.text()
        self.row_data.append(self.val1)
        self.val2 = self.lineEdit_2.text()
        self.row_data.append(self.val2)
        self.val3 =  self.lineEdit_3.text()
        self.row_data.append(self.val3)


        row = self.tableWidget.rowCount()
        self.tableWidget.setRowCount(row+1)
        col = 0
        for item in self.row_data:
            cell = QTableWidgetItem(str(item))
            self.tableWidget.setItem(row, col, cell)
            col += 1
        
        db_new.insert(self.val1,self.val2, self.val3)
       
        for index in range(self.tableWidget.rowCount()):
            self.btx =  QPushButton(self.tableWidget)
            self.btn =  QPushButton(self.tableWidget)
            self.btx.setText("Update")
            self.btn.setIcon(QIcon(QPixmap("delete.png")))
            self.btn.setIconSize(QSize(35,45))
            self.btx.clicked.connect(self.update_pos)
            self.btn.clicked.connect(self.delete_pos)
            self.tableWidget.setCellWidget(index,3, self.btx)
            self.tableWidget.setCellWidget(index,4,self.btn)
        
    
    def show_database(self):
        res = db_new.fetch_data()
        self.tableWidget.setRowCount(0)
        for row_number, row_data in enumerate(res):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget.setItem(row_number, column_number+1  ,  QTableWidgetItem(str(data)))
        

        for index in range(self.tableWidget.rowCount()):
            self.btx =  QPushButton(self.tableWidget)
            self.btn =  QPushButton(self.tableWidget)
            self.btx.setText("Update")
            self.btn.setIcon(QIcon(QPixmap("delete.png")))
            self.btn.setIconSize(QSize(35,35))
            self.btx.clicked.connect(self.update_pos)
            self.btn.clicked.connect(self.delete_pos)
            self.tableWidget.setCellWidget(index,4, self.btx)
            self.tableWidget.setCellWidget(index,0,self.btn)

    def update_pos(self):
        self.button =self.focusWidget()
        self.index = self.tableWidget.indexAt(self.button.pos())
        self.button.clicked.connect(self.btn_trigger)
    
    def btn_trigger(self):

        QMessageBox.information(self, "Update Data", f' Value  is {self.index.row()} {self.index.column()}')
        self.geti=10
        self.timer = QTimer()
        self.timer.timeout.connect(self.num)
        self.timer.start(2000)
        self.show_database()
       

    
    def cell_was_clicked(self, row,column, kale):
        print("Row %d and Column %d was clicked" % (row, column))
        item = self.tableWidget.itemAt(row, column)
        self.ID = item.text()
        QMessageBox.information(self, "INFORMATION",f'ID VALUE is  {item.text()}')
        
    
    def delete_pos(self):

        print("delete Button was clicked")
        self.geti=0
        self.timer = QTimer()
        self.timer.timeout.connect(self.num)

        self.timer.start(1000)
    


    def num(self):

        if self.geti <9999:
            print ( self.geti )
            self.geti += 1
            self.progressBar.setValue(self.geti)
        else:
            self.timer.stop()

def main():
    app =  QApplication(sys.argv)
    win = LoginNew()
    win.show()
    app.exec_()


if __name__ =='__main__':
    main()




I have tried using self.btx.setEnabled(False) But I am unable to decide which all buttons should be disabled??

Link for the UI image Actual Ui

Upvotes: 0

Views: 204

Answers (2)

Dr. C.
Dr. C.

Reputation: 106

If you just want to collect all of specific elements, like lineEdits, into an iterable collection, such as a list, here's a one-time function that lets you pass various instructions to that function to affect all of its fields.

I hope this routine is helpful to someone. ~Mike


        def all_fields(switch=""):
            field_list = [  self.lineEdit_1,
                            self.lineEdit_2,
                            self.lineEdit_3,
                            self.lineEdit_4,
                            self.lineEdit_5,
                            self.lineEdit_6,
                            self.lineEdit_7,
                            self.lineEdit_8,
                            self.lineEdit_9,
                            self.lineEdit_10,
                            self.textEdit  ]
            for field in field_list:
                if str(switch).lower() == "clear":
                    field.clear()
                elif str(switch).lower() == "disable":
                    field.setDisabled(True)
                elif str(switch).lower() == "enable":
                    field.setDisabled(False)

        all_fields("disable")

Upvotes: 1

ypnos
ypnos

Reputation: 52407

The task at hands is:

I want to disable all the buttons whenever I clicked the Delete or Update buttons except the button which is clicked

One solution could be:

  1. Keep a list of all buttons
  2. Write a method that disables all buttons except the specified one (in an argument). You can go through all buttons and compare to the specified one to determine disable or enable
  3. For each button, write a small lambda that calls the method in (2) with the button as argument. Connect this lambda to the button's pressed event

Alternatively, you could also use a QSignalMapper. You would then connect the mapped signal to the method in (2).

Upvotes: 0

Related Questions