【问题标题】:TypeError: QTableView(parent: QWidget = None): argument 1 has unexpected type 'int'TypeError: QTableView(parent: QWidget = None): 参数 1 具有意外类型“int”
【发布时间】:2021-04-30 06:15:03
【问题描述】:

我想在我的 QTableView 中显示数据库中所有可能的数据,但是错误

TypeError: QTableView(parent: QWidget = None): argument 1 has unexpected type 'int'

总是出现在最后。我不明白,因为我数据库中的所有数据类型都是 varchars 而不是 int。

这是我调用数据的函数:

def connectDB(self):

        # TABLE
        columns = ['Time In','Time Out', 'Class', 'Color', 'Specific Attribute', 'Camera Number']
        tableWidget = QTableView(22, 6, self)
        tableWidget.setHorizontalHeaderLabels(columns)
        tableWidget.setGeometry( 400, 50, 850, 692)
        tableWidget.resizeRowsToContents()


        # CONNECTION
        conn = mariadb.connect(
        user="root",
        password="",
        host="localhost",
        port=3306,
        database="IRIS")

        cur = conn.cursor()
        cur.execute("SELECT * FROM iris_table")

        allSQLRows= cur.fetchall()
        tableWidget.setRowCount(len(allSQLRows))

        data = [allSQLRows]
        tableWidget.setModel(data)

【问题讨论】:

    标签: python pyqt5


    【解决方案1】:

    您有以下错误。

    • QTableView 不接受构造函数中的行数或列数,因此您似乎复制了接受它的 QTableWidget 的代码。

    • Qt 中的模型不是数组、列表等,而是继承自 QAbstractItemModel 的类。

    • 如果你使用 QTableWidget 那么你不能使用一个模型,因为它已经有一个默认不能修改的模型。

    考虑到上述情况,有两种可能的解决方案:

    • 将 QTableView 与模型一起使用。

      labels = [
          "Time In",
          "Time Out",
          "Class",
          "Color",
          "Specific Attribute",
          "Camera Number",
      ]
      view = QTableView(self)
      model = QStandardItemModel(0, 6)
      view.setModel(model)
      model.setHorizontalHeaderLabels(labels)
      view.setGeometry(400, 50, 850, 692)
      view.resizeRowsToContents()
      
      conn = mariadb.connect(
          user="root", password="", host="localhost", port=3306, database="IRIS"
      )
      
      cur = conn.cursor()
      cur.execute("SELECT * FROM iris_table")
      
      allSQLRows = cur.fetchall()
      model.setRowCount(len(allSQLRows))
      
      for i, row_data in enumerate(allSQLRows):
          for j, data in enumerate(row_data):
              item = QStandardItem()
              item.setData(data, Qt.DisplayRole)
              model.setItem(i, j, item)
      
    • 使用 QTableWidget

      labels = [
          "Time In",
          "Time Out",
          "Class",
          "Color",
          "Specific Attribute",
          "Camera Number",
      ]
      view = QTableWidget(0, 6, self)
      view.setHorizontalHeaderLabels(labels)
      view.setGeometry(400, 50, 850, 692)
      view.resizeRowsToContents()
      
      conn = mariadb.connect(
          user="root", password="", host="localhost", port=3306, database="IRIS"
      )
      
      cur = conn.cursor()
      cur.execute("SELECT * FROM iris_table")
      
      allSQLRows = cur.fetchall()
      view.setRowCount(len(allSQLRows))
      
      for i, row_data in enumerate(allSQLRows):
          for j, data in enumerate(row_data):
              item = QTableWidgetItem()
              item.setData(Qt.DisplayRole, data)
              view.setItem(i, j, item)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2012-12-12
      • 2021-03-18
      • 2021-05-23
      相关资源
      最近更新 更多