Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Is QStyledItemDelegate::createEditor returned value monitored?

    General and Desktop
    3
    9
    911
    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.
    • tanmanh0707
      tanmanh0707 last edited by

      Hi all,

      I'm working on QTableView with a combo box inside. I chose QStyledItemDelegate for my combo box display.

      By doing this method, QWidget* createEditor(...) and other methods need to be re-implemented. I can say, everytime a cell containing ComboBox in QTableView is in edit mode, this createEditor is called.

      My source code is referred at https://wiki.qt.io/Combo_Boxes_in_Item_Views as below

      QWidget* ComboBoxItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
          // ComboBox ony in column 2
          if (index.column() != 1)
              return QStyledItemDelegate::createEditor(parent, option, index);
      
          // Create the combobox and populate it
          QComboBox* cb = new QComboBox(parent);
          int row = index.row();
          cb->addItem(QString("one in row %1").arg(row));
          cb->addItem(QString("two in row %1").arg(row));
          cb->addItem(QString("three in row %1").arg(row));
          return cb;
      }
      

      My question is, is the pointer cb monitored? I mean, when will it be deleted and who has responsibility to do that? Because as I mentioned above, when a cell contains comboBox is clicked for editing, this function is called which means a lot of memory areas are allocated.

      Thank you.

      VRonin 1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        It is taken care. All the editors will be set with parent. When you delete the parent, editors will be automatically deleted. You don't have to anything extra.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        tanmanh0707 1 Reply Last reply Reply Quote 5
        • VRonin
          VRonin @tanmanh0707 last edited by

          One side note: if (index.column() inside a delegate is bad design as it ties the delegate to a specific model. use QAbstractItemView::setItemDelegateForColumn instead

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          tanmanh0707 2 Replies Last reply Reply Quote 2
          • tanmanh0707
            tanmanh0707 @dheerendra last edited by

            @dheerendra As example at above link https://wiki.qt.io/Combo_Boxes_in_Item_Views, in the function main(), the ComboBoxItemDelegate* cbid object is a child of parent QTableWidget tw;

            As you said, as long as the tw object is alive, the cbid object is still there. That means, a lot of cbid objects will be created when clicking a cell containing combo box for editing. Do I understand it correctly?

            VRonin 1 Reply Last reply Reply Quote 0
            • tanmanh0707
              tanmanh0707 @VRonin last edited by

              @VRonin Thank you for your suggestion. Let me study QAbstractItemView::setItemDelegateForColumn as you mentioned first.

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin @tanmanh0707 last edited by

                @tanmanh0707 said in Is QStyledItemDelegate::createEditor returned value monitored?:

                as long as the tw object is alive, the cbid object is still there

                Nope, the view will call QAbstractItemDelegate::destroyEditor once it doesn't need the editor anymore

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                tanmanh0707 1 Reply Last reply Reply Quote 2
                • tanmanh0707
                  tanmanh0707 @VRonin last edited by

                  @VRonin Thank you very much. I am clear now

                  1 Reply Last reply Reply Quote 0
                  • tanmanh0707
                    tanmanh0707 @VRonin last edited by

                    @VRonin said in Is QStyledItemDelegate::createEditor returned value monitored?:

                    One side note: if (index.column() inside a delegate is bad design as it ties the delegate to a specific model. use QAbstractItemView::setItemDelegateForColumn instead

                    Hi VRonin again,

                    I just want to use delegate on some cells, not a whole column nor row. Is there any way to do that without using if (index.column()) if (index.row())

                    1 Reply Last reply Reply Quote 0
                    • VRonin
                      VRonin last edited by

                      In these cases you normally use a QItemEditorFactory in setItemEditorFactory to create the editor you need for the cell based on the data in the model

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

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