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. How delete list of actions or 1 among this actions
QtWS25 Last Chance

How delete list of actions or 1 among this actions

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.3k Views
  • 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.
  • R Offline
    R Offline
    Ruzik
    wrote on last edited by
    #1

    I have a lis of actions
    @QList<QAction *> actions = menuRecent_projects->actions();@
    How i can delete List or 1 among this actions
    I tryed so:

    @
    for(int a=0;a<=actions.count();a++)
    delete actions[a];
    @

    And so

    @
    delete actions[];
    @

    To delete all list

    [EDIT: code formatting, Volker]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      It depends, how they are created.
      Doesn't it work?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ruzik
        wrote on last edited by
        #3

        QMenu created in the Disigner, QActions created in the program
        QActions created in this string
        menuRecent_projects->addAction(settings.value("lastProjects/" + QString::number(a)).toString());

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Problem of your for loop is that this list is "live", i.e. if you remove one action, it is automatically deleted out of the list of the actions, thus the list is shortened by one. You can overcome this by counting backwards, as you then only remove from the end and the yet unvisited indices remain stable (supposed you do not have actions inserted twice or more often).

          You might want to use "QMutableListIterator":http://doc.qt.nokia.com/4.7/qmutablelistiterator.html if you want to iterate over the list and want to manipulate it in the meantime.

          For deleting all actions, just call "qDeleteAll() ":http://doc.qt.nokia.com/4.7/qtalgorithms.html#qDeleteAll-2 on the list.

          You must not use your second attempt! delete [] is only allowed for deleting arrays created with new []!

          Oh, and a last hint: the list indices are 0-based, so your for loops should look like this:

          @
          for(int i = 0; i < list.size(); ++i)
          @

          You do "i <= list.size()", which gives you an off by one error (segfault or list assertion).

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ruzik
            wrote on last edited by
            #5

            Many thanks for your help!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Please note, that Volkers warning that items should only be in the list once if you start to delete them, also goes if you use qDeleteAll()! This will result in -a crash- undefined behaviour:

              @
              //DON'T DO THIS!!!
              QList<QAction*> actionList;
              actionList << new QAction();
              actionList << actionList.at(0); //re-insert the same item twice

              ...
              qDeleteAll(actionList); // BOOM!
              @

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Ruzik
                wrote on last edited by
                #7

                Many thanks for you advance!

                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