Qt World Summit: Submit your Presentation

[Solved]how to add combo box in trayicon menu?

  • Hi, Basicaly I wanted to add a list of items which will be in a combo box and once use will click on it, it will get expanded. I am able to add actions successfully but don;t knwo how to add a combo box.
    since I am new to QT it would be great if you can give an example as well.

  • Well, the menu you show is just a QMenu, I guess. You fill your QMenu with QActions. One subclass of QAction is QWidgetAction, which can be used to show a widget for an action. That sounds like a feasible angle of attack for this issue.

  • You did read this in the [[Doc:QSystemTrayIcon]] API docs, didn't you?

    bq. To add a system tray entry, create a QSystemTrayIcon object, call setContextMenu() to provide a context menu for the icon, and call show() to make it visible in the system tray.

    Any reason why that's not sufficient?

  • Thanks Andre for your response.
    It will be great if you can give an example, how to use QWidgetAction to add combo box in system tray menu.
    I can see only tow definitions of QWidgetAction(parent) / QWidgetAction(const QWdigetAction &) and none is taking combo box as argument.

    I am using below code to add in system tray menu.



  • What's so hard to follow the steps described in the docs?

    bq. To add a system tray entry, create a QSystemTrayIcon object, call setContextMenu() to provide a context menu for the icon, and call show() to make it visible in the system tray.

    // To add a system tray entry, create a QSystemTrayIcon object
    QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon( QIcon("/Users/volker/src/VDM/Softkat/qt4-multishop/softkat/images/xmedia_16.png"));

    // call setContextMenu() to provide a context menu for the icon
    QMenu *trayMenu = new QMenu(this);
    trayMenu->addAction("Tray Action 1");
    trayMenu->addAction("Tray Action 2");

    // and call show() to make it visible in the system tray.

  • Volker, the question was not on how to add a menu, but how to put a combobox in that menu.

    Volker is right about the documentation. Some quotes from the [[doc:QWidgetAction]] docs:
    quote if you want to implement an action that uses custom widgets for visualization in multiple containers then you have to subclass QWidgetAction.

    If a QWidgetAction is added for example to a QToolBar then QWidgetAction::createWidget() is called. Reimplementations of that function should create a new custom widget with the specified parent.[/quote]

    So: subclass QWidgetAction, reimplement createWidget() to return your combo box, create an instance of that subclassed action, and insert it in your menu.

  • A combo box in a tray icon's menu - weird. I hope I never run into the situation of using such a user interface, sorry.

  • Hey, I did not come up with that requirement, but I agree it's probably a bad idea. It is possible, though.

  • Finally! You got what I am asking.
    Let me tell you the requirement. I wanted to add a list in tray menu which will be having some options, once user click on it a drop down list will be visible and it will contain those options. I thought combo box will help. Let me know if any other option is available to achieve the same.

  • How about having checkable (possibly exclusive) menu options instead?

  • Can I add radio button in tray menu? Because otu of those option user can select at a time only one.

  • Yes, you can. See [[doc:QActionGroup]].

  • Thanks Andre,
    I have successfully able to create QActionGroup But how to add it into Qmenu that is not given any where in the link given by you. could you please help me to find out how to add QActionGroup into Qmenu?

    Thanks in advance. :)

  • Hey,
    I am able to add
    but somehow leftAlignAct->setChecked(true); not working for me. I am not able to marked as checked any action in the group.

  • Adding should work like this:

    On the not being able to mark an item as checked: could you show some code please? The code you show is from the example on the QActionGroup page.

  • I am working on a updater module which will be having a tray menu. Below is the code piece I am working on.
    Once1DayAction = new QAction(tr("Once in every day."), this);
    connect(Once1DayAction, SIGNAL(triggered()), this, SLOT(UpdateGroupActs()));

     Once7DayAction = new QAction(tr("Once in 7 days."), this);
     connect(Once7DayAction, SIGNAL(triggered()), this, SLOT(UpdateGroupActs()));
    alignmentGroup = new QActionGroup(this);
     // create tray icon menu
     trayIconMenu = new QMenu(this);
     trayIconMenu->addSeparator()->setText(tr("Updates"));  // Separater is visible but Text is not visible in the menu.   
     trayIcon = new QSystemTrayIcon(this);


    Note: I tried to use "checkedAction" to see whether any thing is checked but it is returning NULL, seems nothing is set.

  • For some reason you must call setCheckable(true) on the actions to become checkable.

  • Hi Volker,
    I tried both setChecked(true) and setCheckable(true) but none of them help. Not sure what is causing this problem. Please let me know if you have any clue.

  • I don't have any further ideas now. It did not work for me without calling setCheckable at first. After adding that, I could check the menu entries.

    Which OS are you using?

  • Thanks Every one thanks for your support and petience. It is working now. I had to use both in particular order.

Log in to reply