Multi-font in QLineEdit while typing



  • Hi,

    I have a query related to QlineEdit. I want to change the font while I am typing a number in the QLineEdit. I am typing some decimal number in the LineEdit and while typing the digits before decimal point is in some other font say 32 and after decimal point(as soon as it detects a '.' it should start typing in some other font say 27. I have been able to achieve it by subclassing QlineEdit and reimplementing its paintEvent method but the issue is that the cursor blinks only at one position and does not go forward as I type the digits.
    Here is the code snippet for it:

    @void CustomLineEdit::paintEvent(QPaintEvent *p)
    {
    this->setEchoMode(QLineEdit::NoEcho);
    this->setCursor(QCursor(Qt::BlankCursor));
    QLineEdit::paintEvent(p);
    QPainter *painter = new QPainter(this);
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(Qt::NoPen);
    QRect rect(0,0, 20,20);
    QRect paramNameRect = rect;
    QRect paramValueRect = paramNameRect;
    paramNameRect.setBottom(38);
    paramValueRect.setTop(38);
    painter->save();
    painter->setPen(Qt::black);
    painter->setFont(QFont("Arial", 32));
    QString ParameterValue;
    ParameterValue = this->text();
    this->setPlaceholderText(this->text());
    if(ParameterValue.contains(".", Qt::CaseInsensitive))
    {
    QStringList decNumList = ParameterValue.split(".");
    painter->drawText(paramValueRect.x(),paramValueRect.y(), decNumList.at(0));
    QFontMetrics m(QFont("Arial", 32));
    int intPartpixelWidth = m.width(decNumList.at(0));
    painter->drawText(paramValueRect.x()+intPartpixelWidth+1,paramValueRect.y(), ".");
    painter->setFont(QFont("Arial", 27,QFont::Bold));
    QFontMetrics m2(QFont("Arial", 32));
    int decimalpixelWidth = m2.width(QString("."));
    painter->drawText(paramValueRect.x()+intPartpixelWidth+decimalpixelWidth,paramValueRect.y(),decNumList.at(1));
    this->setValidator(new QDoubleValidator(-999.0,999.0, 2, this));

    }
    else
    {
        painter->drawText(paramValueRect.x(),paramValueRect.y(), ParameterValue);
    }
    painter->restore();
    

    }@

    I tried removing "this->setEchoMode(QLineEdit::NoEcho);", but if I remove it, it displays double digits. I guess one which is typed by default and other which is painted by the paint method, so i need to keep NoEcho. But because of this cursor is not moving forward. Also, if I click somewhere else on the screen and then again try to delete the contents of LineEdit its not getting possible. Any help will be appreciated.

    Thanks in advance,
    Shweta



  • No help yet!!!



  • Without having tried to understand your method with subclassing of QLineEdit, I'd rather use QTextEdit in this case. You can limit QTextEdit to one line only in order to mimic the behaviour of QLineEdit and create a properly html-formatted string on the fly.


Log in to reply
 

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