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. QTreeView - QStyledItemDelegate question
Forum Updated to NodeBB v4.3 + New Features

QTreeView - QStyledItemDelegate question

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.0k 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.
  • L Offline
    L Offline
    Lati
    wrote on last edited by
    #1

    Hei,

    I create buttons in QTreeView cells using the code:
    Header:

    class ButtonDelegateTableView : public QStyledItemDelegate{
        Q_OBJECT
    
    public:
        explicit ButtonDelegateTableView(QObject* parent = Q_NULLPTR,
                                         Project *currentProject = new Project())
            ;QStyledItemDelegate(parent);
    
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        // vars
        Project *_currentProject;
    };
    

    .cpp:

    ButtonDelegateTableView::ButtonDelegateTableView(QObject* parent,
                                                     Project *currentProject) : QStyledItemDelegate(parent)
    {
        _currentProject = currentProject;
    }
    
    //ButtonDelegateTableView::~ButtonDelegateTableView(){};
    
    void ButtonDelegateTableView::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    //        Q_ASSERT(index.isValid());
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
        const QWidget *widget = option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    
        QStyleOptionButton editButtonOption;
        editButtonOption.text = QString("Add"); 
        editButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
        editButtonOption.features = QStyleOptionButton::None;
        editButtonOption.direction = option.direction;
        editButtonOption.fontMetrics = option.fontMetrics;
        editButtonOption.palette = option.palette;
        editButtonOption.styleObject = option.styleObject;
        //
        style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
    
    }
    
    QSize ButtonDelegateTableView::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        const QSize baseSize = QStyledItemDelegate::sizeHint(option,index);
        return QSize(baseSize.width()+(2*baseSize.height()),baseSize.height());
    }
    
    QWidget* ButtonDelegateTableView::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QWidget* result = new QWidget(parent);
        result->setGeometry(option.rect);
        QWidget* baseEditor = QStyledItemDelegate::createEditor(result,option,index);
        baseEditor->setObjectName("baseEditor");
        baseEditor->setGeometry(0,0,option.rect.width()-(option.rect.height()),option.rect.height());
        QPushButton* editButton = new QPushButton("Add", result);
        editButton->setObjectName("editButton");
        editButton->setGeometry(option.rect.width()-(option.rect.height()), 0, option.rect.height(),option.rect.height());
        editButton->setToolTip("Add/edit");
    
        return result;
    }
    
    void ButtonDelegateTableView::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
        Q_ASSERT(baseEditor);
        QStyledItemDelegate::setEditorData(baseEditor,index);
    }
    void ButtonDelegateTableView::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
        Q_ASSERT(baseEditor);
        QStyledItemDelegate::setModelData(baseEditor,model,index);
    }
    void ButtonDelegateTableView::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        Q_UNUSED(index)
        editor->setGeometry(option.rect);
        QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
        Q_ASSERT(baseEditor);
        baseEditor->setGeometry(0,0,option.rect.width()-(option.rect.height()),option.rect.height());
        QWidget* editButton = editor->findChild<QWidget*>("editButton");
        Q_ASSERT(editButton);
        editButton->setGeometry(option.rect.width()-(option.rect.height()), 0, option.rect.height(),option.rect.height());
    
        editor->setGeometry(option.rect);
    }
    

    And the resulting table looks like this:
    4e9fa.png

    My question: How will I know the row number of the clicked "Add" button?
    The current code gives me all the time the last row number when I click on any "Add" button.

    Thanks for your responses in advance ! :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How do you get the information currently ?

      The index parameter does give you that from a delegate point of view.

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

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How do you get the information currently ?

        The index parameter does give you that from a delegate point of view.

        L Offline
        L Offline
        Lati
        wrote on last edited by
        #3

        @SGaist Thanks for your response.

        What I did so far is to pass the parameters to the delegate constructor and that was the reason obviously. I didn't think about the index to get the row number (I didn't know it though :)).

        Now I have another issue. How can I connect the buttons to a slot? I tried to connect within the createEditor function like following:

        QWidget* ButtonDelegateTableView::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            QWidget* result = new QWidget(parent);
            result->setGeometry(option.rect);
            QWidget* baseEditor = QStyledItemDelegate::createEditor(result,option,index);
            baseEditor->setObjectName("baseEditor");
            baseEditor->setGeometry(0,0,option.rect.width()-(option.rect.height()),option.rect.height());
            QPushButton* editButton = new QPushButton("Add", result);
            editButton->setObjectName("editButton");
            editButton->setGeometry(option.rect.width()-(option.rect.height()), 0, option.rect.height(),option.rect.height());
            editButton->setToolTip("Add/edit intervals");
        
            connect(editButton, SIGNAL(clicked()), this, SLOT(pushButtonCallback(_currentProject, index.row(), _setupIndex)));
        
            return result;
        }
        

        But no chance to call the pushButtonCallback when I click on a button.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          connect(editButton, &QPushButton::clicked, this, std::bind(&ButtonDelegateTableView::pushButtonCallback, this, _currentProject, index.row(), _setupIndex));

          "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

          L 1 Reply Last reply
          2
          • VRoninV VRonin

            connect(editButton, &QPushButton::clicked, this, std::bind(&ButtonDelegateTableView::pushButtonCallback, this, _currentProject, index.row(), _setupIndex));

            L Offline
            L Offline
            Lati
            wrote on last edited by
            #5

            @VRonin Thanks VRonin. But I got following 2 errors with your connect script:

            • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits:1468: error: C2672: 'std::invoke': no matching overloaded function found

            • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits:1468: error: C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

            Is it related to the slot function definition? It is as following:

            void ButtonDelegateTableView::pushButtonCallback(Project *currentProject, int rowNumber, int setupIndex)
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              you probably just forgot to #include <functional>

              "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

              L 1 Reply Last reply
              1
              • VRoninV VRonin

                you probably just forgot to #include <functional>

                L Offline
                L Offline
                Lati
                wrote on last edited by
                #7

                @VRonin Yes, I forgot to add that (I didn't know it to be honest :))

                It is added but I still get the same errors.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  is ButtonDelegateTableView::pushButtonCallback overloaded?

                  "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

                  L 2 Replies Last reply
                  0
                  • VRoninV VRonin

                    is ButtonDelegateTableView::pushButtonCallback overloaded?

                    L Offline
                    L Offline
                    Lati
                    wrote on last edited by Lati
                    #9

                    @VRonin Nope. I also tested it without any arguments as following and still have the same error:

                    connect(editButton, &QPushButton::clicked, this, std::bind(&ButtonDelegateTableView::pushButtonCallback, this));
                    
                    void ButtonDelegateTableView::pushButtonCallback()
                    
                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      is ButtonDelegateTableView::pushButtonCallback overloaded?

                      L Offline
                      L Offline
                      Lati
                      wrote on last edited by
                      #10

                      @VRonin I made it run using following lambda expression:

                          connect(editButton, &QPushButton::clicked, [=]()
                          {
                             pushButtonCallback( _currentProject, index.row(), _setupIndex);
                          } );
                      

                      Thanks for your response anyway.

                      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