Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProxyStyle with Qmenu and tray appear slowly first time
Forum Updated to NodeBB v4.3 + New Features

QProxyStyle with Qmenu and tray appear slowly first time

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 675 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by
    #1

    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);
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      KaguroK 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        KaguroK Offline
        KaguroK Offline
        Kaguro
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          KaguroK 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • SGaistS SGaist

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

              KaguroK Offline
              KaguroK Offline
              Kaguro
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                KaguroK 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  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.

                  KaguroK Offline
                  KaguroK Offline
                  Kaguro
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved