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. How to display an image with correct physical dimension on screen (1cm in real = 1cm on screen)
Forum Updated to NodeBB v4.3 + New Features

How to display an image with correct physical dimension on screen (1cm in real = 1cm on screen)

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 344 Views 2 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by ademmler
    #1

    hi there,

    I am trying to figure out how to calculate the scale factor to display an given image
    in a GraphicsView that 1cm of the image will be 1cm on screen.
    Or in other words hat the image is display in his real size.

    Of course I need to take into considerations:
    Image density (resolution)
    Physical DPI of the monitor

    In order to determine what Qt is providing I wrote this small code.
    Below is the output from the debugger when I load an image with 20cm at 150 dpi density.

    Would somebody give me the hint wich Parameters I need to calculate the scale factor?

    ui->graphicsView->resetTransform();
    int imageResolution = 150;
    
        QScreen* screen = QGuiApplication::primaryScreen();
        double logicalDpiX = screen->logicalDotsPerInchX();
        double logicalDpiY = screen->logicalDotsPerInchY();
    
        double physicalDpiX = screen->physicalDotsPerInchX();
        double physicalDpiY = screen->physicalDotsPerInchY();
    
        double desiredImageSizeXmm = m_processedImage.width() / imageResolution * 2.54;
        double desiredImageSizeYmm = m_processedImage.height() / imageResolution * 2.54;
    
        double imageSizeXmm = m_processedImage.widthMM();
        double imageSizeYmm = m_processedImage.heightMM();
    
        double logicalImageSizeX = m_processedImage.logicalDpiX();
        double logicalImageSizeY = m_processedImage.logicalDpiY();
    
        double imageSizeX = m_processedImage.width();
        double imageSizeY = m_processedImage.height();
    
        //float scaleFactorX =  ???;
        //float scaleFactorY =  ???;
        //float scaleFactor = qMin(scaleFactorX, scaleFactorY);
    
        ui->graphicsView->scale(scaleFactor, scaleFactor);
        ui->graphicsView->viewport()->rect().center();
    

    Debugger Output:
    [statics]
    desiredImageSizeXmm 20.32 double
    desiredImageSizeYmm 15.24 double
    imageSizeX 1204.0 double
    imageSizeXmm 425.0 double
    imageSizeY 963.0 double
    imageSizeYmm 340.0 double
    logicalDpiX 72.0 double
    logicalDpiY 72.0 double
    logicalImageSizeX 72.0 double
    logicalImageSizeY 72.0 double
    physicalDpiX 109.00000163701577 double
    physicalDpiY 109.00000163701577 double

    johngodJ 1 Reply Last reply
    0
    • johngodJ johngod

      @ademmler Hi

      I did this in some old project, please adapt to you needs

      int DpiManager::getPixelsFromCentimeters(const double &centimeters_)
      {
          // 1 inch = 2.54 centimeters
          // x inch =  n centimeters
          // x inchs = n / 2.54
         // double inchs = centimeters_ / 2.54;
          // m_physicalDotsPerInch = 1 inch
          // x dots = n inchs
      
          // x = n inchs * m_physicalDotsPerInch;
      
          return int(centimeters_ / 2.54 * m_physicalDotsPerInch);
      
      //m_physicalDotsPerInch is a cached property
      //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch();
      }
      

      In qml I do this:

      property real mm: Screen.pixelDensity
      property real my1centimerterSize: 10 * mm
      
      ademmlerA Offline
      ademmlerA Offline
      ademmler
      wrote on last edited by ademmler
      #3

      @johngod Thx John I checkt his out but it did not worked.

      However I found simple a solution:
      The rendering of Qt is in pixels without any scaling. This means that the scaling factor is (physical screen resolution / divided by image desinty)

      1 Reply Last reply
      1
      • ademmlerA ademmler

        hi there,

        I am trying to figure out how to calculate the scale factor to display an given image
        in a GraphicsView that 1cm of the image will be 1cm on screen.
        Or in other words hat the image is display in his real size.

        Of course I need to take into considerations:
        Image density (resolution)
        Physical DPI of the monitor

        In order to determine what Qt is providing I wrote this small code.
        Below is the output from the debugger when I load an image with 20cm at 150 dpi density.

        Would somebody give me the hint wich Parameters I need to calculate the scale factor?

        ui->graphicsView->resetTransform();
        int imageResolution = 150;
        
            QScreen* screen = QGuiApplication::primaryScreen();
            double logicalDpiX = screen->logicalDotsPerInchX();
            double logicalDpiY = screen->logicalDotsPerInchY();
        
            double physicalDpiX = screen->physicalDotsPerInchX();
            double physicalDpiY = screen->physicalDotsPerInchY();
        
            double desiredImageSizeXmm = m_processedImage.width() / imageResolution * 2.54;
            double desiredImageSizeYmm = m_processedImage.height() / imageResolution * 2.54;
        
            double imageSizeXmm = m_processedImage.widthMM();
            double imageSizeYmm = m_processedImage.heightMM();
        
            double logicalImageSizeX = m_processedImage.logicalDpiX();
            double logicalImageSizeY = m_processedImage.logicalDpiY();
        
            double imageSizeX = m_processedImage.width();
            double imageSizeY = m_processedImage.height();
        
            //float scaleFactorX =  ???;
            //float scaleFactorY =  ???;
            //float scaleFactor = qMin(scaleFactorX, scaleFactorY);
        
            ui->graphicsView->scale(scaleFactor, scaleFactor);
            ui->graphicsView->viewport()->rect().center();
        

        Debugger Output:
        [statics]
        desiredImageSizeXmm 20.32 double
        desiredImageSizeYmm 15.24 double
        imageSizeX 1204.0 double
        imageSizeXmm 425.0 double
        imageSizeY 963.0 double
        imageSizeYmm 340.0 double
        logicalDpiX 72.0 double
        logicalDpiY 72.0 double
        logicalImageSizeX 72.0 double
        logicalImageSizeY 72.0 double
        physicalDpiX 109.00000163701577 double
        physicalDpiY 109.00000163701577 double

        johngodJ Offline
        johngodJ Offline
        johngod
        wrote on last edited by
        #2

        @ademmler Hi

        I did this in some old project, please adapt to you needs

        int DpiManager::getPixelsFromCentimeters(const double &centimeters_)
        {
            // 1 inch = 2.54 centimeters
            // x inch =  n centimeters
            // x inchs = n / 2.54
           // double inchs = centimeters_ / 2.54;
            // m_physicalDotsPerInch = 1 inch
            // x dots = n inchs
        
            // x = n inchs * m_physicalDotsPerInch;
        
            return int(centimeters_ / 2.54 * m_physicalDotsPerInch);
        
        //m_physicalDotsPerInch is a cached property
        //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch();
        }
        

        In qml I do this:

        property real mm: Screen.pixelDensity
        property real my1centimerterSize: 10 * mm
        
        ademmlerA 1 Reply Last reply
        3
        • johngodJ johngod

          @ademmler Hi

          I did this in some old project, please adapt to you needs

          int DpiManager::getPixelsFromCentimeters(const double &centimeters_)
          {
              // 1 inch = 2.54 centimeters
              // x inch =  n centimeters
              // x inchs = n / 2.54
             // double inchs = centimeters_ / 2.54;
              // m_physicalDotsPerInch = 1 inch
              // x dots = n inchs
          
              // x = n inchs * m_physicalDotsPerInch;
          
              return int(centimeters_ / 2.54 * m_physicalDotsPerInch);
          
          //m_physicalDotsPerInch is a cached property
          //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch();
          }
          

          In qml I do this:

          property real mm: Screen.pixelDensity
          property real my1centimerterSize: 10 * mm
          
          ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by ademmler
          #3

          @johngod Thx John I checkt his out but it did not worked.

          However I found simple a solution:
          The rendering of Qt is in pixels without any scaling. This means that the scaling factor is (physical screen resolution / divided by image desinty)

          1 Reply Last reply
          1
          • ademmlerA ademmler has marked this topic as solved on

          • Login

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