Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to translate string in ListElement?
Forum Updated to NodeBB v4.3 + New Features

How to translate string in ListElement?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 2.4k 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.
  • O Offline
    O Offline
    owenzhao
    wrote on last edited by
    #1

    @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.

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

      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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        task_struct
        wrote on last edited by
        #3

        Hello,

        You can use "this approach":http://developer.qt.nokia.com/wiki/Qt_Quick_Carousel#70b4903abcb62ace84264ad0443ae759

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        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