Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding widget to QSystemTrayIcon



  • I need something like small widget, hanging constantly near system tray. I decided to add QWidgetAction to menu and setContextMenu() for QSystemTrayIcon. But it does not work, widget does not appear. If i add that action to QMenubar, everything ok.

    QWidget *TrayWidgetAction::createWidget(QWidget *parent)
    {
        auto trayWidget = new QWidget(parent);
        inputLangs = new QComboBox(trayWidget);
        outputLangs = new QComboBox(trayWidget);
        QStandardItemModel* model = new QStandardItemModel(inputLangs);
        proxy1 = new QSortFilterProxyModel();
        proxy1->setSourceModel(model);
        proxy1->sort(0);
        inputLangs->setModel(proxy1);
    
        auto input = new TrayInpuTextEdit(trayWidget);
        auto output = new QTextEdit(trayWidget);
        auto grid = new QGridLayout(trayWidget);
    
        input->setFixedSize(110,70);
        output->setFixedSize(110,70);
    
        grid->addWidget(input,0,0,1,1,Qt::AlignCenter);
        grid->addWidget(output,0,1,1,1,Qt::AlignCenter);
        grid->addWidget(inputLangs,1,0,1,1);
        grid->addWidget(outputLangs,1,1,1,1);
        grid->setContentsMargins(1,1,1,1);
    
        trayWidget->setFixedSize(225,100);
        trayWidget->setLayout(grid);
        return trayWidget;
    }
    

    and the creating:

        widgetAction = new TrayWidgetAction(this);
        auto trayMenu = new QMenu(this);
        trayMenu->addAction(widgetAction);
        trayIcon->setContextMenu(trayMenu);
        trayIcon->show();
    

    Also i have found this bugreport:
    https://bugreports.qt.io/browse/QTBUG-26840

    UPDATE:
    On Windows it is working, so the problem is only on Linux


Log in to reply