text in paintevent



  • Hi,

    alt text

    I have overlay text, aligned , can i have the overlay text aligned one by one vertically in paintevent.

            painter.drawText(rect,Qt::AlignBottom | Qt::AlignLeft,StartString);
            painter.drawText(rect,Qt::AlignBottom | Qt::AlignCenter,Centerstring);
            painter.drawText(rect,Qt::AlignBottom | Qt::AlignRight,EndString);
    

    Thanks,



  • @Pradeep-Kumar

    hi,

    seems like if you add a new line \n QPainter actually draws a new line. Take a look at the docu here

    In case you want to use multiple drawTexts, you'll have to add an Vertical offset to the rect , of at least the average Char height, instead of a horizontal offset but that seems kind of obvious!?



  • I used \n , but u specified vertical offset , how can i achieve ,?.

    Thanks,



  • @Pradeep-Kumar

    QFontMetrics metrix(painter->font());
    int hOffset = metrix.height();
    
    painter.drawText(rect,Qt::AlignBottom | Qt::AlignLeft,StartString);
    rect = rect.setY(rect.y()+hOffset);
    painter.drawText(rect,Qt::AlignBottom | Qt::AlignCenter,Centerstring);
    rect = rect.setY(rect.y()+hOffset);
    painter.drawText(rect,Qt::AlignBottom | Qt::AlignRight,EndString);
    

  • Moderators

    @Pradeep-Kumar I'm not sure I understand your question. But isn't Qt::AlignVCenter what you're looking for?



  • Hi @Pradeep-Kumar ,

    Try This,

    QRect firstTextrect(0,0,rect.width(),rect.heght()/3);
    QRect secTextrect(0,firstTextrect.bottomLeft(),rect.width(),rect.heght()/3);
    QRect ThirdTextrect(0,secTextrect.bottomLeft(),rect.width(),rect.heght()/3);

    painter.drawText(firstTextrect,Qt::AlignLeft,StartString);
    painter.drawText(secTextrectr,Qt::AlignLeft,Centerstring);
    painter.drawText(ThirdTextrect,Qt::AlignLeft,EndString);


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.