Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Spli QString and show different result in a QListWidget
Forum Updated to NodeBB v4.3 + New Features

Spli QString and show different result in a QListWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iyustlop
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3

        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));
        }
        

        Mandruk1331

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • I Offline
            I Offline
            iyustlop
            wrote on last edited by
            #5

            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

            jsulmJ J.HilkJ VRoninV 3 Replies Last reply
            0
            • I iyustlop

              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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @iyustlop Did you consider what @VRonin suggested?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I iyustlop

                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

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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 .


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  iyustlop
                  wrote on last edited by
                  #8

                  Thank you all,

                  I'll consider change to ListView.

                  1 Reply Last reply
                  0
                  • I iyustlop

                    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

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    @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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved