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. Error: array initializer must be an initializer list or string literal
Forum Updated to NodeBB v4.3 + New Features

Error: array initializer must be an initializer list or string literal

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 12.6k 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.
  • B Offline
    B Offline
    berrste
    wrote on 24 May 2013, 00:44 last edited by
    #1

    when I try to do this:

    @char string1[] = str;
    //str is a QString@

    I get this error

    @error: array initializer must be an initializer list or string literal@

    What can I do to fix this?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 24 May 2013, 06:52 last edited by JohanSolo
      #2

      A QString cannot be casted into a char array, have a look at the doc. You can for instance use

      char string1[] = str.toStdString().c_str();
      char string1[] = str.unicode();
      

      IIRC depending on your Qt version, the first solution won't work. Note also that depending on your encoding (latin1, utf8, ...) you may need to set the codec for the char array to contain what you expect.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alek Śmierciak
        wrote on 24 May 2013, 07:09 last edited by
        #3

        It is discouraged to use C-style static arrays in C++; the code you wrote will probably not compile because of not specifying an array size beforehand.

        Instead of going this way, have a look at "C++ std::string":http://www.cplusplus.com/reference/string/string/ or better yet, since you're dabbling in Qt, stick to "QStrings":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html when wanting to represent textual data of any kind and to "QByteArrays":http://qt-project.org/doc/qt-5.0/qtcore/qbytearray.html when wanting to represent "lower-level" data (bytes, flags, et cetera).

        @#include <QByteArray>
        #include <QDebug>
        #include <QString>

        int main()
        {
        QString string = QString::fromUtf8("This is a test QString variable. ½ ¼ ¾ ⅓ ⅔ † ‡ µ ¢ £ €");
        QByteArray array = string.toUtf8();

        qDebug() << string;
        qDebug() << array;
        

        }@

        1 Reply Last reply
        0

        1/3

        24 May 2013, 00:44

        • Login

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