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

How to get clean text/icon from QTextDocument/html?



  • Hello all. I have used the following code to create dialer buttons so that they have the smaller letters underneath like any phone dialer:

    QString numberStyle = "color: #7f7f7f; font-family: Verdana; font-weight: bold;";
        QString numberSize = "font-size: 19px;";
        if(currentTheme.compare("z-theme-light")==0 || currentTheme.compare("z-theme-dark")==0){
                numberStyle = "color: rgba(91, 91, 93, 0.6); font-family: \"SF Pro Text\";";
                numberSize = "font-size: 22px;";
        }
        //Main Page - Dialer Buttons
        QTextDocument doc;
        doc.setHtml("<p align=center style='"+numberStyle+"'><font style='"+numberSize+"'>1</font><br/><font style='font-size: 9px;'> </font></p>");
        QPixmap pixmap(40, doc.size().height());
        doc.setTextWidth(40);
        pixmap.fill( Qt::transparent );
        QPainter *painter = new QPainter(&pixmap);
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setRenderHint(QPainter::TextAntialiasing);
        doc.drawContents(painter);
        ui->dialerOne->setIconSize(pixmap.size());
        ui->dialerOne->setIcon(pixmap);
    

    The problem is, I've tried the renderhints and some other things and it just doesn't come out clean. I'm attaching an image of the output. The final row (*, 0, #) is clean because that's just text directly in the button (since there's nothing underneath it) so the above code doesn't apply to those buttons. As you can see, the first 9 buttons though are not very pretty. Is there something I'm missing to get clean output? Thanks.
    alt text



  • @wesblake what if you try using icons directly instead of constructing every button?

    ui->dialerOne->setIcon("/path/to/icon/one);
    ui->dialerTwo->setIcon("/path/to/icon/two);
    

    and you store the icons in the Qt resource system


Log in to reply