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 set QGraphicstextitem's height in mm?
Qt 6.11 is out! See what's new in the release blog

how to set QGraphicstextitem's height in mm?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5
3 Posts 2 Posters 741 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.
  • S Offline
    S Offline
    Selvajothi
    wrote on last edited by
    #1
    code to add QGraphicstextitem
    
    QFont font;
    font.setFamily(defaultFamily);
    font.setPointSizeF(defaultTxtsize* mmtopt);
    QGraphicstextItem* textitem = new QGraphicstextItem();
    textitem->pointSize = defaultTxtsize;
    textitem->setPlainText(textEdit->text());
    textitem->setFont(font);            
    textitem->setDefaultTextColor(defaultColor);
    textitem->setPos(gridPosition.x()-5,gridPosition.y()-7);
    textitem->setTextInteractionFlags(Qt::TextEditable);
    addItem(textitem);
    
    code to export to pdf:
    
    printer = new QPrinter(QPrinter::HighResolution);
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOutputFileName(pdfFile);//pdfFile contains the name of the  pdf which we have to print
    printer->setPageSize(QPrinter::A3);
    printer->setOrientation(QPrinter::Landscape);
    printer->setPdfVersion(QPagedPaintDevice::PdfVersion_1_6);
    QPainter painter(printer);
    painter.setRenderHint(QPainter::Antialiasing);
    scene->setBackgroundBrush(QBrush(Qt::white));
    scene->render(&painter);
    
    
    //defaultTxtsize = input given by the user in mm
    mmtopt = 2.83465(factor to convert mm to points)
    

    I want to add QGraphicsTextitem with its height measured in millimeter, export it to pdf and print it. After printing I checked using ruler manually and there is difference in text height(user input is 2 mm and in paper it shows 1mm). can anyone help me to find out the mistake.

    A 1 Reply Last reply
    0
    • S Selvajothi
      code to add QGraphicstextitem
      
      QFont font;
      font.setFamily(defaultFamily);
      font.setPointSizeF(defaultTxtsize* mmtopt);
      QGraphicstextItem* textitem = new QGraphicstextItem();
      textitem->pointSize = defaultTxtsize;
      textitem->setPlainText(textEdit->text());
      textitem->setFont(font);            
      textitem->setDefaultTextColor(defaultColor);
      textitem->setPos(gridPosition.x()-5,gridPosition.y()-7);
      textitem->setTextInteractionFlags(Qt::TextEditable);
      addItem(textitem);
      
      code to export to pdf:
      
      printer = new QPrinter(QPrinter::HighResolution);
      printer->setOutputFormat(QPrinter::PdfFormat);
      printer->setOutputFileName(pdfFile);//pdfFile contains the name of the  pdf which we have to print
      printer->setPageSize(QPrinter::A3);
      printer->setOrientation(QPrinter::Landscape);
      printer->setPdfVersion(QPagedPaintDevice::PdfVersion_1_6);
      QPainter painter(printer);
      painter.setRenderHint(QPainter::Antialiasing);
      scene->setBackgroundBrush(QBrush(Qt::white));
      scene->render(&painter);
      
      
      //defaultTxtsize = input given by the user in mm
      mmtopt = 2.83465(factor to convert mm to points)
      

      I want to add QGraphicsTextitem with its height measured in millimeter, export it to pdf and print it. After printing I checked using ruler manually and there is difference in text height(user input is 2 mm and in paper it shows 1mm). can anyone help me to find out the mistake.

      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      @Selvajothi said in how to set QGraphicstextitem's height in mm?:

      font.setPointSizeF(defaultTxtsize* mmtopt);

      You got your units mixed up. Point size is given in 1/72 inch. So if you want your text to be 2 mm high, you just need to convert 2 mm to inch and multiply by 72

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Selvajothi
        wrote on last edited by Selvajothi
        #3

        I have changed as u mentioned and printed it in paper. Still it is not giving the correct height when measured using ruler.![alt text](Screenshot 2021-01-11 183715.png image url)

        font.setPointSizeF(defaultTxtSize0.039370172)

        Here defaultRxtSize is 10mm for the attached image.

        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