Search code examples
c++qtqlineedit

C++ QLineEdit: setmaxlength() for number of bytes and not the number of the characters?


I have a QLineEdit in my application in which I should be able to enter a maximum of 10byte characters in english and while entering Japanese characters , if the character is of 2byte , I should be able to enter only 5 characters in japanese and if the Japanese character is a 1byte character, I should be able to enter 10 characters in japanese. Please help me with this.

I tried using

QLineEdit::setMaxLength(10) 

but it is working fine only with English characters but not with Japanese characters.

Any idea/suggestions ???


Solution

  • I resolved my issue as follow :

    In LineEdit Slot:

     void ABC::on_abc_cmd_task_tableWidget_linedit_cellChanged(QString str)
     {
        QLineEdit *edit = qobject_cast<QLineEdit *>(sender());
    
        if(edit)
        {
            QByteArray chkDataSize;
            chkDataSize.clear();
            chkDataSize.append(str);
            int dSize = chkDataSize.size();
    
            qDebug() << "total Bytes in text = " << dSize;
            qDebug() << "Max length before = " << edit->maxLength();
    
            if (dSize == edit->maxLength())
            {
                 edit->setMaxLength(str.size());
            }
    
            qDebug() << "Max length after = " << edit->maxLength();
    
          }
    
    
     }
    

    Well tested with Russian Characters / Japanese Character etc.