Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multi-font in QLineEdit while typing
Forum Updated to NodeBB v4.3 + New Features

Multi-font in QLineEdit while typing

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shweta
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shweta
      wrote on last edited by
      #2

      No help yet!!!

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sashko
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved