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 ;

    op1;op2;op3

    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.

    KR


  • Lifetime Qt Champion

    Hi,

    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
    ui->yourList->clear();
    for (int i=0;i<NUM_OF_ITEMS;i++){
    ui->youList->addItem("Sample"+QString::number(i));
    }
    


  • 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

    KR


  • Moderators

    @iyustlop Did you consider what @VRonin suggested?



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

    myListwidget->addItems(myStringList);
    

    link

    I still fail to see the problem .



  • Thank you all,

    I'll consider change to ListView.



  • @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);
    if(mystringList.contains(currIdx.data().toString()))
    selectedItems.select(currIdx,currIdx);
    }
    myList->selectionModel()->select(selectedItems,QItemSelectionModel::ClearAndSelect);
    

    works for both tableview and tablewidget


Log in to reply
 

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