pythondatabasepyqt5qtablewidgetqpushbutton

Python PyQt5: Can I also insert a QPushButton into a database via SQL?


As the title suggests I want to know if it's possible to insert a QPushButton into a database via SQL?

    connection = pymysql.connect(host = 'localhost',
        user = 'root',
        db = 'myDatabase',
        cursorclass = pymysql.cursors.DictCursor)  
    cur = connection.cursor()

    cur.execute("INSERT INTO myTable VALUES("%s"), [QPushButton("Click me!")])  

Solution

  • If you're using a QTableWidget to display the database, you need to add the button to the cell in the table:

        button = QPushButton('Show Image', self)
        button.clicked.connect(self.handleImageButton)
        self.tableWidget.setCellWidget(row, column, button)
        ...
    
    def handleImageButton(self):
        button = self.sender()
        item = self.tableWidget.itemAt(button.pos())
        if item is not None:
            print(item.row(), item.column())
            # get image data, etc ...