转换为 python 后是否可以从 QT Designer 访问自定义属性?

发布于 2024-08-22 21:15:09 字数 315 浏览 4 评论 0原文

我是 PyQt 和 Qt Designer 的新手,我正在尝试创建一种简单的方法来将 qWidgets 与 SQLite 数据库中的表和列相关联。我的想法是用两个自定义属性标记设计器中的每个 qWidget,一个带有表名称,一个带有列名称。后来,我将使用设计者提供的信息来构建我自己的类,该类在 qwidets 和 SQLite 数据库之间创建关系。

在 Designer 中添加自定义属性似乎工作正常,但是,将设计器的 xml 转换为 python(使用 UIC)时,不会生成这些自定义属性的代码。有人成功做到这一点吗?也许有更好的方法来做到这一点?

谢谢,

埃里克

I am new to PyQt and Qt Designer and I'm trying to create an easy method for relating qWidgets with the tables and columns in an SQLite database. My idea was to tag each qWidget in designer with two custom properties, one with the table name and one with the column name. Later, I would use the info provided by designer to build my own class which creates a relationship between the qwidets and SQLite database.

Adding the custom properties in Designer seems to work fine however, the code for these custom properties do not get generated when converting the xml of designer into python (using UIC). Has anyone done this successfully? Perhaps there is a better way to do this?

Thanks,

Eric

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

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

发布评论

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

评论(3

放血 2024-08-29 21:15:09

如果您在 Designer 中添加了一个名为“myproperty”的属性,请使用...获取它...

mywidget.property("myproperty")

这在 pyqt 4.8.3 中工作正常,也许在以前的版本中不起作用。

If you added a property in Designer called "myproperty", fetch it with...

mywidget.property("myproperty")

This works fine in pyqt 4.8.3, perhaps it did not work in previous versions.

旧情别恋 2024-08-29 21:15:09

查看这篇文章埃里克。特别要注意标题为“生成插件”的部分。 River BankComputing 还有另一个很棒的 PyQt 参考

编辑:
我一直在阅读更多内容,但找不到一种方法可以自动为您完成此操作。如果您同意在运行时而不是设计时添加动态属性,您可以实现相同的最终结果。 这里是 setProperty( ) 方法在 QObject 中,QWidget 继承自该方法。

如果这对您不起作用,那么您最好采用不太通用的方法。您可以使用从 QSqlTableModel 派生的自定义类来跟踪您的连接信息,而不是使用通用的 QWidget。另一种方法是仅使用 QTableView 并自己执行查询来填充数据。 此处这里是有关 Qt 数据库的文章。您可能会从其中之一中获得新设计的灵感。

Check out this article Eric. Particularly look at the section titled "Producing a Plugin." River Bank Computing has another great PyQt reference.

EDIT:
I've been doing some more reading and I can't find a way to have this done automatically for you. If you are ok with with adding dynamic properties at run-time instead of design time you could accomplish the same end result. Here is an explanation of the setProperty() method in QObject, from which QWidget inherits.

If that doesn't work for you then it seems you might be better off going with a less generic approach. Instead of using a generic QWidget, you might be able to use a custom class derived from QSqlTableModel to keep track of your connection info. Another way would be to just use a QTableView and do the queries yourself to populate the data. Here and here are articles on databases in Qt. You might some inspiration for a new design from one of them.

一曲琵琶半遮面シ 2024-08-29 21:15:09

我在使用设计器工具方面运气不佳——通常我最终会使用 pyuic 进行一些粗略的布局,然后手动编辑和添加其他内容。

听起来您可以通过创建自己的自定义类来轻松完成您的任务,该自定义类继承自 QWidget 并具有您所描述的其他属性。根据我尝试在设计器中使用自定义小部件的经验,我认为“更简单”的方法是自己编写类,然后手动设置布局。

我知道这并不能完全回答您的问题,但也许您会尝试我的一些建议。

I have not had great luck with using the Designer tool -- usually I end up doing a few rough layout, using pyuic and then editing and adding other stuff by hand.

It sounds like you could easily accomplish your task by creating your own custom class that inherits from QWidget and has the additional properties that you described. With my experiences trying to use custom widgets in the designer, I think the 'easier' way is to just write the class yourself and then set the layout by hand.

I know this doesn't exactly answer your question, but maybe you will try some of my suggestions.

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