Solved Copy QStringList pointer to QStringList
-
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!
-
Hi,
The first question is: why are you using pointers to QStringList ?
There's no need for that in your use case.
-
Hi,
the completerList is used in different codeEditors. If it is not a pointer it will get copied over and over, right?
Best
-
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);
-
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?
-
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.
-
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.
-
Where exactly are you calling that ?
-
I replaced the setStringList with the for loop. The output is displayed in the Qt Debug window (the right one with all the variables).
-
I got it. I needed to add
completerModel = new QStringListModel();
Prior setting the string list.