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 Updated to NodeBB v4.3 + New Features

pointer to array of QString.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 4.8k 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.
  • FranciscoSantiagoF Offline
    FranciscoSantiagoF Offline
    FranciscoSantiago
    wrote on 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      FranciscoSantiagoF 1 Reply Last reply
      2
      • VRoninV VRonin

        *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

        FranciscoSantiagoF Offline
        FranciscoSantiagoF Offline
        FranciscoSantiago
        wrote on 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

        mrjjM RajeeshRaveendranR 2 Replies Last reply
        0
        • FranciscoSantiagoF FranciscoSantiago

          @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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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];
          }

          FranciscoSantiagoF 1 Reply Last reply
          1
          • mrjjM mrjj

            @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];
            }

            FranciscoSantiagoF Offline
            FranciscoSantiagoF Offline
            FranciscoSantiago
            wrote on last edited by
            #5

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

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

            mrjjM 1 Reply Last reply
            1
            • FranciscoSantiagoF FranciscoSantiago

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

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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
              • FranciscoSantiagoF FranciscoSantiago

                @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

                RajeeshRaveendranR Offline
                RajeeshRaveendranR Offline
                RajeeshRaveendran
                wrote on 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

                • Login

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