How to translate string in ListElement?



  • @ListElement {
    logColor: "darkgrey"
    log: qsTr("This is a long sentence.")
    }@
    and I got the error code
    @ListElement: cannot use script for property value @

    Any ideas?
    Thank you.



  • I will consider implementing a model using C++ that will store the strings to translate. (I have done this for displaying a list with settings entries that have to be translated)

    Header
    @
    class MyModel : public QAbstractListModel
    {
    Q_OBJECT
    Q_PROPERTY(int count READ count NOTIFY countChanged)
    public:
    enum SelectorRole {
    TextRole
    };
    explicit MyModel(QObject *parent = 0);
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int count() const;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    Q_SIGNALS:
    void countChanged(int count);
    public Q_SLOTS:
    void addEntry(const QString &text);
    private:
    QStringList _items;
    };
    @

    Implementation
    @
    MyModel::MyModel(QObject *parent) :
    QAbstractListModel(parent)
    {
    // Definition of roles
    QHash <int, QByteArray> roles;
    roles.insert(TextRole, "text");
    setRoleNames(roles);
    }

    int MyModel::rowCount(const QModelIndex &parent) const
    {
    Q_UNUSED(parent)
    return _items.count();
    }

    int MyModel::count() const
    {
    return rowCount();
    }

    QVariant MyModel::data(const QModelIndex &index, int role) const
    {
    if (index.row() < 0 or index.row() >= rowCount())
    {
    return QVariant();
    }

    switch(role) {
    case TextRole:
        return _items.at(index.row());
        break;
    default:
        return QVariant();
        break;
    }
    

    }

    void SelectorModel::addEntry(const QString &text)
    {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());

    _items.append(text);
    
    emit countChanged(rowCount());
    endInsertRows();
    

    }
    @

    And declare it in QML using

    @
    qmlRegisterType<MyModel>(uri, 1, 0, "MyModel");
    @

    Finally, invoke it in QML with

    @
    MyModel {
    id: model
    Component.onCompleted {
    addEntry(qsTr("First entry with translation"))
    addEntry(qsTr("Second entry with translation"))
    }
    }
    @

    PS : It seems that in Qt4.8 there are some impouvements :
    https://bugreports.qt-project.org/browse/QTBUG-11403




Log in to reply
 

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