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. Is QStyledItemDelegate::createEditor returned value monitored?
Forum Updated to NodeBB v4.3 + New Features

Is QStyledItemDelegate::createEditor returned value monitored?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.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.
  • tanmanh0707T Offline
    tanmanh0707T Offline
    tanmanh0707
    wrote on last edited by
    #1

    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.

    VRoninV 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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

      tanmanh0707T 1 Reply Last reply
      5
      • tanmanh0707T tanmanh0707

        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.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        tanmanh0707T 2 Replies Last reply
        2
        • dheerendraD dheerendra

          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.

          tanmanh0707T Offline
          tanmanh0707T Offline
          tanmanh0707
          wrote on last edited by
          #4

          @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?

          VRoninV 1 Reply Last reply
          0
          • VRoninV VRonin

            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

            tanmanh0707T Offline
            tanmanh0707T Offline
            tanmanh0707
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • tanmanh0707T tanmanh0707

              @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?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @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

              tanmanh0707T 1 Reply Last reply
              2
              • VRoninV VRonin

                @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

                tanmanh0707T Offline
                tanmanh0707T Offline
                tanmanh0707
                wrote on last edited by
                #7

                @VRonin Thank you very much. I am clear now

                1 Reply Last reply
                0
                • VRoninV VRonin

                  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

                  tanmanh0707T Offline
                  tanmanh0707T Offline
                  tanmanh0707
                  wrote on last edited by
                  #8

                  @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
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    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
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved