Results 1 to 6 of 6

Thread: QListWidget, access to index and item after drag and drop event

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation QListWidget, access to index and item after drag and drop event

    Hi,

    I have a problem. I don't know how to get the access to item and it index after drag and drop event. I created a connection between signal and slot but item data are invalid. To be specific in this function: rowsInserted(QModelIndex,int,int) first variable is invalid. How to get access to valid data?
    Qt Code:
    1. connect(ui->lwSunday->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(ItemInserted(QModelIndex,int,int)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::ItemInserted(const QModelIndex &parent, int first, int /*last*/)
    2. {
    3. QString text = parent->data(Qt::DisplayRole).toString(); // text is "" why?
    4. }
    To copy to clipboard, switch view to plain text mode 

    Can you help me?

    P.S. Sorry for my poor language skills.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, access to index and item after drag and drop event

    As the arguments name suggests, the model index in this context is the parent of the inserted rows.

    Since you are working with a list view, there is no such thing as a parent, all rows are top level rows.

    The only arguments of interest to you are the row indexes describing the range of new rows.
    See QListWidget::itemAt(int)

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListWidget, access to index and item after drag and drop event

    Thanks for your answer. But still I don't know how to get data from inserted item. Function QListWidget::itemAt(int, int) can only take data from items which was inserted before. Is there any valid solution?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, access to index and item after drag and drop event

    Not item(int, int), item(int)

    Check the documentation, those two have a very different semantic

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListWidget, access to index and item after drag and drop event

    Function QListWidget::item(int) won't work too because inserted item is filled with data after sinal rowsInserted. When I take data when signal is emitted inserted item is empty(yes no data, just empty item).
    I solved this problem but in some awful way. I created additional variables and when items are dragged and dropped first is emitted signal rowsInserted(I store a index as source of data), then item is added to list, next signal emitted id rowsRemoved(I add data now and remove unnecessary item). Awful isn't it? But it works!

    UPDATE:
    Well there is one drawback. When user hold CTRL when dragging(copy action) there is no rowsRemoved signal emitted
    Well i will fix this problem someday :P
    Last edited by FarAway; 7th July 2015 at 16:30.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, access to index and item after drag and drop event

    Well, if you want to call the model's data method, you can ask if for a QModelindex for any row you want.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 30th January 2014, 07:46
  2. Replies: 4
    Last Post: 25th January 2013, 11:14
  3. to use drag & drop on QListWidget
    By giorgik in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2012, 19:00
  4. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 05:29
  5. Replies: 3
    Last Post: 10th June 2010, 16:13

Tags for this Thread

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.