gpt4 book ai didi

c++ - 在 QT 中设置和操作图标

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:45 27 4
gpt4 key购买 nike

我想在 QMainWindow 中添加图标,当我单击该窗口时,它应该执行一些操作,例如弹出某个窗口。那么我应该为图标菜单使用什么?

最佳答案

您可以使用 QToolButton类来完成这个任务。

可以将其设置为仅包含没有文本的图像/图标。

l buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts.

A tool button's icon is set as QIcon. This makes it possible to specify different pixmaps for the disabled and active state. The disabled pixmap is used when the button's functionality is not available. The active pixmap is displayed when the button is auto-raised because the mouse pointer is hovering over it.

The button's look and dimension is adjustable with setToolButtonStyle() and setIconSize(). When used inside a QToolBar in a QMainWindow, the button automatically adjusts to QMainWindow's settings (see QMainWindow::setToolButtonStyle() and QMainWindow::setIconSize()). Instead of an icon, a tool button can also display an arrow symbol, specified with arrowType.

因此,您将使用这些方法:

QAction * QToolBar::addAction(const QIcon & icon, const QString & text)

Creates a new action with the given icon and text. This action is added to the end of the toolbar.

toolButtonStyle : Qt::ToolButtonStyle

This property holds whether the tool button displays an icon only, text only, or text beside/below the icon.

The default is Qt::ToolButtonIconOnly.

To have the style of toolbuttons follow the system settings (as available in GNOME and KDE desktop environments), set this property to Qt::ToolButtonFollowStyle.

QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides.

如您所见,默认只有图标。

关于c++ - 在 QT 中设置和操作图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21107314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com