Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How delete list of actions or 1 among this actions

    General and Desktop
    4
    7
    3792
    Loading More Posts
    • 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
      Ruzik last edited by

      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 Reply Quote 0
      • G
        giesbert last edited by

        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 Reply Quote 0
        • R
          Ruzik last edited by

          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 Reply Quote 0
          • G
            goetz last edited by

            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 Reply Quote 0
            • R
              Ruzik last edited by

              Many thanks for your help!

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                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 Reply Quote 0
                • R
                  Ruzik last edited by

                  Many thanks for you advance!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post