Results 1 to 5 of 5

Thread: Move multiple selected items from Qtreeview to QListBox

  1. #1
    Join Date
    Nov 2012
    Posts
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60
    Thanks
    9

    Default Move multiple selected items from Qtreeview to QListBox

    I want to move selected items from QTreeview (populated by QFileSystemModel) to a QListBox. I have managed to get all selected items in a QMdelIndex list, but I cant add this to QListbox since QListBox->addItems(QStringList) only accepts QStringList.
    Please guide me through.

    QItemSelectionModel selectiveModel=ui->treeView->selectionModel();
    QModelIndexList list=selectiveModel.selectedIndexes();
    ui->inputImageLstBox->addItems(list);

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Move multiple selected items from Qtreeview to QListBox

    Qt Code:
    1. QTreeView * treeView;
    2. QListWidget * listBox;
    3.  
    4. QModelIndexList list = treeView->selectionModel()->selectedIndexes();
    5. QStringList labels;
    6. foreach(QModelIndex index, list)
    7. labels << index.data().toString();
    8.  
    9. listBox->addItems(labels);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    sargeslash (31st January 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60
    Thanks
    9

    Default Re: Move multiple selected items from Qtreeview to QListBox

    I tried the code, is here any possibility to add any spfic column to the listbox?
    As right now I when I am adding all the coulmns of the QTreeView are added (i.e. column 0,1,2...)
    Since column 1 only represents name so I only want to add this(column 1) to the Listbox.
    Output looks like this:Screenshot from 2013-01-31 15:56:01.png

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Move multiple selected items from Qtreeview to QListBox

    Yes, just check the column number of the index
    Qt Code:
    1. foreach(QModelIndex index, list)
    2. if(index.column() == 1)
    3. labels << index.data().toString();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    sargeslash (31st January 2013)

  7. #5
    Join Date
    Nov 2012
    Posts
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60
    Thanks
    9

    Default Re: Move multiple selected items from Qtreeview to QListBox

    You made my day
    Thats so awesome


    Added after 35 minutes:


    This was really nice help but now it causes some troubles in my application,
    Since before this I was doing this

    QModelIndex index = ui->treeView->currentIndex();
    QListWidgetItem *item;
    item = new QListWidgetItem;
    item->setData(Qt:isplayRole, model->fileName(index));
    item->setData(Qt::UserRole, model->filePath(index));

    and then saving the items by

    outputFile.write(ui->inputImageLstBox->model()->index(i, 0).data(Qt::UserRole).toString().toStdString().c_s tr());

    But now by using the above concept of selecting multiple files how can I use the filePath for all the selectedIndexes?
    Last edited by sargeslash; 31st January 2013 at 16:40.

Similar Threads

  1. Replies: 0
    Last Post: 1st July 2010, 05:21
  2. Replies: 2
    Last Post: 30th July 2009, 11:20
  3. undo for multiple selected items
    By mooreaa in forum Qt Programming
    Replies: 3
    Last Post: 13th July 2008, 15:13
  4. Move multi selected graphics items
    By tebessum in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 21:00
  5. Replies: 1
    Last Post: 15th March 2007, 21:45

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
  •  
Qt is a trademark of The Qt Company.