PyQt4 的 xml 数据库浏览器问题(treeview、singals 和 insertplaintext)

发布于 2024-11-03 01:44:50 字数 1500 浏览 1 评论 0原文

我正在使用 PyQt4 编写一个数据库浏览器应用程序,因为我是 Qt 新手,所以遇到了一些问题。我已经用 python 编写了大部分功能,但知道我正在尝试使用 PyQt4 GUI 来实现它们。

首先,我的应用程序现在看起来像这样:
浏览器
在这些目录中,我有我想要解析的 xml 文件。 我写过这样的东西:

<代码> QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.plainTextEdit, QtCore.SLOT("paste()"))

它有效。但是当我尝试做这样的事情时:

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.plainTextEdit, QtCore.SLOT("insertPlainText('test')"))

它失败了。阅读手册后我知道 SIGNAL 和 SLOT 必须采用相同的参数。所以我应该编写一些执行 SLOT 的信号,该 SLOT 是一个带有 QModelIndex 参数的函数,它查找单击的文件,创建 xmldocument 对象,然后打印出它的字典。
我的问题是:

    How can I create such function which is callable as SLOT?


因为在 ui 文件中我创建了一个函数:

定义测试(自我): 打印“调试”

当我试图调用它时

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.test() )

我收到此错误:

类型错误:参数与任何重载调用都不匹配:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection):参数 3 具有意外类型“NoneType”
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): 参数 3 具有意外类型“NoneType”
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection):参数 2 具有意外类型“str”


还有一个问题:

    How should I get path to clicked file from QModelIndex?

I'm writing a database browser application with PyQt4 and because I'm new to Qt, I have some problems. I have most functionalities already written in python but know I'm trying to implement them with PyQt4 GUI.

First of all my application now looks like this:
Browser
In these catalogues I have xml files which I would like to parse.
I've written something like this:


QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.plainTextEdit, QtCore.SLOT("paste()"))

and it works. But when I'm trying to do something like this:

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.plainTextEdit, QtCore.SLOT("insertPlainText('test')"))

it fails. After reading the manual I know that SIGNAL and SLOT must take the same arguments. So I should write some signal which executes a SLOT which is a function with QModelIndex argument which finds clicked file, creates the xmldocument object and then prints out it's dictionaries.
My questions are:

    How can I create such function which is callable as SLOT?

Because inside ui file I created a function:


def test(self):
print "Debug"

And when I'm trying to call it out

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL("clicked(QModelIndex)"), self.ui.test())

I'm getting this error:

TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'str'

And another question:

    How should I get path to clicked file from QModelIndex?

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

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

发布评论

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

评论(1

无声情话 2024-11-10 01:44:50

考虑使用 PyQt 提供的新型连接机制:

self.ui.treeView.clicked.connect(self.ui.plainTextEdit.paste)

现在,paste 是一个接受单个参数的方法,并且该参数的类型为 clicked 信号>self.ui.treeView 发送。如您所愿,从那里进行解剖。


如何创建可作为 SLOT 调用的函数?

这是一个不同的错误。您连接到 self.ui.test(),但这是函数调用,而不是函数。删除test后面的()。但更好的是,使用如上所述的新型连接机制。


我应该如何从 QModelIndex 获取单击文件的路径?

可能是通过调用其 data 方法。阅读 QModelIndex 的文档

Consider using the new-style connection mechanism provided by PyQt:

self.ui.treeView.clicked.connect(self.ui.plainTextEdit.paste)

Now, paste is a method accepting a single argument, and this argument is of the type the clicked signal of self.ui.treeView sends. Dissect if from there, as you wish.


How can I create such function which is callable as SLOT?

It's a different error. You connect to self.ui.test(), but this is a function call, not a function. Remove the () after test. But better yet, use the new-style connection mechanism as described above.


How should I get path to clicked file from QModelIndex?

Probably by calling its data method. Read the doc for QModelIndex

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