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

QProxyStyle with Qmenu and tray appear slowly first time



  • Hi Guys!
    I wanna create a tray apllication with a Qmenu. I added QActions (one with icon) to QMenu. I wanted to resize the QAction's icons so i had to use a special QProxyStyle class like below, and i added it to the QMenu. When i start the application, and I make a right click at the first time on the app tray icon , the Qmenu appear slowly but only at the first time, after it, it is working perfeclty and quick.
    Any advice where is my mistake(s)?
    Thank you!

    mainwindow.h

    #include <QProxyStyle>
    class MyProxyStyle: public QProxyStyle {
     public:
      MyProxyStyle(QStyle* style = 0) : QProxyStyle(style) { }
      MyProxyStyle(const QString& key) : QProxyStyle(key) { }
      virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0 ) const {
        switch ( metric ) {
          case QStyle::PM_SmallIconSize:
            return 40; // here u want pixmaps size i assume
          default:
            return QProxyStyle::pixelMetric( metric, option, widget ); // return default values for the rest
        }
      }
    
    };
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
    
        //system tray and menu
        QAction *minimizeAction;
        QAction *maximizeAction;
        QAction *ConnectDisconnetAction;
        QAction *quitAction;
        QSystemTrayIcon *trayIcon;
        QMenu *trayIconMenu;
        void createActions();
        void createTrayIcon();
        MyProxyStyle *mystlye;
    
    
    private slots:
        //system tray and menu
        void setCur();
    
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        createActions();
        mystlye = new MyProxyStyle;
        createTrayIcon();
    
    }
    void MainWindow::createActions()
    {
        minimizeAction = new QAction(tr("item1"), this);
        connect(minimizeAction, &QAction::triggered, this, &MainWindow::removeDocuments);
        minimizeAction->setIcon(QIcon("xy.ico"));
    
        maximizeAction = new QAction(tr("item2"), this);
        connect(maximizeAction, &QAction::triggered, this, &MainWindow::setPath);
    
        ConnectDisconnetAction = new QAction(tr("item3"), this);
        ConnectDisconnetAction->setCheckable(true);
        ConnectDisconnetAction->setChecked(true);
    
        connect(ConnectDisconnetAction, &QAction::triggered, this, &MainWindow::setConnectDisconnet);
    
        quitAction = new QAction(tr("item4"), this);
        connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
    }
    
    void MainWindow::createTrayIcon()
    {
        trayIconMenu = new QMenu(this);
        trayIconMenu->setStyle(mystlye);
     
        trayIconMenu->addAction(minimizeAction);
        trayIconMenu->addAction(maximizeAction);
        trayIconMenu->addAction(ConnectDisconnetAction);
        trayIconMenu->addSeparator();
        trayIconMenu->addAction(quitAction);
    
        trayIcon = new QSystemTrayIcon(this);
    
        trayIcon->setContextMenu(trayIconMenu);
    
        QIcon icon = QIcon("xy.ico");
        trayIcon->setIcon(icon);
        setWindowIcon(icon);
        trayIcon->setToolTip("xyz");
        trayIcon->show();
    
        connect(trayIconMenu, &QMenu::aboutToShow, this, &MainWindow::setCur);
    }
    
    void MainWindow::setCur()
    {
        trayIconMenu->setCursor(Qt::PointingHandCursor);
    
    }
    

  • Lifetime Qt Champion

    Hi,

    Which version of Qt ?
    On which OS ?
    Do you have the same behaviour without the style ?



  • @SGaist QT 5.14.2, Windows 10 64bit, no without the QProxyStyle its working perfectly.


  • Lifetime Qt Champion

    Just as a test, do you have the same behaviour if you set just a QProxyStyle ?


  • Lifetime Qt Champion

    Or use your proxy style but don't change the icon size?



  • @SGaist @Christian-Ehrlicher If i use my stlye like this:

    #include <QProxyStyle>
    class MyProxyStyle: public QProxyStyle {
    };
    
    

    The problem is the same :/ it can be problam that i create this class in the mainwindow class header?


  • Lifetime Qt Champion

    Please provide a minimal, compilable example to reproduce your issue. I don't see why it should take longer when you simply derive from QProxyStyle.



  • @Christian-Ehrlicher Sorry for late! I had so many different work :( Today i will provide a minimal example! Thanks a lot! :)


Log in to reply