将现有 QDialog 保存到 *.ui 文件
我有一个从数据库表模型动态生成的表单:
我有一种方法将该表单保存到*.ui 文件?我想允许用户在 Qt Designer 中编辑该表单。
I have a form generated dynamically from a database table model:
I there a way to save that form to a *.ui file? I want to allow user to edit that form in Qt Designer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想必您的用户可以添加和编辑的内容一定有一些限制?
如果是这样,那么也许您可以采取更结构化的方法并使用 QWizard 提供用于设计和编辑表单的简单界面。该向导将生成
ui
文件,然后使用 uic 模块(如果您使用的是 PyQt4,那就是 - 因为它不包含QUiLoader
类)。当然,要实现此功能,您需要调整当前的动态生成表单的过程,以便它也适用于
ui
文件。编辑
它看起来像
QAbstractFormBuilder
提供了一个 API,用于将小部件加载和保存为 ui 文件。它是 QtDesigner 模块的一部分,现已包含在 PyQt4 中。
话虽如此,我对
load()
和save()
方法的简短实验并没有产生非常有用的结果 - 但希望其他人能有更多的运气。就我个人而言,如果我正在设计这样的应用程序,我更愿意使用合适的 XML 库(Qt 或几个 python 标准库模块之一)自己生成 ui 文件。表单布局的结构非常简单且规则,因此 ui 文件复制起来应该不会太困难。以这种方式进行操作的主要好处是它允许完全控制输入和输出。
Presumably there must be some limitations on what your users can add and edit?
If so, then maybe you could take a more structured approach and use QWizard to provide a simple interface for designing and editing forms. The wizard would generate
ui
files which would then be loaded in your application using the uic module (if you're using PyQt4, that is - because it does not include theQUiLoader
class).Of course, for this to work, you would need to adapt your current procedure for dynamically generating forms so that it also works with
ui
files.EDIT
It looks like
QAbstractFormBuilder
provides an API for both loading and saving widgets as ui files. It is part of the QtDesigner module, which is now included in PyQt4.Having said that, my brief experimentation with the
load()
andsave()
methods did not produce very useful results - but hopefully others will have more luck.Personally, if I was designing an application like this, I would prefer to generate the ui files myself using a suitable XML library (either Qt's, or one of the several python standard library modules). The structure of a form layout is pretty simple and regular, so the ui files should not be too difficult to replicate. The major benefit of doing things this way is that it allows for complete control over the input and output.
QUiLoader 中有一个 load(),但没有 save():
http://developer.qt.nokia.com/doc/qt-4.8/quiloader.html#load
UI 文件格式已记录,并且是 XML。因此,您可以编写自己的 .UI 文件生成器:
http://developer.qt.nokia.com/doc/qt-4.8/designer-ui-file-format.html
事实上,您可以用相反的方式解决这个问题。不是使用编程小部件 API 调用生成对话框...而是使用 XML 生成 .UI 文件。然后您可以将其加载到您的应用程序中或通过 QtDesigner。
(根据您的应用程序的用途或用途,您还可以考虑将其重新考虑为 QtDesigner 插件......在这种情况下,此功能可能是免费的。)
There is a load(), but no save() in the QUiLoader:
http://developer.qt.nokia.com/doc/qt-4.8/quiloader.html#load
The UI file format is documented, and is XML. So you could write your own .UI file generator:
http://developer.qt.nokia.com/doc/qt-4.8/designer-ui-file-format.html
In fact, you could attack this problem the other way around. Instead of generating the dialog using programmatic widget API calls...instead generate a .UI file with XML. Then you can load it in your app or through QtDesigner.
(Depending on what your app is or is intended to do, you might also look into rethinking it as a QtDesigner plug-in...in which case this functionality might come for free.)