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. Creating add/delete buttons that modify a list
QtWS25 Last Chance

Creating add/delete buttons that modify a list

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 959 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.
  • P Offline
    P Offline
    Patrick_Li
    wrote on last edited by
    #1

    Screenshot (846).png

    I am currently creating a GUI with several lists (Radiologist ID, Presets, Nodules).
    I am trying to create add/remove buttons for each list that add a new item to the list, and remove a selected item from the list, respectively.
    After searching for some time, I haven't been able to find any way to do this.

    SGaistS Pl45m4P 2 Replies Last reply
    0
    • P Patrick_Li

      Screenshot (846).png

      I am currently creating a GUI with several lists (Radiologist ID, Presets, Nodules).
      I am trying to create add/remove buttons for each list that add a new item to the list, and remove a selected item from the list, respectively.
      After searching for some time, I haven't been able to find any way to do this.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which list are you referring to ?

      You seem to already have add and delete buttons for the three top elements which looks good.

      Are you talking about the annotations part ?

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

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Which list are you referring to ?

        You seem to already have add and delete buttons for the three top elements which looks good.

        Are you talking about the annotations part ?

        P Offline
        P Offline
        Patrick_Li
        wrote on last edited by
        #3

        @SGaist
        The add and delete buttons are non-functional. When you click on them, nothing happens.

        SGaistS 1 Reply Last reply
        0
        • P Patrick_Li

          @SGaist
          The add and delete buttons are non-functional. When you click on them, nothing happens.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That's normal, you have to code their functionality.

          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
          • P Patrick_Li

            Screenshot (846).png

            I am currently creating a GUI with several lists (Radiologist ID, Presets, Nodules).
            I am trying to create add/remove buttons for each list that add a new item to the list, and remove a selected item from the list, respectively.
            After searching for some time, I haven't been able to find any way to do this.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Patrick_Li said in Creating add/delete buttons that modify a list:

            After searching for some time, I haven't been able to find any way to do this.

            Dont know about your coding background, but it's not that hard.

            Connect the add button to

            • https://doc.qt.io/qt-6/qlistwidget.html#addItem

            either with lambda or write some code around it, to make it work (dialog or whatever prompt to enter new item text). Or even leave the text empty and edit after the item has been added.

            Connect the delete button to

            • https://doc.qt.io/qt-6/qlistwidget.html#takeItem

            and pick one or all items from

            • https://doc.qt.io/qt-6/qlistwidget.html#selectedItems

            to remove them from your list.

            Note, that you have to delete your QListWidgetItem manually after you have taken it from your list.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            P 1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @Patrick_Li said in Creating add/delete buttons that modify a list:

              After searching for some time, I haven't been able to find any way to do this.

              Dont know about your coding background, but it's not that hard.

              Connect the add button to

              • https://doc.qt.io/qt-6/qlistwidget.html#addItem

              either with lambda or write some code around it, to make it work (dialog or whatever prompt to enter new item text). Or even leave the text empty and edit after the item has been added.

              Connect the delete button to

              • https://doc.qt.io/qt-6/qlistwidget.html#takeItem

              and pick one or all items from

              • https://doc.qt.io/qt-6/qlistwidget.html#selectedItems

              to remove them from your list.

              Note, that you have to delete your QListWidgetItem manually after you have taken it from your list.

              P Offline
              P Offline
              Patrick_Li
              wrote on last edited by
              #6

              @Pl45m4
              I haven't been using any code for this, instead I've only been dragging-and-dropping widgets and such onto the UI. Is there a way to modify the code of the UI at this point?

              Pl45m4P 1 Reply Last reply
              0
              • P Patrick_Li

                @Pl45m4
                I haven't been using any code for this, instead I've only been dragging-and-dropping widgets and such onto the UI. Is there a way to modify the code of the UI at this point?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @Patrick_Li said in Creating add/delete buttons that modify a list:

                I haven't been using any code for this, instead I've only been dragging-and-dropping widgets and such onto the UI

                After I wrote my comment, I thought so :)

                Is there a way to modify the code of the UI at this point?

                Unfortunately not.
                The Qt Designer is just an auxiliary tool to create simple UI's or help you with designing more complex ones.
                Therefore QtDesigner is limited and there are many many things, which are simply not possible to do using the Design Mode only.
                You can make direct widget-2-widget connections, when the signal and the slot are compatible, e.g. a connection for clear()'ing the whole list on button click() would be possible to establish just from your UI Designer.
                But deleting specific items from that list or adding a new item, wont work this way.

                Because I've said, it is easy, I made some tests myself to see if it's really that easy and I came up with this:
                Feel free to use it or ask, if something's not clear :)

                    connect(ui->pushButton_add, &QPushButton::clicked, [this](){
                
                        bool ok;
                        QString text = QInputDialog::getText(this, tr("Create new list item"),
                                                                 tr("Insert item name:"), QLineEdit::Normal,
                                                                 "New item", &ok);
                        if(ok && !text.isEmpty())
                           ui->listWidget->addItem(text);
                
                    });
                
                    connect(ui->pushButton_delete, &QPushButton::clicked, [this](){
                
                        for(auto item: ui->listWidget->selectedItems()){
                
                            int takeAtRow = ui->listWidget->row(item);
                            auto removedItem = ui->listWidget->takeItem(takeAtRow);
                            delete removedItem;
                
                        }
                
                    });
                

                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                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