清除 PyQt 中的 TableView

发布于 2024-09-15 11:39:59 字数 1980 浏览 4 评论 0原文

我正在学习如何使用 PyQt 显示数据和表格。最终,我想要一个显示数据库内容的表格,但现在我只是掌握一些基础知识。我有一个使用 Qt Designer 和一组按钮(“创建”、“添加行”、“添加列”和“清除”)进行的基本设置(粘贴在下面)。 “创建”使用假装数据创建一个虚拟表。除了“清除”按钮之外,所有按钮都可以工作,当我单击它时,Python 完全崩溃,并且我没有任何错误消息来开始找出问题所在。

我的问题是:1)我做错了什么? 2)我可以通过异常处理的形式做些什么来防止这种情况发生,这样我就可以看到将来出了什么问题?

import sys
from sqlite3 import *
from PyQt4 import QtCore, QtGui, QtSql
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from test_class import Ui_MainWindow

class StartQT4(QtGui.QMainWindow, QTableWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.createbutton,QtCore.SIGNAL("clicked()"),self.file_dialog)
QtCore.QObject.connect(self.ui.addrowbutton,QtCore.SIGNAL("clicked()"),self.add_row)
QtCore.QObject.connect(self.ui.addcollumnbutton,QtCore.SIGNAL("clicked()"),self.add_column)
QtCore.QObject.connect(self.ui.clearbutton,QtCore.SIGNAL("clicked()"),self.clear_table)

def add_row(self):
    self.ui.tableWidget.insertRow (0)       

def add_column(self):
    self.ui.tableWidget.insertColumn (0)

def clear_table(self):
    #This bit that won't seem to work in any combination!
    #self.ui.tableWidget.clearContents()
    self.ui.tableWidget.clear()
    #self.ui.tableWidget.setColumnCount(0)
    #self.ui.tableWidget.setRowCount(0)

def file_dialog(self):
    self.ui.textEdit.setText("Testing testing")
    self.ui.tableWidget.setColumnCount(3)
    self.ui.tableWidget.setRowCount(3)
    a = QTableWidgetItem("A")
    self.ui.tableWidget.setHorizontalHeaderItem (0, a)
    a = QTableWidgetItem("B")
    self.ui.tableWidget.setHorizontalHeaderItem (1, a)
    self.ui.tableWidget.setHorizontalHeaderItem (2, a)
    b = QTableWidgetItem("Test")
    self.ui.tableWidget.setItem(1,1,b)       

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

I'm in the process of learning how to display data and tables using PyQt. Ultimately I'd like to have a Table showing the contents of a database, but for now I'm just getting to grips with some of the fundamentals. I have a basic setup (pasted below) made using Qt Designer with a set of buttons ("Create", "Add Row", "Add Column", and "Clear"). "Create" makes a dummy table with pretend data. All of the buttons work apart from the "Clear" button, and when I click it, Python crashes entirely, and I'm left with no error messages to start working out what is wrong.

My questions are: 1) What am I doing wrong? 2) What can I do in the form of exception handling to prevent this, so I can see what's gone wrong in the future ?

import sys
from sqlite3 import *
from PyQt4 import QtCore, QtGui, QtSql
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from test_class import Ui_MainWindow

class StartQT4(QtGui.QMainWindow, QTableWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.createbutton,QtCore.SIGNAL("clicked()"),self.file_dialog)
QtCore.QObject.connect(self.ui.addrowbutton,QtCore.SIGNAL("clicked()"),self.add_row)
QtCore.QObject.connect(self.ui.addcollumnbutton,QtCore.SIGNAL("clicked()"),self.add_column)
QtCore.QObject.connect(self.ui.clearbutton,QtCore.SIGNAL("clicked()"),self.clear_table)

def add_row(self):
    self.ui.tableWidget.insertRow (0)       

def add_column(self):
    self.ui.tableWidget.insertColumn (0)

def clear_table(self):
    #This bit that won't seem to work in any combination!
    #self.ui.tableWidget.clearContents()
    self.ui.tableWidget.clear()
    #self.ui.tableWidget.setColumnCount(0)
    #self.ui.tableWidget.setRowCount(0)

def file_dialog(self):
    self.ui.textEdit.setText("Testing testing")
    self.ui.tableWidget.setColumnCount(3)
    self.ui.tableWidget.setRowCount(3)
    a = QTableWidgetItem("A")
    self.ui.tableWidget.setHorizontalHeaderItem (0, a)
    a = QTableWidgetItem("B")
    self.ui.tableWidget.setHorizontalHeaderItem (1, a)
    self.ui.tableWidget.setHorizontalHeaderItem (2, a)
    b = QTableWidgetItem("Test")
    self.ui.tableWidget.setItem(1,1,b)       

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-09-22 11:40:01

它“彻底崩溃”?没有堆栈跟踪和一切?很难相信......

无论如何,PyQt4 文档既不知道 QTableView.clear 也不知道 QTableView.clearContents,但它知道 QTableView.clearSpans 应该做你想做的事。

It "crashes entirely"? Without stack trace and everything? Hard to believe...

Anyway, the PyQt4 documentation knows neither QTableView.clear nor QTableView.clearContents, but it knows QTableView.clearSpans which is supposed to do what you want.

七禾 2024-09-22 11:40:01

也许晚了,但是:

1)我做错了什么?

  • 任何错误都可能导致崩溃,所以我无法回答。

2)我可以通过异常处理的形式做些什么来防止这种情况发生,这样我就可以看到将来出了什么问题?

  • 在调试模式下运行 python。在 Windows 下,发生任何错误时,干净的崩溃是 PyQT 的行为。

这个问题应该让你继续使用 QTableView 对象。

Maybe late but:

1) What am I doing wrong?

  • Any error can cause the crash, so I cannot answer.

2) What can I do in the form of exception handling to prevent this, so I can see what's gone wrong in the future ?

  • Run python in debug mode. Under windows, the clean crash is the PyQT behaviour when any error happens.

This question should get you going with QTableView objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文