Question

I am trying to code context menu similar to the one Dropbox has:

enter image description here

I thought that the best way to do this is to style QMenu attached to the QSystemTrayIcon:

class canvas : public QMenu
{
    Q_OBJECT
public:
    canvas(QObject* parent = 0);
};

canvas::canvas(QObject* parent) : QMenu()
{
    setStyleSheet("QMenu{background-color:#00ff00;}");
}

class tray_icon : public QSystemTrayIcon
{
    Q_OBJECT
public:
    tray_icon(canvas* a_canvas, QObject* parent = 0);
};

tray_icon::tray_icon(canvas* a_canvas, QObject* parent) : QSystemTrayIcon(parent)
{
    setContextMenu(a_canvas);
}

But for some reason QMenu does not react to the setStyleSheet call and it stays the same.

I have couple questions:

  1. What I am doing wrong in this situation (why the background colour is not set to green).
  2. Is QMenu the right tool to build dropdowns like that (maybe I should use some other widget). How Dropbox made such a menu?

Thank you!

Was it helpful?

Solution

As for the style sheet - since I've only done non-customized tray menus with QT it's hard for me to say what is wrong with the given example, i think you need to override the items in the menu bar as well, since this would only set the background for the menu, and since the item takes the whole width, I think you just can't see the background. Try setting styles for QMenu::item and QMenu::item:selected to see if that helps.

As for the approach - again, haven't done the exact thing, but i don't think that a generic QMenu will fit this, since you don't have much control over how buttons are laid out and I don't see a way to get blank space like in the Dropbox example, I think you need your own widget implementation and add other widgets, lay them out etc. You could subclass QMenu and try adding some custom widgets like buttons etc to see if that can help you give the changes you need.

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