Results 1 to 11 of 11

Thread: Problem with QListWidget

  1. #1
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Problem with QListWidget

    Hello everybody!

    I've a problem since a while now with QListWidget. I'll try to explain it.

    I've a QListWidget containing a widget. I've done this with the setItemWidget() method. But when I move an item of the list (by drag'n'drop or by button), his widget disappear. I don't know why. Let me illustrate it:







    Someody have an idea?

    Here's a little bit of code:

    Qt Code:
    1.  
    2. QWidget *w = new QWidget;
    3. QLabel *labelGroup = new QLabel("Label");
    4. QLabel *labelTime = new QLabel("14:00");
    5. QListWidget *listWidgetGroup = new QListWidget;
    6. listWidgetGroup->addItem("Groupe");
    7. QVBoxLayout *vLayout = new QVBoxLayout;
    8. vLayout->addWidget(labelGroup);
    9. vLayout->addWidget(labelTime);
    10. vLayout->addWidget(listWidgetGroup);
    11.  
    12. w->setLayout(vLayout);
    13. w->adjustSize();
    14.  
    15. item->setSizeHint(w->size());
    16. ui.listWidget->addItem(item);
    17. ui.listWidget->setItemWidget(item, w);
    To copy to clipboard, switch view to plain text mode 

    I think i'm not using the right widget, but I don't know wich to use...

    Thank you in advance for your precious help!

    Elwe

    PS: I'm working with Qt 4.5.1 and WindowsXP (but I've the same problem on Linux).
    Last edited by Elwe; 15th June 2009 at 13:31.

  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: Problem with QListWidget

    Quote Originally Posted by Elwe View Post
    But when I move an item of the list (by drag'n'drop or by button), his widget disappear. I don't know why.
    Please post that code of the button and drag'n'drop. There must be the error.

  3. #3
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    Here's the button's code:

    Qt Code:
    1. void DragNDrop::upSlot(void) {
    2. int row = ui.listWidget->currentRow();
    3.  
    4. if(!(0 == row)) {
    5. QListWidgetItem *item = ui.listWidget->takeItem(row);
    6.  
    7. row--;
    8.  
    9. ui.listWidget->insertItem(row, item);
    10. ui.listWidget->setCurrentItem(item);
    11. ui.listWidget->selectionModel()->select(ui.listWidget->currentIndex(), QItemSelectionModel::Select);
    12. }
    13. }
    14.  
    15.  
    16.  
    17. void DragNDrop::downSlot(void) {
    18. int row = ui.listWidget->currentRow();
    19.  
    20. if(!(ui.listWidget->count() - 1 == row)) {
    21. QListWidgetItem *item = ui.listWidget->takeItem(row);
    22.  
    23. row++;
    24.  
    25. ui.listWidget->insertItem(row, item);
    26. ui.listWidget->setCurrentItem(item);
    27. ui.listWidget->selectionModel()->select(ui.listWidget->currentIndex(), QItemSelectionModel::Select);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    For the drag'n'drop, I've set it by the QtDesigner (activating acceptDrop, dragEnable and dragMode to InternalMove).

  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: Problem with QListWidget

    Qt Code:
    1. ui.listWidget->insertItem(row, item);
    To copy to clipboard, switch view to plain text mode 

    This will delete the original one! You have to swap the values! Like (not optimized):
    Qt Code:
    1. Take item a in a
    2. Take item b in b
    3. insert a in the row of b
    4. insert b in the row of a
    To copy to clipboard, switch view to plain text mode 

    if you call
    Qt Code:
    1. ui.listWidget->setCurrentItem(item);
    To copy to clipboard, switch view to plain text mode 
    then
    Qt Code:
    1. ui.listWidget->selectionModel()->select(ui.listWidget->currentIndex(), QItemSelectionModel::Select);
    To copy to clipboard, switch view to plain text mode 
    is not necessary.

  5. #5
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    This:
    Qt Code:
    1. ui.listWidget->insertItem(row, item);
    To copy to clipboard, switch view to plain text mode 
    Doesn't delete the original one (look at the picture, the item in second position is in third after moving up the third item).

    However, I've try your suggestion. But the problem is still there. The items widgets aren't repaint. They just disappear.

  6. #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: Problem with QListWidget

    Ok,ok, sorry, after further investigations I see clearer: The widget one is setting for the item is not stored inside them. So moving an item via your button or per drag and drop "destroys" the relation between the item and the widget. This could be a bug or intended by Qt. Don't know.

    But If you have the same arrangement of widget for each row you really should reconsider using a QListView with a custom delegate, where you can draw the content yourself. That isn't so hard. Only the list inside could be tricky.

    May be also a QScrollArea with a simple box layout could be a solution for you. Then you have to code the movement of the elements (inside the box layout) on yourowne.

  7. #7
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    Ok I see. In this case, I've another question;

    The labels and QListWidgets insides the widgets are dynamic. I mean they depend on values in a database and they could change with some events. Is the custom delegate and a QListView a good choice?

  8. #8
    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: Problem with QListWidget

    Quote Originally Posted by Elwe View Post
    The labels and QListWidgets insides the widgets are dynamic. I mean they depend on values in a database and they could change with some events. Is the custom delegate and a QListView a good choice?
    Definitely yes.
    ...but if you want to use "QListWidgets" inside "QListWidgets" then the QScrollArea could be easier to realize. Because I don't know spontaneous how to imitate a list view with a QPainter...

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

    Elwe (17th June 2009)

  10. #9
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    Thanks a lot for your responses! I'll try this and if I have something good, I'll post here the solution.

    Thank you.

  11. #10
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    I've forgot something. I've to insert or remove items. So I'm not sure that a view is the best choice. What do you think?

  12. #11
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    So, finally I've do my first idea. Here it's the code, it could help someone one day!

    Qt Code:
    1. void DragNDropQListWidget::startDrag(Qt::DropActions) {
    2. int row = currentRow();
    3.  
    4. QByteArray itemData;
    5. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    6.  
    7. //We put the current row into datastream
    8. dataStream << row;
    9.  
    10. QMimeData *mimeData = new QMimeData;
    11. mimeData->setData("application/x-dndgroup", itemData);
    12.  
    13. QDrag *drag = new QDrag(this);
    14. drag->setMimeData(mimeData);
    15.  
    16. drag->exec(Qt::MoveAction);
    17. }
    18.  
    19. void DragNDropQListWidget::dropEvent(QDropEvent *event) {
    20. if (event->mimeData()->hasFormat("application/x-dndgroup")) {
    21. QListWidgetItem *itemToDrag;
    22. QByteArray itemData = event->mimeData()->data("application/x-dndgroup");
    23. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    24. int previousRow;
    25.  
    26. //Will stock the target destination row
    27. int currentDropRow = row(itemAt(event->pos()));
    28.  
    29. //We take the source row
    30. dataStream >> previousRow;
    31.  
    32. //Beforhand, we've stock the itemWidget into a QList of QWidget*
    33. QVBoxLayout *vLayoutA = refWidget->at(previousRow);
    34. QWidget *wA = new QWidget;
    35. wA->setLayout(vLayoutA);
    36.  
    37.  
    38. QVBoxLayout *vLayoutTemp = refWidget->takeAt(previousRow);
    39. refWidget->insert(currentDropRow, vLayoutTemp);
    40.  
    41. //We "remove" the item of the list
    42. itemToDrag = takeItem(previousRow);
    43. //Important, we have to remove the item widget, otherwise it will be blank widget.
    44. removeItemWidget(itemToDrag);
    45. //We insert the removed item into the destination
    46. insertItem(currentDropRow, itemToDrag);
    47. //And we reset the item widget.
    48. setItemWidget(itemToDrag, wA);
    49.  
    50. setCurrentItem(itemToDrag);
    51.  
    52. if (event->source() == this) {
    53. event->setDropAction(Qt::MoveAction);
    54. event->accept();
    55. } else {
    56. event->ignore();
    57. }
    58. } else {
    59. event->ignore();
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 

    For buttons who up and down items, use the same idea (save the widget created into a QList).

  13. The following user says thank you to Elwe for this useful post:

    nabroyan (2nd November 2017)

Similar Threads

  1. Problem with QListWidget and paintEvent()
    By joksi in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2009, 10:50
  2. QListWidget problem [solved]
    By satoshi in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2009, 18:58
  3. Replies: 1
    Last Post: 23rd April 2009, 09:05
  4. Replies: 19
    Last Post: 3rd April 2009, 23:17
  5. QListWidget - stylesheet problem
    By febil in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2009, 16:44

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.