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 can I remove rows from QComboBox?
Forum Updated to NodeBB v4.3 + New Features

How can I remove rows from QComboBox?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 642 Views 1 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Hi,

    I have subclass of QComboBox, where I set model ( QStandardItemModel ) with QStandardItems.

    Now I have a vector with QStandardItems ( his name is items ):

    // ( in QComboBox subclass constructor ):
    
        itemModel = new QStandardItemModel;
        for(auto text: itemsText)
        {
            items.append(new QStandardItem(text));
            items.last()->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
            items.last()->setData(Qt::Unchecked, Qt::CheckStateRole);
            itemModel->setItem(itemModel->rowCount(),0,items.last());
        }
        setModel(itemModel);
    

    Now I setIndexWidget in view() - QPushButtons. So all my items in QComboBox have widgets on it.

    // for example:
    
        QModelIndex index = model()->index(0,0);
        view()->setIndexWidget(index, button);
    

    Next I would like to delete in a proper way item from model and from QComboBox when I clicked on this button and delete that buttons. Now my code is something like:

    int row = getItemRow();    <= here I get row, where is that QPushButton, which I clicked
    model()->removeRow(row);
    showPopup();
    

    And my questions:

    1. Need I that QVector with QStandardItems or can I only have items in model?
    2. Have I to delete all items in QStandardItemModel or QStandardItemModel deletes items like QVector ( I haven't do anything )
    3. model()->removeRow(row) <- really delete row or only change parent like takeAt in QGridLayout?
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You can get the pushbutton with
      https://doc.qt.io/qt-5/qabstractitemview.html#indexWidget

      1. The model owns the items and unless you need to keep track of them for other reasons ( like update text)
        i dont think there is any reason to have a list also besides the model.

      2. The model owns the items and will delete them so no manually deleting is needed.

      3. https://doc.qt.io/qt-5/qstandarditem.html#removeRow
        "The items that were in the row are deleted."

      1 Reply Last reply
      3
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by TomNow99
        #3

        @mrjj Thank you :)

        Only one more question. How can I add items to model not using vector? Something like that:

            for(auto text: itemsText)
            {
                QStandardItem *tmp = new QStandardItem(text);
                tmp->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
                tmp->setData(Qt::Unchecked, Qt::CheckStateRole);
                itemModel->setItem(itemModel->rowCount(),0,tmp);
            }
        

        EDIT: And one more:

        What with QPushButtons in model? Have I delete them?

        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