Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change icon size depending on screen resolution.
Qt 6.12 LTS is out! See what's new in the release blog

Change icon size depending on screen resolution.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 4.7k Views
  • 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.
  • N Offline
    N Offline
    nen777w
    wrote on last edited by nen777w
    #1

    I have a GUI application with many controls: buttons, tool buttons, tabs,.. e.t.c.
    Also, I have a self-defined UI style (due: QApplication::setStyle() I give that information if it may help me in the question.)
    Some icons I set in Qt GUI editor, and some of them manually, but anyway generated code and my code, looks the same like that:

        QIcon icon;
        icon.addPixmap(QPixmap(":/icons2/icon_zoom_lock.png"), DEF_ICO_MODE, DEF_ICO_STATE);
        m_syncZoomButton->setIcon(icon);
        m_syncZoomButton->setIconSize(QSize(24, 24));
    

    I have several displays connected to my PC (FullHD (100% system scale) and 4k (150% system scale)).
    The problem is that on 4k the icon 24x24 looks nice, but on FullHD display, it's too big and down-scale it to 18x18 will be nice in that case.
    Is it possible to Qt change the icon size depending on the screen resolution in automatic or semi-automatic mode?
    I will be very appreciated for the hint.

    Thank you.

    KillerSmathK 1 Reply Last reply
    0
    • N nen777w

      I have a GUI application with many controls: buttons, tool buttons, tabs,.. e.t.c.
      Also, I have a self-defined UI style (due: QApplication::setStyle() I give that information if it may help me in the question.)
      Some icons I set in Qt GUI editor, and some of them manually, but anyway generated code and my code, looks the same like that:

          QIcon icon;
          icon.addPixmap(QPixmap(":/icons2/icon_zoom_lock.png"), DEF_ICO_MODE, DEF_ICO_STATE);
          m_syncZoomButton->setIcon(icon);
          m_syncZoomButton->setIconSize(QSize(24, 24));
      

      I have several displays connected to my PC (FullHD (100% system scale) and 4k (150% system scale)).
      The problem is that on 4k the icon 24x24 looks nice, but on FullHD display, it's too big and down-scale it to 18x18 will be nice in that case.
      Is it possible to Qt change the icon size depending on the screen resolution in automatic or semi-automatic mode?
      I will be very appreciated for the hint.

      Thank you.

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      @nen777w
      It is possible notify the application just connecting the screenchanged signal from QWindow pointer of this widget.

      The screenchanged signal is emitted when your widget moves out of actual screen to another one, so you can implement a logic to resize your buttons depending of size of window.

      Note: windowHandle() return the QWindow pointer of a widget but it is always null until the window has been showed. So, i recomend you to connect the signal while reimplement the showevent of your widget.

      Example (QMainWindow derived class):

      // .h file
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      protected:
          void showEvent(QShowEvent *event);
      
      private slots:
          void screenChanged(QScreen *screen);
      
      private:
          Ui::MainWindow *ui;
      };
      
      //. cpp file
      #include <QScreen>
      #include <QWindow>
      ...
      void MainWindow::showEvent(QShowEvent *event)
      {
          QMainWindow::showEvent(event);
          connect(windowHandle(), &QWindow::screenChanged, this, &MainWindow::screenChanged, Qt::UniqueConnection); // avoid duplicate (Unique)
      }
      
      void MainWindow::screenChanged(QScreen *screen)
      {
          qDebug() << "Window Changed: " << screen->name();
          const QSize size = screen->availableSize();
          qDebug() << "Before -> " << ui->pushButton->iconSize();
          ui->pushButton->setIconSize(QSize(calc_prop(24, size.height()),calc_prop(24, size.height())));
          qDebug() << "After -> " << ui->pushButton->iconSize();
      }
      

      It is used a function calc_prop to calculate the proporcional size of the icon depending of the horizontal lines of the window.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      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
      • Unsolved