Qt Designer 与手动编码

发布于 2024-10-01 07:28:57 字数 227 浏览 6 评论 0原文

每次我使用图形工具包开始一个项目时,第一个冲突就会发生在如何处理视觉设计和小部件布局的决定上:图形工具还是手动编码?

这是一个非常棘手/主观的问题,因为大多数人会根据个人喜好来决定。它还很大程度上取决于图形工具的质量。在这种情况下,我想只关注 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 技术交流群。

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

发布评论

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

评论(6

慕巷 2024-10-08 07:28:57

我开始做所有手工编码的事情,最近已经转而使用 Qt Designer 来处理大多数表单。以下是每个职位的一些好处:

使用 Qt Designer

  • 对我来说,最大的节省时间是管理复杂的布局;它节省了很多繁琐的编码工作。简单地(非常粗略地)排列您的小部件,选择它们,右键单击,然后将它们放入正确的布局类型中。特别是当布局变得嵌套时,这变得更加容易。
  • 它倾向于使您的实现文件保持干净,而不是用所有样板布局代码填充它们。我是A型,所以我喜欢这样。
  • 如果您正在翻译您的应用程序,可以向您的翻译人员发送 .ui 文件,以便他们可以在您的 GUI 上看到他们正在翻译的文本所在的位置。 (假设他们使用 Qt Linguist。)

手动编码

  • 控制。如果您的布局需要以非常特定的顺序实例化/初始化控件,或者根据其他条件(数据库查找等)动态创建控件,那么这是最简单的方法。
  • 如果您有自定义小部件,您可以使用设计器,添加您的类派生的最接近的内置 QWidget,然后“升级”它。但是,除非您将其作为单独项目中的设计器插件,否则您将看不到小部件的预览,这对于大多数用例来说工作量太大。
  • 如果您有自定义小部件,其构造函数中的参数超出了可选的 QWidget 父级,则 Designer 无法处理它。您别无选择,只能手动添加该控件。

其他

  • 使用自动连接插槽和信号功能(基于命名约定,例如“on_my_button_clicked”。)我发现我几乎总是必须设置在一个确定的时间建立这个连接,而不是每当 Qt 为我做的时候。
  • 对于 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

  • The biggest time saver for me is managing complex layouts; it saves a lot of tedious coding. Simply (very roughly) arrange your widgets, select them, right-click, and put them in the correct type of layout. Especially as layouts become nested, this is so much easier.
  • It tends to keep your implementation files cleaner instead of filling them with all the boilerplate layout code. I'm type-A, so I like that.
  • If you are translating your application, it is possible to send your translators the .ui files so they can see on your GUI where the text they are translating will be. (Assuming they are using Qt Linguist.)

Hand-coding

  • Control. If you have a layout where you need to instantiate / initialize the controls in a very particular order, or dynamically create the controls based on other criteria (database lookup, etc.), this is the easiest way.
  • If you have custom widgets, you can kind-of-sort-of use the Designer, adding the closest built-in QWidget from which your class derived and then "upgrading" it. But you won't see a preview of your widget unless you make it a designer plugin in a separate project, which is way too much work for most use cases.
  • If you have custom widgets that take parameters in their constructor beyond the optional QWidget parent, Designer can't handle it. You have no choice but to add that control manually.

Miscellaneous

  • I don't use the auto-connect SLOTS and SIGNALS feature (based on naming convention such as "on_my_button_clicked".) I have found that I almost invariably have to set up this connection at a determinate time, not whenever Qt does it for me.
  • For 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.

自演自醉 2024-10-08 07:28:57

我的答案基于两年来使用 PyQt4(Python 绑定到 Qt 4)和 OpenGL 开发生物化学应用程序。我没有做过C++ Qt,因为我们只将C++用于性能关键的算法。也就是说,PyQt4 API 与 Qt4 非常相似,因此这里的很多内容仍然适用。

Qt设计师

    • 探索。了解哪些小部件可用、这些小部件的名称、您可以为每个小部件设置哪些属性等。
    • 强制将 UI 逻辑与应用程序逻辑分离。
  • 坏的
    • 如果您需要在运行时添加或删除小部件,则必须在代码中包含该逻辑。我认为将 UI 逻辑放在两个地方不是一个好主意。
    • 更改嵌套布局。当布局中没有小部件时,它会折叠,并且很难将小部件拖放到您想要的位置。

手工编码

  • 很好

    • 如果您非常熟悉 Qt,速度会很快。
    • 如果您需要在运行时添加或删除小部件,这是最佳选择。
    • 如果您有自己的自定义小部件,则比 Qt Designer 更容易。
    • 通过纪律,您仍然可以将 UI 布局与行为分开。只需将用于创建和布局小部件的代码放在一个位置,并将用于设置信号和槽的代码放在另一位置即可。
  • 不好

    • 如果您是 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

  • Good
    • Exploration. Discover what widgets are available, the names for those widgets, what properties you can set for each, etc.
    • Enforces separation of UI logic from application logic.
  • Bad
    • If you need to add or remove widgets at run-time, you have to have that logic in code. I think it's a bad idea to put your UI logic in two places.
    • Making changes to nested layouts. When a layout has no widgets in it, it collapses, and it can be really hard to drag and drop a widget in to the location you want.

Hand coding

  • Good

    • Fast if you are very familiar with Qt.
    • Best choice if you need to add or remove widgets at run-time.
    • Easier than Qt Designer if you have your own custom widgets.
    • With discipline, you can still separate UI layout from behavior. Just put your code to create and layout widgets in one place, and your code to set signals and slots in another place.
  • Bad

    • Slow if you are new to Qt.
    • Does not enforce separation of layout from behavior.

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.

南城旧梦 2024-10-08 07:28:57

我倾向于使用设计器布局对话框,但我在主代码中完成所有事件处理工作。我还用直接代码制作所有主窗口、工具栏、菜单。

设计师只是令人沮丧 - 遗憾的是,基于拖放尺寸器的优秀设计师已经存在了十多年

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

最偏执的依靠 2024-10-08 07:28:57

这取决于您的应用程序所需的不同窗口/面板的数量。如果数量较少,请使用图形工具。完美设计几个窗口要快得多。如果数量很大,图形工具可以(并且应该)仅用于原型。您需要对布局进行编码,以便能够以可接受的成本进行应用程序范围的更改。

这包括创建应用程序 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.

天邊彩虹 2024-10-08 07:28:57

在某些开发工作流程中,基于 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.

ㄟ。诗瑗 2024-10-08 07:28:57

我将两者结合使用:

  1. 我发现对于 x,y 坐标,Designer 是最佳选择。

  2. 许多其他 UI 属性等可以在您的代码中设置。

我认为尝试完全通过手工编码来完成 UI 将是一个非常耗时的项目。它并不像设置 HTML 表格那么简单。

I use a combination of both:

  1. I find for x,y coordinates, Designer is the way to go.

  2. 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.

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