Results 1 to 6 of 6

Thread: Adding QStringList to QListView

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Adding QStringList to QListView

    Hello!

    I've listed some directories to QStringList. Now I want to put that list into QListView. How can I do it? My code:

    Qt Code:
    1. void MainWindow::listMyDirs()
    2. {
    3. QDir myDir("/home/");
    4. QStringList list = myDir.entryList (QDir::Dirs|QDir::NoDotAndDotDot, QDir::Name);
    5.  
    6. // ui->listaCzujnikow->setModel() or smething??
    7. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding QStringList to QListView

    See QStringListModel.

    EDIT: Ehm, better use QFileSystemModel direct!

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStringList to QListView

    Quote Originally Posted by Lykurg View Post
    How can I use it here? Some code would be very helpful.

    I've tried:

    Qt Code:
    1. QDir myDir("/home/");
    2.  
    3. QStringListModel list(myDir.entryList (QDir::Dirs|QDir::NoDotAndDotDot, QDir::Name));
    4.  
    5. ui->myList->setModel(&list);
    To copy to clipboard, switch view to plain text mode 

    but it doesn't work. It's compiling but list is empty...

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 30th September 2010 at 20:15.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding QStringList to QListView

    Old same c++ weakness... Create it on the heap.

  5. The following user says thank you to Lykurg for this useful post:

    Tomasz (30th September 2010)

  6. #5
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStringList to QListView

    It works! Thanks! But why it's necessary to create that object on heap to make it work?

    thanks in advance
    best regards
    Tomasz

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding QStringList to QListView

    Because otherwise your model gets deleted at the scope's end. So when your view is displayed the model does not exist any more. And without a model, there is nothing to display.

Similar Threads

  1. QListView - adding items
    By creep33 in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 17:30
  2. Adding QPixmaps to QListView
    By ada10 in forum Newbie
    Replies: 3
    Last Post: 16th August 2010, 12:15
  3. Adding validation to a QListView's edit mode
    By MattT in forum Qt Programming
    Replies: 10
    Last Post: 1st August 2010, 01:53
  4. Adding elements to a QListView
    By Arreis in forum Newbie
    Replies: 4
    Last Post: 30th June 2009, 06:56
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.