简体   繁体   English

QPushButton和QToolButton之间的区别

[英]Difference between QPushButton and QToolButton

I'm new to Qt and the difference between QPushButton and QToolButton is not so clear to me. 我是Qt的新手, QPushButtonQToolButton之间的区别对我来说并不是那么清楚。

I know that a QToolButton is usually used in a QToolBar and it usually shows only an icon, without text, but I don't quite understand the main difference between both. 我知道QToolButton通常用在QToolBar ,它通常只显示一个图标,没有文字,但我不太明白两者之间的主要区别。

Does it have any bigger difference? 它有什么更大的区别吗? When should I use QPushButton and when should I use QToolButton ? 我什么时候应该使用QPushButton ?何时应该使用QToolButton

I would like to know this to use the most appropriate button, and I need to perform some GUI tests and maybe it can be relevant. 我想知道这个使用最合适的按钮,我需要执行一些GUI测试,也许它可能是相关的。

QToolButton is part of a group of widgets in the QtWidgets module that is focused around QAction : QMenu , QToolBar , and so forth. QToolButton是一组QtWidgets模块在重点是围绕小部件的一部分QActionQMenuQToolBar ,等等。 QPushButton is just a button. QPushButton只是一个按钮。 As a result, QToolButton is much more complex under the hood than QPushButton . 因此, QToolButtonQPushButton更加复杂。 A (not complete) list of examples: 一个(不完整的)示例列表:

  • QToolButton is designed to be tightly integrated with QAction . QToolButton旨在与QAction紧密集成。 Changing the icon, text, etc. of a tool button's default action is reflected on the button. 更改工具按钮的默认操作的图标,文本等会反映在按钮上。
  • You can change the layout of the tool button contents (icon only, text only, text beside icon, text below icon). 您可以更改工具按钮内容的布局(仅图标,仅文本,图标旁边的文本,图标下方的文本)。
  • QToolButton supports a "split" button type where a sidebar hot zone opens a menu instead of triggering the default action. QToolButton支持“拆分”按钮类型,其中侧边栏热区打开菜单而不是触发默认操作。
  • Tool buttons can be created directly in a QToolBar by adding an action, while other widgets must be explicitly added. 可以通过添加动作直接在QToolBar创建工具按钮,而必须显式添加其他小部件。

Also note that: 另请注意:

  • QToolButton has smaller default internal margins than QPushButton , reflecting that it is meant to be displayed in a tight row or grid. QToolButton默认内部边距小于QPushButton ,反映出它意味着以紧密的行或网格显示。 QPushButton is more for "Ok"/"Close" type buttons that contain text with an optional icon. QPushButton更适用于包含带有可选图标的文本的“Ok”/“Close”类型按钮。
  • A QToolButton should generally have an icon. QToolButton通常应该有一个图标。 A QPushButton should always have text. QPushButton应始终包含文本。

From Qt doc: http://doc.qt.io/qt-5/qtoolbutton.html#details 来自Qt doc: http//doc.qt.io/qt-5/qtoolbutton.html#details

"A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead." “工具按钮是一个特殊的按钮,可以快速访问特定的命令或选项。与普通的命令按钮相反,工具按钮通常不显示文本标签,而是显示图标。”

When i want a button in the GUI simple with only an icon, I use QToolButton. 当我想在GUI中只有一个图标的按钮时,我使用QToolButton。 But when i want a classic button, i use QPushButton. 但是当我想要一个经典按钮时,我会使用QPushButton。

No big differences, 没有太大的区别,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM