2

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!

1 Answers1

0

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.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • Yeah... Looks like extending QMenu is the right direction to move to. Here [1] someone has similar problem. The question is now: how to align the QMenu.. -_- [1] http://stackoverflow.com/questions/8704668/how-to-put-pushbutton-inside-the-qmenu-or-qaction-control –  Sep 10 '13 at 15:46
  • 1
    Well, at least looking at the provided link, it seems that in that scenario the person still did not want to customize the layout and type of menu items, just the contents, which i guess is doable that way. The Dropbox example changes everything, even the outer shape of the window, so you will need control at a higher level I guess. So can you accept my answer then? – Rudolfs Bundulis Sep 10 '13 at 16:24
  • I really tried to make it work but eventually gave up. I switched to Objective C for interface code. I wanted to implement this in Qt and paste it here, so other people can see it. Accepting your answer. –  Nov 21 '13 at 15:22