Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I set a different font for different texts when printing?



  • Hi!
    How do I set a different font for different texts when printing?
    If I do so, the font will be the same. I need to adjust the font for each text.

        QPrinter printer;
        
        QFont font;
        font.setPixelSize(15);
        
        QPainter painter;
          painter.begin(&printer);
          painter.setFont(font);
    
          //painter.drawText(x, y, width, height, Qt::AlignLeft|Qt::AlignTop, text); //int x, int y, int width, int height,
          painter.drawText(x, y, width, height, Qt::AlignLeft|Qt::AlignTop, "777"); //int x, int y, int width, int height,
          painter.drawText(30, 10, width, height, Qt::AlignLeft|Qt::AlignTop, "888"); //int x, int y, int width, int height,
    
          painter.end();
    


  • It's works

        QPrinter printer;
    
        QFont font15;
        font15.setPixelSize(20);
    
        QPainter painter;
          painter.begin(&printer);
    
          painter.setFont(font15);
    
          //painter.drawText(x, y, width, height, Qt::AlignLeft|Qt::AlignTop, text); //int x, int y, int width, int height,
          painter.drawText(0, 0, width, height, Qt::AlignLeft|Qt::AlignTop, "666"); //int x, int y, int width, int height,
    
          font15.setPixelSize(35);
          painter.setFont(font15);
          painter.drawText(30, 20, width, height, Qt::AlignLeft|Qt::AlignTop, "777"); //int x, int y, int width, int height,
          //painter.setFont(font25);
    
          painter.end();
    

Log in to reply