Change icon size depending on screen resolution.
-
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.
-
@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.