[Qt-interest] QStringListModel setStringList usage

Samuel Gaist samuel.gaist at edeltech.ch
Wed Dec 22 10:17:30 CET 2010


Sorry, sent  private first...
Hi,
On 22 déc. 2010, at 10:02, Francisco Gonzalez wrote:

> #include <QCoreApplication>
> #include <QStringListModel>
> #include <QDebug>
> 
> class Test
> {
> private:
>   QStringListModel *myModel;
>   QStringList myList;
> 
> public:
>   void check()
>   {
>       qDebug() << myModel->stringList().size();
>       qDebug() << myModel->stringList();
>   };
> 
> public:
>   Test()
>   {
> 
>       myList << "One" << "Two" << "Three" ;
> 
**** The following line is the problem ****
>       QStringListModel *myModel = new QStringListModel();
You are shadowing your class variable with this one
Your program should segfault when calling check()
And you have a memory leak (but this has nothing to do with your problem)
>       myModel->setStringList(myList);
> 
>       qDebug() << myModel->stringList().size();
>       qDebug() << myModel->stringList();
> 
> 
> 
>   };
>   ~Test()
>   {
You should delete your model here (to avoid the memory leak)
>   };
> };
> 
> int main(int argc, char *argv[])
> {
>   QCoreApplication a(argc, argv);
>   Test t;
>   t.check();
> 
>   return 0;
> }

Hope this helps
Samuel


More information about the Qt-interest-old mailing list