How to set ui according to dpi in qt4



  • Hello all,

    I am trying to create an ui which can change its size according to dpi(dots per inch). there may be a method in qt 5 but i want to do it using qt4. So please help me to change ui size according to dpi. How is it possible in qt4 ??

    thank you.



  • Here, i can find out physical dpi or logical dpi by using following methods:

    int nY = QApplication::desktop()->physicalDpiY();
    int nX = QApplication::desktop()->physicalDpiX();

    but how can i set size of widget according to above dpi.

    All suggestion are welcomed.
    Thanx in Advance,



  • This Qt Blog article covered the new Qt 5 auto-dpi adjustments, with backports to Qt 4.

    Article: http://blog.qt.digia.com/blog/2013/04/25/retina-display-support-for-mac-os-ios-and-x11/
    Backport: https://codereview.qt-project.org/#change,54636

    If you're able to get the backport patch to work, the desktop manager will report an updated DPI, and Qt will do the adjustment for scalable content automatically. Scalable content includes text size and vector graphics, but not raster graphics by default. Check the article for the details, including using:
    qApp->setAttribute(Qt::AA_UseHighDpiPixmaps);

    If you don't want to go this route, you could possibly scale yourself by using the DPI to make a decision about a resolution to set your app size to. You can do this fixed by creating a set of common DPIs and selecting the one closest to your device value, or by trying to create a scale factor relating DPI to resolution.

    You can set then set the geometry of your widgets based on a percentage of the app you want them to take up. You'd do this by overriding resizeEvents for the widgets you want to change size. The resizeEvent will take the full size it's given, and use percentages of that size to size its children.

    Example: You have a vertical toolbar, a content pane, and a right vertical side bar. The resizeEvent for your containing widget will do:
    @verticalToolBar->setGeometry(0, 0, size.width() * 0.1, size.height());
    contentPane->setGeometry(size.width() * 0.1, 0, size.width() * 0.8, size.height());
    verticalSideBar->setGeometry(size.width() * 0.9, 0, size.width() * 0.1, size.height());@



  • thanx Adolby. I m working on it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.