QListWidget dragDropMode = InternalMove except the 2 first



  • Hello every one,

    In a QListWidget I've set dragDropMode = InternalMove so that the user can reorder elements in list.
    This works well however I would like to prevent the first 2 elements to be moved, they must always stay the two first, we cannot move them and we cannot place any element before/between them, reordering is allowed only within range (3, ... infinite)

    A solution would be to subscribe to the @dropEvent ( QDropEvent * event )@ and cancel the drag&drop action if it's not allowed but I don't know if this is possible. Something like event->acceptThisDrop(false) would be perfect haha!

    Something even more userfriendly would be to make it impossible to start any drag action with the two first elements nor terminate any drop action within the 1st and 2nd position.

    Any idea to do this as simple as possible?
    Thanks



  • Hi,

    I would use 2 QListWidgets and put those 2 items to one of them with drag-and-drop disabled.


  • Lifetime Qt Champion

    Hi,

    What about setting the item flags of the two first elements to Qt::NoItemFlags ?



  • Hi all and thanks :)

    I forgot to mention that one element at a time can be selected in this list, including the two first. I must forbid only drag&drop-ing these two but they must be clickable.

    @ckakman well, that would be a bit annoying to synchronize both list since the user can select one element

    @SGaist your solution disables the item completely, I cannot click them anymore and they are grey.


  • Lifetime Qt Champion

    Then use the appropriate combination like

    @Qt::ItemIsSelectable |Qt::ItemIsEnabled@



  • @item->setFlags( ItemIsEnabled | ItemIsSelectable | ItemIsUserCheckable );@ is nearly what I want.
    However, I can still drop other items at the place of the first two.


  • Lifetime Qt Champion

    Then maybe reimplement the drop event to "freeze" the portion of the widget where they reside



  • [quote author="myoan" date="1421401499"]Hi all and thanks :)

    I forgot to mention that one element at a time can be selected in this list, including the two first. I must forbid only drag&drop-ing these two but they must be clickable.

    ckakman well, that would be a bit annoying to synchronize both list since the user can select one element

    [/quote]

    I'd say just deselect items in one list when an item in the other list is selected. In a slot connected to itemActivated():
    @
    foreach( QListWidgetItem *item, ui->otherList->selectedItems() )
    item->setSelected(false);
    @

    Repeat this for the other table as well. 4 lines of code in total in 2 slots plus connecting the signals.



  • Hmmm thanks. I'll see what is easier to implement.

    By the way, how could I detect a "drag&drop" operation?
    I can't find any working signal warning me about this.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.