14

I am attempting to add an icon to my QMenu using Qt Designer, however I realized that my text disappears when my icon is added. Is there any way for me to show my icon next to my text?

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • Is it happening with a toolbutton in maintoolbar? or when you drop the menu and actions appear at that place? – Tab Sep 17 '13 at 04:30
  • No a QMenu is placed on a QMenuBar. The Text of the QMenu is `File` and if I add an icon to it the text `File` disappears and only the icon is present – MistyD Sep 17 '13 at 04:36
  • 1
    I think there is not any provided method its just my suggestion that use a Icon that have text along with picture ;). Nice question btw! – Tab Sep 17 '13 at 04:50
  • I think there should be a style property with options like `TextBesideIcon`, `IconOnly`... like in `QToolButton`. – headsvk Sep 17 '13 at 07:19
  • @lpapp I thought about that. Between *I didn't understand the question*, *maybe it is supported* and *I have not had time to check whether it actually works* not awarding the bounty seems to be the best option. – nwp Dec 11 '14 at 10:57
  • @nwp: well, we (at least I) will not write you a ready-made solution from scratch for a very corner case without any effort shown, not even for 50 bounties. The theory should just work and fine-tuning is left with reader as a finger exercise. ;-) But fair enough, it is up to you after all, so no grudge held or anything. Hope you get it working; good luck! – László Papp Dec 11 '14 at 10:59
  • @nwp: btw, if you put up 500 reputation points, I think someone might provide a polished answer if you really cannot get it work yourself. – László Papp Dec 11 '14 at 12:58
  • @lpapp I had hoped for a simple answer like what [headsvk hinted at](http://stackoverflow.com/questions/18841141/qmenu-item-text-disappears-when-icon-added?noredirect=1#comment27800729_18841141): something like *change `QMenu->style` from `IconOnly` to `TextBesideIcon`*. I understand that a reimplementation of `QMenu` is too much to ask and did not expect that to be necessary. As a workaround I used an icon-only-menu and a text-only-menu next to each other with the same content. People do not seem to care about the difference. 500 is almost a third of my hard earned rep, you do it ;D – nwp Dec 11 '14 at 13:08
  • @nwp: in that case, I do not know what you expected. We told you that it is not possible, just with ugly workaround or difficult. You think your question cannot be answered then? There is no IconOnly or TextBesideIcon since this feature is uncommon. It does not mean that it is not valuable for you, but not for the majority. Wow, it is strange that users do not care about duplicated menu list! It would be a strange user experience to me. I would rather apply an image with text on it, then draw and save the image programatically or just doing once in some painter app. – László Papp Dec 11 '14 at 13:10
  • Hi, could you find how this is possible? – Saeed Masoomi Dec 27 '17 at 10:54

2 Answers2

2

It was not supported in Qt 4, maybe it is in Qt5 I haven't checked. In Designer itself there isn't much you can do. In the code one option is to customize the style to draw both the icon and text: - sizeFromContents for QStyle::CT_MenuBarItem - drawControl for QStyle::CE_MenuBarItem - drawCustomControl for QStyleOptionMenuItem

László Papp
  • 51,870
  • 39
  • 111
  • 135
guest
  • 21
  • 2
  • Can you give an example with a given `ui->menuMyQMenu`? – nwp Dec 05 '14 at 12:34
  • @nwp: the answer was submitted as an unregistered guest, unfortunately. Not sure if they get notification under such circumstances when you write to them. – László Papp Dec 10 '14 at 15:49
0

This is not supported by default, mostly because it is not usual an operation that you wish to achieve in here. Of course, you could always use an image with text included, but that is also hackish, unless you paint the image dynamically and then load it later. Although even that would be quite a bit of work.

In order to do, you will need to fiddle with Qt a bit. This is the closest experiment that I would start off with, personally. I have not had time to check whether it actually works, but there should be something among these lines:

class CustomMenuBarWidget : public QWidget
{
public:
    explicit CustomMenuBarWidget(QWidget *parent = Q_NULLPTR)
        : QWidget(parent)
        , menuBar(new QMenuBar())
        {
        }

    virtual void paintEvent(QPaintEvent *event) {
        QStyleOptionMenuItem styleOptionMenuItem;
        QIcon icon("path/to/my/icon");
        styleOptionMenuItem.icon = icon;
        styleOptionMenuItem.text = "Hello World!";
        QPainter painter(this);
        menuBar->style()->drawControl(QStyle::CE_MenuBarItem, &styleOptionMenuItem, &painter, menuBar);
    }
private:
    QMenuBar *menuBar;
};

You could probably also have a look at QWidgetAction how to insert custom widgets into toolbars and menubars. I have never used that myself in any serious project, but might be useful to be aware of.

László Papp
  • 51,870
  • 39
  • 111
  • 135