QMenu item text disappears when icon added
Asked Answered
H

2

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?

Hamza answered 17/9, 2013 at 4:6 Comment(10)
Is it happening with a toolbutton in maintoolbar? or when you drop the menu and actions appear at that place?Ebsen
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 presentHamza
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!Ebsen
I think there should be a style property with options like TextBesideIcon, IconOnly... like in QToolButton.Arni
@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.Labe
@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!Feuillant
@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.Feuillant
@lpapp I had hoped for a simple answer like what headsvk hinted at: 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 ;DLabe
@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.Feuillant
Hi, could you find how this is possible?Squama
B
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

Brunelle answered 5/12, 2014 at 10:7 Comment(2)
Can you give an example with a given ui->menuMyQMenu?Labe
@nwp: the answer was submitted as an unregistered guest, unfortunately. Not sure if they get notification under such circumstances when you write to them.Feuillant
H
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.

Hutcheson answered 10/12, 2014 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.