How can i adjust text of a QWidget just as QMessageBox adjust text automatically ?

  • Can i ? if yes how ?

    this is what i do :

    object->setStylesheet("font:16pt Arial;");

    that makes the font size fixed/static but i want to make it dynamic ? i mean the text should fit itself in a widget on its own just as text in QMessageBox is adjusted on its own when the display resolution is changed.

  • @Ahti

    You can set the font for a widget also through setFont

    If you specify a negative value of point size in the constructor you have default value for the system. Maybe this is of help for you.

  • @koahnig How is setFont method different than stylesheet ? and what do you mean by "negative value of point size in the constructor" where is this point size ?

  • @Ahti

    Just follow the links (under "setFont" and "the contructor" in my previous post) to the parts of the documentation.

    Sorry, I do not know the actual difference, but I would go for the font routines rather setting a style, but that is probably more a matter of taste. However, with setFont you can use also some defaults which you might to set as in your style.

  • Lifetime Qt Champion

    Do you mean to adjust the width of the widget ?
    Like for text
    it will be small
    but for
    it would be much wider?

  • @mrjj I just want the text to fit in the QWidget so that i will get rid of text getting out of alignment in a widget problem. This happens when i run my app on different resolutions like 1440X900, 1920X1080, ect.

  • Lifetime Qt Champion

    Well how are you drawing the text ?
    If you dont word wrap, then text will be cut when less screen space is available.

    Do you u mean that text should shrink to fit inside the available widget rect?

  • @mrjj yeah that is what i want although with word warp it appears on the next line that i don't want to happen i just want it appear in a single line and at the same time it should shrink to fit inside the available widget rect ??

    this is how i draw text :

    QLabel* EyeCare::constructLabel(QWidget *parent,QLabel *labelName,QString style,
                                         int x,int y,int w,int h,QString labelText){
        labelName = new QLabel(parent) ;
        labelName->setStyleSheet( style );
        return labelName ;
    notificationHeader = constructLabel(homeBackground,notificationHeader,
                                             "color: #c8c8c8;",170,135,300,50,"Welcome To EyeCare") ;     
         notificationArea = constructLabel(homeBackground,notificationArea,"color: #c8c8c8;",
                                           0,188,640,45,"Protect your precious eyes as you compute.") ;

  • Lifetime Qt Champion

    you can use

    QFontMetrics fm(font());
    and fm.width(text()) to check size of text
    if too wide, then you must reduce the font.setPointSize and see again.
    Repeat until it fits.

Log in to reply

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