Spli QString and show different result in a QListWidget

  • Hi all,

    I need some help. I get some option from a QListWidget and I store this option in a QString separater by ;


    New I want to split this QString and display this options in the same QListwidget. If some could describe the method of how I shall do, please post it.

    Thank very much in advanced.


  • Lifetime Qt Champion


    Please take the time to look at QString's documentation. The split method looks like what you are looking for.

    Do you mean you want to replace the values in your QListWidget with new ones ?

  • If you want something like an updater for your QListWidget than just clear the list and add the new values to the list by using smth like this:
    void QListWidget::addItem(const QString & label) -- add the QString to the list
    void QListWidget::clear() - to clear the list
    So the code could be:

    #define NUM_OF_ITEMS 3
    for (int i=0;i<NUM_OF_ITEMS;i++){

  • Qt Champions 2018

    Instead of using QListWidget use QListView then call listView->setModel(new QStringListModel(mystring.split(";"),this));

  • Thank every one.

    The idea is mark as selected the values that are in the QStringList because these values are always the same.

    I have try with additems but it repeats the values in the bottom of the QlistWidget.

    Yesterday, reviewing the documentation I think that I have to use some iterator to select ne by one the elements of the QStringList.
    Has someone suffer this issue?

    Thank you very much in advanced


  • Qt Champions 2018

    @iyustlop Did you consider what @VRonin suggested?

  • Moderators

    @iyustlop Even so, what @VRonin suggested is the better approach over all, there is nothing easier than turning a QStringList into a QListwidget.



    I still fail to see the problem .

  • Thank you all,

    I'll consider change to ListView.

  • Qt Champions 2018

    @iyustlop said in Spli QString and show different result in a QListWidget:

    The idea is mark as selected the values that are in the QStringList

    This is a different problem altogether:

    const QStringList mystringList=mystring.split(';'); // depending how big the list is you might want to turn this in a QSet<QString>
    QItemSelection selectedItems;
    const int rowCount = myList->model()->rowCount();
    for(int i=0;i<rowCount ;++i){
    const QModelIndex currIdx=myList->model()->index(i,0);

    works for both tableview and tablewidget

Log in to reply