Search code examples
qtqt4

QLabel setText not displaying text of QLineEdit if label size small


I have a QLabel in which I want to show text from a QLineEdit. The size of line-edit is bigger than the label so I want to show the label ending with a dotted line

ui->LE_Serverpath// contains 20 charecters

ui->LB_UsernameInfo  // having size of 10 charecters

ui->LB_UsernameInfo->setText(ui->LE_Serverpath->text());

using wordwrap the line is getting cut but I need dotted lines at the end

ui->LB_UsernameInfo->setWordWrap(true); 

Solution

  • You have to set an ElideMode for the label, you need a QFontMetrics instance from label font then set text elide mode for the label. to show dots by end of line, set ElidMode to Qt::ElideRight, on the text copied form QLieEdit:

    //QFontMetrics metrics(ui->LB_UsernameInfo->font()); // QLabel already has font metrics
    
    int width = ui->LB_UsernameInfo->width() - 2;
    QString text = ui->LB_UsernameInfo->fontMetrics().elidedText(ui->LE_Serverpath->text(), Qt::ElideRight, width);
    ui->LB_UsernameInfo->setText(text);
    ui->LB_UsernameInfo->setWordWrap(true);