Qt Designer 与手动编码
每次我使用图形工具包开始一个项目时,第一个冲突就会发生在如何处理视觉设计和小部件布局的决定上:图形工具还是手动编码?
这是一个非常棘手/主观的问题,因为大多数人会根据个人喜好来决定。它还很大程度上取决于图形工具的质量。在这种情况下,我想只关注 Qt 库的最新版本。我无意讨论哪种方法更好。我确信最好的答案是:取决于项目。
我想要的是一篇基于多个项目经验的好的、公正的文章的参考。这篇文章应该只描述两种选择的权衡。
Every time I start a project with some graphical toolkit, one of the first conflicts happen with the decision of how to deal with the visual design and the widget layout: A graphical tool or handcoding?
This is a quite tricky/subjective question because most people will decide based on personal preference. It also depends greatly on the quality of the graphical tool. In this case, I would like to focus just on the latest version of the Qt library. I do not intend to discuss which method is better. I am convinced that the best answer is: depends on the project.
What I want is a reference to a good, non-biased article, based on experience after several projects. The article should just describe the tradeoffs of both choices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我开始做所有手工编码的事情,最近已经转而使用 Qt Designer 来处理大多数表单。以下是每个职位的一些好处:
使用 Qt Designer
手动编码
其他
QWizard
表单,我发现我需要为每个页面使用不同的 UI 文件。您可以一次完成所有操作,但是以任何一种自定义方式在页面之间进行通信都变得非常尴尬。总之,我从 Qt Designer 开始,让它尽可能地引导我,然后从那里手动编码。这是 Qt Designer 生成的一件好事——它只是成为您的类的成员的另一个类,您可以根据需要访问它并操作它。
I started with doing everything hand-coded, and of late have been switching to using Qt Designer for most forms. Here are some benefits for each position:
Using Qt Designer
Hand-coding
Miscellaneous
QWizard
forms, I have found that I need to use a different UI file for each page. You can do it all in one, but it becomes very awkward to communicate between pages in any kind of custom way.In summary, I start with Qt Designer and let it take me as far as it can, then hand-code it from there. That's one nice thing about what Qt Designer generates--it is just another class that becomes a member of your class, and you can access it and manipulate it as you need.
我的答案基于两年来使用 PyQt4(Python 绑定到 Qt 4)和 OpenGL 开发生物化学应用程序。我没有做过C++ Qt,因为我们只将C++用于性能关键的算法。也就是说,PyQt4 API 与 Qt4 非常相似,因此这里的很多内容仍然适用。
Qt设计师
手工编码
很好
不好
提示
不要直接开始创建窗口。首先快速绘制几种可能的设计草图,无论是在纸上还是使用 Balsamiq Mockups 等工具。虽然您可以在 Qt Designer 中执行此操作,但我认为在决定窗口是否是最佳设计之前花费大量时间试图让窗口看起来恰到好处太诱人了。
如果您使用 PyQt 的 Qt Designer,则需要执行额外的步骤:运行 pyuic4 将 *.ui 文件编译为 Python 源文件。我发现很容易忘记这一步,并且想不明白为什么我的更改不起作用。
如果您手动编写 UI 代码,我建议将布局代码放在一个位置,将信号和槽放在另一个位置。这样做可以更轻松地更改窗口小部件在窗口上的排列方式,而不会影响任何应用程序逻辑。或者,您可以更改某些行为,而无需费力地完成所有布局代码。
My answer is based on two years developing biochemistry applications using PyQt4 (Python bindings to Qt 4) and OpenGL. I have not done C++ Qt, because we only used C++ for performance-critical algorithms. That said, the PyQt4 API greatly resembles Qt4, so much here still applies.
Qt Designer
Hand coding
Good
Bad
Tips
Don't just jump into creating your windows. Start by quickly sketching several possible designs, either on paper or using a tool like Balsamiq Mockups. Though you could do this in Qt Designer, I think it is too tempting to spend a lot of time trying to get your windows to look just right before you've even decided if it is the best design.
If you use Qt Designer for PyQt, you have the extra step of running pyuic4 to compile your *.ui files to Python source files. I found it easy to forget this step and scratch my head for a second why my changes didn't work.
If you code your UI by hand, I suggest putting your layout code in one place and your signals and slots in another place. Doing this makes it easier to change the way your widgets are arranged on a window without affecting any of your application logic. Or you can change some behavior without having to wade through all the layout code.
我倾向于使用设计器布局对话框,但我在主代码中完成所有事件处理工作。我还用直接代码制作所有主窗口、工具栏、菜单。
设计师只是令人沮丧 - 遗憾的是,基于拖放尺寸器的优秀设计师已经存在了十多年
I tend to layout dialogs using the designer but I do all the event handling stuff in the main code. I also do all the main windows, toolbars, menus in direct code.
The designer is just frustrating - a pity since decent drag and drop sizer based designers have been around for more than a decade
这取决于您的应用程序所需的不同窗口/面板的数量。如果数量较少,请使用图形工具。完美设计几个窗口要快得多。如果数量很大,图形工具可以(并且应该)仅用于原型。您需要对布局进行编码,以便能够以可接受的成本进行应用程序范围的更改。
这包括创建应用程序 UI 如何工作的模型以及在运行时动态添加和删除小部件。有关此类模型(在不同环境中)的绝佳示例,请查看魅力模型 用于创建对象浏览器。
我反对认为它是棘手/主观的(至少比其他开发选择更多)。很容易提出决定的标准。个人经验和偏好对此很重要,因为它们决定何时应将不同窗口的数量视为较小。工具质量也是如此。
It depends on the number of different windows/panels you need for your application. If the number is small, use a graphical tool. It is much faster to get a few windows designed perfectly. If the number is large, the graphical tool can (and should) only be used for prototypes. You need to code the layout to be able to make application-wide changes at acceptable cost.
That includes creating a model of how the UI of the application works and dynamically adding and removing widgets at runtime. For an excellent example of such a model (in a different environment), take a look at the glamour model for creating object browsers.
I object to the suggestion that it is tricky/subjective (at least more than other development choices). It is easy to come up with criteria to decide on. Personal experience and preference are important for that, as they decide when the number of different windows should be considered small. The same goes for tool quality.
在某些开发工作流程中,基于 GUI 的工具可能被视为潜在的干扰,导致人们倾向于手动编码,以通过仅关注代码来潜在地增强概念化。这种方法类似于阅读一本没有图像的书,它可以加快开发过程。
对于那些对 C++ 有强烈倾向的人来说,手动编码仍然是保持与该语言一致的实践的可能性,即使它需要编写一些冗余的代码。
虽然nano或vim等工具可用,但有些人可能会发现它们速度较慢,特别是在调试期间。
In some development workflows, GUI-based tools might be perceived as potential distractions, leading to a preference for hand-coding to potentially enhance conceptualization by focusing solely on the code. This approach is similar to reading a book with no images, and it could expedite the development process.
For those with a strong inclination towards C++, hand-coding remains a possibility to maintain consistent practice with the language, even if it entails writing somewhat redundant code.
While tools like nano or vim are available, the possibility exists that some may find them slower, particularly during debugging.
我将两者结合使用:
我发现对于 x,y 坐标,Designer 是最佳选择。
许多其他 UI 属性等可以在您的代码中设置。
我认为尝试完全通过手工编码来完成 UI 将是一个非常耗时的项目。它并不像设置 HTML 表格那么简单。
I use a combination of both:
I find for x,y coordinates, Designer is the way to go.
A lot of the other UI properties etc can be set in your code.
I think trying to do UI completely by hand-coding would be a very time-consuming project. It's not as simple as setting up HTML tables.