【问题标题】:Displaying image from tableWidget into PyQt5 form [duplicate]将tableWidget中的图像显示为PyQt5表单[重复]
【发布时间】:2021-01-11 10:41:37
【问题描述】:

图像已作为图标显示在表格小部件列中。我现在正在尝试展示一个搜索功能,该功能将接受行编辑,以便可以通过某些标签显示查询的行数据。如果我的查询格式容易受到 SQL 注入的影响,请根据最佳实践帮助审查和修改此代码并协助更正,以便我能够在表单上显示从数据库中获取的图像。该代码仅用于显示文本数据,但我没有获得重新显示从 tablewidget 获取的图像的逻辑。

 def searchPers(self):
    con = MySQLdb.connect(host="localhost", user="root", password="", database="db")
    with con:
        cur = con.cursor()

        name = self.ui.name_edit.text()

        cmd = "SELECT * FROM persons WHERE name LIKE'"+name+"'"
        cur.execute(cmd)
        row = cur.fetchone()

        if row == None:
            self.ui.response_label.setText("Person not found")
        else:
            self.ui.name_label.setText(str(row[0]))
            pixmap = QPixmap()
            pixmap.loadFromData(QByteArray.toBase64(row[1]))
            self.ui.photo_label.setPixmap(QPixmap(pixmap))

【问题讨论】:

    标签: mysql pyqt5 qtablewidget qpixmap qbytearray


    【解决方案1】:

    经过多次反复试验,我后来得到了它:

    def searchPers(self):
      con = MySQLdb.connect(host="localhost", user="root", password="", database="db")
      with con:
        cur = con.cursor()
    
        name = self.ui.name_edit.text()
    
        cmd = "SELECT * FROM persons WHERE name LIKE'"+name+"'"
        cur.execute(cmd)
        row = cur.fetchone()
    
        if row == None:
            self.ui.response_label.setText("Person not found")
        else:
            self.ui.name_label.setText(str(row[0]))
            pixmap = QPixmap()
            pixmap.loadFromData(QByteArray.fromBase64(row[1]))
            self.ui.photo_label.setPixmap(QPixmap(pixmap))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多