Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. pointer to array of QString.
Forum Update on Monday, May 27th 2025

pointer to array of QString.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 4.7k Views
  • 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.
  • F Offline
    F Offline
    FranciscoSantiago
    wrote on 22 Aug 2016, 15:56 last edited by
    #1

    Hi everyone,

    I have a question for you, doctor in programming world.
    I have two files .cpp. In one of these I have an array of QString and I want to get it from the other file.
    The name of QString array is history_array[30]

    So in the first file I implement this function:

    QString history_array[30];
    ...
    
    void Data_mng::get_userHistory_array (QString *p)
    {
        *p=history_array;
    }
    

    while in the second file I implement these two code lines to get the array.

    Data_mng dat;    
    QString *test[30];
    dat.get_userHistory_array(&test);
    

    Who can tell me where I'm doing wrong? Who can tell me how can I solve it?
    I think it's not a simple question...the array of QString is not easy to manage....

    Thank you folks!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 22 Aug 2016, 16:56 last edited by
      #2

      *p is of type QString, history_array is of type QString[] you cannot = the two

      &test is of type QString**[] so does not match the function signature

      void Data_mng::get_userHistory_array (QString *& p)
      {
          p=history_array;
      }
      
      Data_mng dat;    
      QString *test;
      dat.get_userHistory_array(test);
      

      P.S.
      This design looks really bad but I don't think it's the point here

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      F 1 Reply Last reply 22 Aug 2016, 18:11
      2
      • V VRonin
        22 Aug 2016, 16:56

        *p is of type QString, history_array is of type QString[] you cannot = the two

        &test is of type QString**[] so does not match the function signature

        void Data_mng::get_userHistory_array (QString *& p)
        {
            p=history_array;
        }
        
        Data_mng dat;    
        QString *test;
        dat.get_userHistory_array(test);
        

        P.S.
        This design looks really bad but I don't think it's the point here

        F Offline
        F Offline
        FranciscoSantiago
        wrote on 22 Aug 2016, 18:11 last edited by
        #3

        @VRonin

        Thank you Robin for you help.
        It works, but I have the access only to the first element of the array..

        Do you know how can I get the entire array?

        Thanks, again

        M R 2 Replies Last reply 22 Aug 2016, 19:08
        0
        • F FranciscoSantiago
          22 Aug 2016, 18:11

          @VRonin

          Thank you Robin for you help.
          It works, but I have the access only to the first element of the array..

          Do you know how can I get the entire array?

          Thanks, again

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 22 Aug 2016, 19:08 last edited by mrjj
          #4

          @FranciscoSantiago
          Hi
          No matter what you do the syntax will be ugly and the size (30) will fast get lost.
          Is there any reason you cannot use a QStringList ?

          also, if you keep the list inside a class, why give it away to outside code?

          why not do
          QString Data_mng::get_userHistory_str (int index) {
          // check index
          return history_array[index];
          }

          F 1 Reply Last reply 22 Aug 2016, 19:54
          1
          • M mrjj
            22 Aug 2016, 19:08

            @FranciscoSantiago
            Hi
            No matter what you do the syntax will be ugly and the size (30) will fast get lost.
            Is there any reason you cannot use a QStringList ?

            also, if you keep the list inside a class, why give it away to outside code?

            why not do
            QString Data_mng::get_userHistory_str (int index) {
            // check index
            return history_array[index];
            }

            F Offline
            F Offline
            FranciscoSantiago
            wrote on 22 Aug 2016, 19:54 last edited by
            #5

            @mrjj
            I didn't know what QStringList was..

            Thank you.. I go to discover it.. ;)

            M 1 Reply Last reply 22 Aug 2016, 20:00
            1
            • F FranciscoSantiago
              22 Aug 2016, 19:54

              @mrjj
              I didn't know what QStringList was..

              Thank you.. I go to discover it.. ;)

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 22 Aug 2016, 20:00 last edited by mrjj
              #6

              @FranciscoSantiago
              its a better way to have list of QStrings

              http://doc.qt.io/qt-5/qstringlist.html#details

              and you can just do

              QStringList & Data_mng::GetThelist() {
              return history_list;
              }

              1 Reply Last reply
              1
              • F FranciscoSantiago
                22 Aug 2016, 18:11

                @VRonin

                Thank you Robin for you help.
                It works, but I have the access only to the first element of the array..

                Do you know how can I get the entire array?

                Thanks, again

                R Offline
                R Offline
                RajeeshRaveendran
                wrote on 23 Aug 2016, 04:49 last edited by RajeeshRaveendran
                #7

                @FranciscoSantiago

                increment the pointer by 1 to get the next QString object.

                eg:-

                QString strings[30]; /// Array of 30 strings
                QString* pStrings = strings; /// Pointer to string array.

                QString firstString = *(pStrings + 0);
                QString secString = *(pStrings + 1);
                QString thirdString = *(pStrings + 2); /// You can also loop it. But beware that you are not accessing beyond 30th item.
                ...

                But anyway it is better to use QStringList instead(it is easy and you can save lot of efforts as above code)

                Regards,
                Rajeesh Raveendran

                1 Reply Last reply
                2

                1/7

                22 Aug 2016, 15:56

                • Login

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