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. Range-based for loop for a QList
Forum Updated to NodeBB v4.3 + New Features

Range-based for loop for a QList

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 1.8k Views 2 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.
  • tomyT tomy

    Hello all,

    Please take a look at my project.

    ListAdd.h:

    #ifndef LISTADD_H
    #define LISTADD_H
    
    #include <QDialog>
    #include <QAbstractButton>
    
    class QPushButton;
    class QListWidget;
    class QComboBox;
    class QDialogButtonBox;
    
    class ListAdd : public QDialog
    {
        Q_OBJECT
    
    public:
        ListAdd(QWidget *parent = nullptr);
    
    private slots:
        void addData();
        void save();
    
    private:
        void load();
        void btnClicked(QAbstractButton*);
    
        QComboBox* combo = nullptr;
        QPushButton* add = nullptr;
        QListWidget* list = nullptr;
        QDialogButtonBox* dBtBox = nullptr;
    };
    #endif // LISTADD_H
    

    ListAdd.cpp:

    #include "listadd.h"
    #include <QListWidget>
    #include <QComboBox>
    #include <QPushButton>
    #include <QDialogButtonBox>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QSettings>
    #include <QMessageBox>
    #include <QVariant>
    
    ListAdd::ListAdd(QWidget *parent)
        : QDialog(parent)
    {
        combo = new QComboBox;
        combo->setEditable(true);
        add = new QPushButton(tr("&Add"));
        list = new QListWidget;
        list->setSelectionMode(QAbstractItemView::ExtendedSelection);
    
        dBtBox = new QDialogButtonBox(QDialogButtonBox::Ok |
                                      QDialogButtonBox::Cancel);
        dBtBox->setEnabled(true);
        dBtBox->addButton("Clear", QDialogButtonBox::ButtonRole::ActionRole);
        dBtBox->addButton("Remove", QDialogButtonBox::ButtonRole::ActionRole);
    
        QHBoxLayout* topLayout = new QHBoxLayout;
        topLayout->addWidget(combo);
        topLayout->addWidget(add);
    
        QVBoxLayout* mainLayout = new QVBoxLayout;
        mainLayout->addLayout(topLayout);
        mainLayout->addWidget(list);
        mainLayout->addWidget(dBtBox);
    
        setLayout(mainLayout);
    
        connect(add, &QPushButton::clicked, this, &ListAdd::addData);
        connect(dBtBox, &QDialogButtonBox::clicked, this, &ListAdd::btnClicked);
        load();
    }
    
    //*******************************************
    
    void ListAdd::addData()
    {
        if(combo->findText(combo->currentText()) < 0)
            combo->addItem(combo->currentText());
    
        list->addItem(combo->currentText());
    }
    
    //*************************************************************
    
    void ListAdd::save()
    {
        QSettings settings("Me", "CS Company");
        settings.clear();
    
        settings.setValue("text",combo->currentText());
    
        settings.beginWriteArray("combo");
        for (int i=0; i<combo->count();i++)
        {
           settings.setArrayIndex(i);
           settings.setValue("item", combo->itemText(i));
        }
        settings.endArray();
    
        settings.beginWriteArray("list");
        for (int i=0; i<list->count();i++)
        {
           settings.setArrayIndex(i);
           settings.setValue("item", list->item(i)->text());
        }
        settings.endArray();
    }
    
    //********************************************************
    
    void ListAdd::load()
    {
     QSettings settings("Me", "CS Company");
    
     combo->setCurrentText(settings.value("text", "").toString());
    
     int comboSize = settings.beginReadArray("combo");
     for (int i=0; i<comboSize;i++)
     {
        settings.setArrayIndex(i);
        combo->addItem(settings.value("item", "").toString());
     }
     settings.endArray();
    
     int listSize = settings.beginReadArray("list");
     for (int i=0; i<listSize;i++)
     {
        settings.setArrayIndex(i);
        list->addItem(settings.value("item", "").toString());
     }
     settings.endArray();
    }
    
    //********************************************************************
    
    void ListAdd::btnClicked(QAbstractButton* button)
    {
        if(button->text().contains("OK"))
        {
            save();
            QMessageBox::information(this, "Saved", "Your data is saved! Please re-open the application");
        }
    
        else if(button->text().contains("Cancel"))
        {
            QMessageBox::information(this, "Quit", "Your data is being lost");
            accept();
        }
    
        else if(button->text().contains("Clear"))
        {
            list->clear();
            combo->clear();
        }
    
        else if(button->text().contains("Remove")) {
            QList<QListWidgetItem*> items = list->selectedItems();
    
            for(auto& item : items)
            {
                list->removeItemWidget(item);
                delete item;
            }
        }
    }
    

    The question is about the else-if condition above, which is, why without delete item;, won't the item be removed from the list, please?

    A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #6

    @tomy said in Range-based for loop for a QList:

        for(auto& item : items)
        {
            list->removeItemWidget(item);
            delete item;
        }
    

    Not sure it's safe to delete an item while looping over the list
    Safer to do:

            for(auto& item : items)
            {
                list->removeItemWidget(item);
            }
            qDeletAll(items);
    
    Christian EhrlicherC 1 Reply Last reply
    2
    • tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by tomy
      #7

      I hope I get answers for the last two questions I asked, "entirely" and "destructor". Should I make separate threads for them?

      @Bonnie
      Well, there's no widget set by setItemWidget; I used selectionItems. So you mean that function neither removes any item in the list nor is it needed in the code?

      @Asperamanca

      It may make sense not to delete the cells of the list while you're still connected to it, but except for that is there any rationale?
      So if I use this for that last condition, it's the easiest and best method!

      else if(button->text().contains("Remove")) {
              qDeleteAll(list->selectedItems());
      
      B 1 Reply Last reply
      0
      • tomyT tomy

        I hope I get answers for the last two questions I asked, "entirely" and "destructor". Should I make separate threads for them?

        @Bonnie
        Well, there's no widget set by setItemWidget; I used selectionItems. So you mean that function neither removes any item in the list nor is it needed in the code?

        @Asperamanca

        It may make sense not to delete the cells of the list while you're still connected to it, but except for that is there any rationale?
        So if I use this for that last condition, it's the easiest and best method!

        else if(button->text().contains("Remove")) {
                qDeleteAll(list->selectedItems());
        
        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #8

        @tomy Yes, if you open qlistwidget.h, you can see that:

        inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
        { setItemWidget(aItem, nullptr); }
        
        tomyT 1 Reply Last reply
        1
        • B Bonnie

          @tomy Yes, if you open qlistwidget.h, you can see that:

          inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
          { setItemWidget(aItem, nullptr); }
          
          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #9

          @Bonnie

          I took a look twice on the task that removeItemWidget does, but couldn't understand this, since the setItemWidget looks complicated.
          Will you explain what that remove function does in my code, please. This way it becomes clearer I suppose.

          Christian EhrlicherC B 2 Replies Last reply
          0
          • A Asperamanca

            @tomy said in Range-based for loop for a QList:

                for(auto& item : items)
                {
                    list->removeItemWidget(item);
                    delete item;
                }
            

            Not sure it's safe to delete an item while looping over the list
            Safer to do:

                    for(auto& item : items)
                    {
                        list->removeItemWidget(item);
                    }
                    qDeletAll(items);
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @Asperamanca said in Range-based for loop for a QList:

            Safer to do:

            In this case it's not needed - it just adds another loop for nothing.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply
            1
            • tomyT tomy

              @Bonnie

              I took a look twice on the task that removeItemWidget does, but couldn't understand this, since the setItemWidget looks complicated.
              Will you explain what that remove function does in my code, please. This way it becomes clearer I suppose.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @tomy said in Range-based for loop for a QList:

              Will you explain what that remove function does in my code, please.

              It removes the widget from the view, nothing more (and exactly what the function name states...)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              tomyT A 2 Replies Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @tomy said in Range-based for loop for a QList:

                Will you explain what that remove function does in my code, please.

                It removes the widget from the view, nothing more (and exactly what the function name states...)

                tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by
                #12

                @Christian-Ehrlicher

                By "from the view" you mean "disappearing" right? But it does nothing to be seen.

                1 Reply Last reply
                0
                • tomyT tomy

                  @Bonnie

                  I took a look twice on the task that removeItemWidget does, but couldn't understand this, since the setItemWidget looks complicated.
                  Will you explain what that remove function does in my code, please. This way it becomes clearer I suppose.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #13

                  @tomy As I said, if you haven't called setItemWidget to set a widget, then it does nothing at all.
                  [ADDED]
                  And setItemWidget is not that complicated.
                  You can use it to set a widget to a item, maybe a button, a combox, or any kind of widget.
                  When the item is visible in the list view, the widget will be shown on the item rect.
                  So by removeItemWidget, you can remove that widget.

                  tomyT 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @tomy said in Range-based for loop for a QList:

                    Will you explain what that remove function does in my code, please.

                    It removes the widget from the view, nothing more (and exactly what the function name states...)

                    A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #14
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Asperamanca said in Range-based for loop for a QList:

                      Safer to do:

                      In this case it's not needed - it just adds another loop for nothing.

                      A Offline
                      A Offline
                      Asperamanca
                      wrote on last edited by Asperamanca
                      #15
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • B Bonnie

                        @tomy As I said, if you haven't called setItemWidget to set a widget, then it does nothing at all.
                        [ADDED]
                        And setItemWidget is not that complicated.
                        You can use it to set a widget to a item, maybe a button, a combox, or any kind of widget.
                        When the item is visible in the list view, the widget will be shown on the item rect.
                        So by removeItemWidget, you can remove that widget.

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by tomy
                        #16

                        @Bonnie

                        I've used "addItem" for both the comboBox and listWidget to add items thereby they are visible, but removeItem is for this usage apparently and deleting the selected items will do the job.
                        Thanks.

                        1 Reply Last reply
                        0

                        • Login

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