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. Copy QStringList pointer to QStringList
Forum Update on Monday, May 27th 2025

Copy QStringList pointer to QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 4.2k 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.
  • SikarjanS Offline
    SikarjanS Offline
    Sikarjan
    wrote on last edited by A Former User
    #1

    Hi,

    how can I make a copy of a QStringList pointer that is not a new pointer? I am learning by doing and even if I've been reading about pointers I am still very unsure of the concept.

    My goal is to dynamically update a QCompleter:

    void CodeEditor::scanForVariables(){
        QStringList customCompList = new QStringList(completerList); // <- this is not working because completerList is a pointer. If it is not a pointer other parths of my code will not work
    
        QRegularExpression variableExpression = QRegularExpression("\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*");
        QRegularExpressionMatchIterator matches = variableExpression.globalMatch(this->toPlainText());
        while (matches.hasNext()) {
            QRegularExpressionMatch match = matches.next();
            customCompList << match.captured();
        }
    
        QRegularExpression functionExpression = QRegularExpression("(?<=function )[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?=\\s*\\()");
        matches = functionExpression.globalMatch(this->toPlainText());
        while (matches.hasNext()) {
            QRegularExpressionMatch match = matches.next();
            customCompList << match.captured();
        }
    
        completerModel->setStringList(customCompList);
    }
    

    My problem is that setStringList does not allow pointers.

    Thanks for any hints!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The first question is: why are you using pointers to QStringList ?

      There's no need for that in your use case.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SikarjanS Offline
        SikarjanS Offline
        Sikarjan
        wrote on last edited by
        #3

        Hi,

        the completerList is used in different codeEditors. If it is not a pointer it will get copied over and over, right?

        Best

        1 Reply Last reply
        0
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          Hi,
          first of, @SGaist is correct, no need to use pointers here.

          But to answer your question, to copy your QStringlist you'll have to do either do a "deep copy" or copy each string and append it to your new list.

          I would recommend the later one.

          QStringList customCompList;
          for( QString s : completerList)
                 customCompList.append(s);
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • SikarjanS Offline
            SikarjanS Offline
            Sikarjan
            wrote on last edited by Sikarjan
            #5

            Okay, this was quite a mess and a lot of try and error. Now I am not using pointers but the program crashes on the last line. "Segmentation error". The customCompList looks okay. The old and new values are there. Can I not do this:

            QStringList customCompList = completerList;
            

            And then append to it via <<? Will this mess up the list in the memory?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              If you pass your QStringList around using const references then no there won't be copies done and if you need really need to pass a pointer from time to time for a good reason, then just pass the address of your QStringList to that function.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • SikarjanS Offline
                SikarjanS Offline
                Sikarjan
                wrote on last edited by
                #7

                Hi,

                I am new to the debugging tool. Here is what I get when I try to qDebug the list:

                    for(QString s: customCompList)
                        qDebug() << s;
                
                		__for_begin	@0x28c1b8	QList<QString>::iterator
                		__for_end	@0x28c1b4	QList<QString>::iterator
                			i	@0x28c1b4	QList<QString>::Node
                				v	0x123eb08 <vtable for QTextDocument+8>	void*
                		__for_range	<2818 Elemente>	QStringList &
                			[0]	"$GLOBALS"	QString
                			[...]
                			[2817]	"zlib_get_coding_type"	QString
                		customCompList	<2819 Elemente>	QStringList
                
                

                Why is the for range with 2818 elements one element short? The missing element is the one I added in the function.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Where exactly are you calling that ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • SikarjanS Offline
                    SikarjanS Offline
                    Sikarjan
                    wrote on last edited by
                    #9

                    I replaced the setStringList with the for loop. The output is displayed in the Qt Debug window (the right one with all the variables).

                    1 Reply Last reply
                    0
                    • SikarjanS Offline
                      SikarjanS Offline
                      Sikarjan
                      wrote on last edited by
                      #10

                      I got it. I needed to add

                      completerModel = new QStringListModel();
                      

                      Prior setting the string list.

                      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