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

How to print text with the right font



  • Hi!
    How to print text with the right font?
    If I do so, the font does not change.

        QString testText = "test1";
    
        int id = QFontDatabase::addApplicationFont(":/fonts/code128.ttf"); 
        QString family = QFontDatabase::applicationFontFamilies(id).at(0); 
        QFont font(family);  
        font.setPixelSize(30);
    
        QStaticText testStaticText;
        testStaticText.setText(testText);
        QTransform  transform;
        testStaticText.prepare(transform,font);
    
        QPrinter printer;
    
        QPainter painter;
        painter.begin(&printer);
        painter.drawStaticText(0, 0, testStaticText);
        painter.end();
    


  • it is work

        painter.setFont(font);
        painter.drawText(0, 0, 200, 30, Qt::AlignLeft|Qt::AlignTop, testText); //int x, int y, int width, int height,
    

Log in to reply