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