From Qt Wiki
Jump to: navigation, search

QUtf8ByteLimitValidator is an example of a user-defined QValidator class which can be used in C++ and QML. This validator converts characters to UTF-8 byte array and checks if this byte array size exceeds value.

Class reference


  • int : length

This property defines the number of bytes. If text length in bytes exceeds this value, validation results in QValidator::Invalid otherwise validation results in QValidator::Acceptable. The validation of empty string results in QValidator::Intermediate.

Default value is: UINT_MAX

  • QString : prefix

Prefix is a case-insensitive property with which string begins with. The length of prefix is not not considered in a string validation.

Default value is: "" (empty)

  • bool : less

If less is false, validation will return QValidator::Acceptable only if string length is equal to length property, otherwise validation results in QValidator::Intermediate.

Default value is: true


static void declareQML()

Use this method in int main to declare QUtf8ByteLimitValidator class in QML.

Example (QML)

In the start of qml file:

    import QtUtf8ByteLimitValidator 1.0
    TextField {
        id: toxId
        font.pointSize: 32
        leftPadding: 10
        rightPadding: leftPadding
        verticalAlignment: TextInput.AlignVCenter
        width: parent.width
        text: ""
        validator: BytesValidator { length: bridge.getToxAddressSizeHex(); prefix: "tox:"; less: false }
        color: acceptableInput ? "black" : "red"

Source code

#include <QValidator>
#include <QQmlComponent>
class QUtf8ByteLimitValidator : public QValidator {
    Q_PROPERTY(int length READ getLength WRITE setLength)
    Q_PROPERTY(QString prefix READ getPrefix WRITE setPrefix)
    Q_PROPERTY(bool less READ getLess WRITE setLess)
     explicit QUtf8ByteLimitValidator(QObject *parent = nullptr) : QValidator(parent) {
        m_length = UINT_MAX;
        m_prefix = "";
        m_less = true;
    ~QUtf8ByteLimitValidator() {}
    QValidator::State validate(QString &input, int &) const
        int prefix_length = 0;
        if (!m_prefix.isEmpty() && input.left(m_prefix.length()).toUpper() == m_prefix.toUpper()) {
            prefix_length = m_prefix.toUtf8().length();
        QByteArray bytes = input.toUtf8();
        if (bytes.length() - prefix_length > m_length) {
            parent()->setProperty("acceptableInput", false);
            return QValidator::Invalid;
        if (!m_less && bytes.length() - prefix_length < m_length) {
            parent()->setProperty("acceptableInput", false);
            return QValidator::Intermediate;
        parent()->setProperty("acceptableInput", true);
        return QValidator::Acceptable;
    void fixup(QString &) const {}
    static void declareQML() { qmlRegisterType<QUtf8ByteLimitValidator>("QtUtf8ByteLimitValidator", 1, 0, "BytesValidator"); }
    int getLength() { return m_length; }
    void setLength(int length) { m_length = length; }
    QString getPrefix() { return m_prefix; }
    void setPrefix(const QString &prefix) { m_prefix = prefix; }
    bool getLess() { return m_less; }
    void setLess(bool less) { m_less = less; }
    int m_length;
    QString m_prefix;
    bool m_less;