Reordering ListWidgetItem Items.



  • Hello,

    I'm a beginner at qt.
    I’m doing a kind of ranking using QListWidget, I want to be able to reorder some labels. I have used QListWidget as my list and added to it the QListWidgetItem and Qlabel. I enabled the drag and drop property for it. I'm able to reorder the list but I can not get the reordering result.
    Each time after reordering when I read the QListWidget I get the initial order. I do not know how can I get the reordering result after the user reorders these labels, and is it possible even if I do it in this simple way.

    How can I get the reordering information form GUI.

    Thank you.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean get a string list with the current content of the QListWidget ?



  • Hey,

    Yes I want to get the Qlabel text each time I do reordering.
    I want to check the result of the reordering by checking the text on each label, which should be changed each time.
    After the label reordering I check the list item again, but the problem is I can only get the initial order.

    This is the code:
    @

    for (int i = 0 ; i< list->count(); i++){

    QListWidgetItem* item =  dynamic_cast <QListWidgetItem*>(list->item(i));
    QLabel* label = dynamic_cast<QLabel*> (list->itemWidget(item));
    
    std::cout<<"The text after reordering :"<<label->text().toStdString()<<std::endl;
    

    }

    @

    Thank you,


  • Lifetime Qt Champion

    There's something strange here, why are you casting to QLabel ?



  • Because I want to read the text from that label,
    I don't know if I did it in a wrong way, I'm a beginner so I do not know if it should be done in different way.

    It will be nice if you can help me.
    Thank you in advance.


  • Lifetime Qt Champion

    QListWidget doesn't create QLabel for its items. You can get the text directly from the item.



  • okay I deleted the Qlabel.
    But the problem is still there. I'm wondering why each after I do reordering, and I click proceed the Qlistwidget has the initial ordering, and it does not update its items according to the new reordering from the GUI.

    @
    void Gui::qtReactOnProceed(){

    for (int i = 0 ; i< list->count(); i++){
    QListWidgetItem* item = dynamic_cast <QListWidgetItem*>(list->item(i));
    std::cout<<"The row:"<<item->text().toStdString()<<std::endl;
    }
    }
    @

    Actually I do not know how the DragandDropMode updates the Qlistwidget, is it done by default.

    Thank you again.


  • Lifetime Qt Champion

    How are you doing the reordering ?



  • I’m reordering By using the drag and drop mode for the listwidget.
    I have created the listwidget then enabled the drag and drop mode for it.

    After the user do reordering he clicks a button so in that action I’m reading the list again to check its items "new reorder for the items".

    Is that the right way.

    Does the Gui refresh itself, or I have to add the list again to Gui each time I do reordering.

    I hop you a clear get an idea about what I trying to do.
    Thank you again.


  • Lifetime Qt Champion

    Can you show the code where you setup the drag and drop behavior ?



  • Hello,

    Thank you,
    I could solve the problem, it was not in the qlistwidget, actually the qlistwidget with dragnaddrop mode works fine.

    But the problem was in calling the function, it was called many times and in that case more than one list are created, and added on the top of each other, accordingly I had incorrect result from the list.


  • Lifetime Qt Champion

    You're welcome !

    Nice you found out and thanks for sharing.

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

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