Question

I want to add icons in QMainWindow and when i will click that window it should perform some action like popup some window. So what should i use for the icon menu?

Was it helpful?

Solution

You could use the QToolButton class to accomplish this task.

It is possible to set it to only contain an image/icon without text.

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.

So, you would use these methods:

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.

and

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.

As you can see, the default is icon only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top