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. add buttons in tablewidget's row
Forum Updated to NodeBB v4.3 + New Features

add buttons in tablewidget's row

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 8.6k 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.
  • S saber

    @JonB
    is there noway to achieve this?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @saber

    Hi
    have a look at
    http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html

    its bascailly what you want to do,.
    but except of drawing stars , you will draw your custom buttons.

    @JonB
    Its setWidgetItem, he hates due to bad performance. ;)
    But yes you can make this with Delegate.

    JonBJ 1 Reply Last reply
    1
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #7

      May setItemWidget burn in hell forever!

      #include <QApplication>
      #include <QTableView>
      #include <QStandardItemModel>
      #include <QStyledItemDelegate>
      #include <QHeaderView>
      #include <QPushButton>
      class TailButtonsDelegate : public QStyledItemDelegate{
          Q_OBJECT
          Q_DISABLE_COPY(TailButtonsDelegate)
      public:
          explicit TailButtonsDelegate(QObject* parent = Q_NULLPTR)
              :QStyledItemDelegate(parent)
          {}
          void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
              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);
              if(!(option.state & QStyle::State_Selected))
                  return;
              QStyleOptionButton editButtonOption;
              editButtonOption.text = QString(QChar(0x270D)); //use emoji for text, optionally you can use icon + iconSize
              editButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(2*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;
              QStyleOptionButton removeButtonOption(editButtonOption);
              removeButtonOption.text = QString(QChar(0x274C)); //use emoji for text, optionally you can use icon + iconSize
              removeButtonOption.rect = QRect(option.rect.left()+option.rect.width()-option.rect.height(),option.rect.top(),option.rect.height(),option.rect.height());
              style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
              style->drawControl(QStyle::CE_PushButton, &removeButtonOption, painter, widget);
          }
          QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
              const QSize baseSize = QStyledItemDelegate::sizeHint(option,index);
              return QSize(baseSize.width()+(2*baseSize.height()),baseSize.height());
          }
          QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
              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()-(2*option.rect.height()),option.rect.height());
              QPushButton* editButton = new QPushButton(QChar(0x270D),result);
              editButton->setObjectName("editButton");
              editButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
              connect(editButton,&QPushButton::clicked,[](){qDebug("Edit");});
              QPushButton* removeButton = new QPushButton(QChar(0x274C),result);
              removeButton->setObjectName("removeButton");
              removeButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
              connect(removeButton,&QPushButton::clicked,[](){qDebug("Remove");});
              return result;
          }
          void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE{
              QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
              Q_ASSERT(baseEditor);
              QStyledItemDelegate::setEditorData(baseEditor,index);
          }
          void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
              QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
              Q_ASSERT(baseEditor);
              QStyledItemDelegate::setModelData(baseEditor,model,index);
          }
          void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
              Q_UNUSED(index)
              editor->setGeometry(option.rect);
              QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
              Q_ASSERT(baseEditor);
              baseEditor->setGeometry(0,0,option.rect.width()-(2*option.rect.height()),option.rect.height());
              QWidget* editButton = editor->findChild<QWidget*>("editButton");
              Q_ASSERT(editButton);
              editButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
              QWidget* removeButton = editor->findChild<QWidget*>("removeButton");
              Q_ASSERT(removeButton);
              removeButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
              editor->setGeometry(option.rect);
          }
      };
      int main(int argc, char **argv) {
          QApplication app(argc, argv);
          QStandardItemModel baseModel;
          baseModel.insertRows(0, 3);
          baseModel.insertColumns(0, 4);
          for(int i=0;i<3;++i){
              for(int j=0;j<4;++j){
                  baseModel.setData(baseModel.index(i,j),QStringLiteral("%1,%2").arg(i).arg(j));
              }
          }
          QTableView view;
          view.setEditTriggers(QTableView::AllEditTriggers);
          view.setModel(&baseModel);
          view.setSelectionBehavior(QTableView::SelectRows);
          view.horizontalHeader()->setStretchLastSection(true);
          view.setItemDelegateForColumn(3,new TailButtonsDelegate(&view));
          view.show();
          return app.exec();
      }
      

      "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
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8

        And behold the Beauty
        alt text

        Thank you @VRonin :)

        1 Reply Last reply
        1
        • mrjjM mrjj

          @saber

          Hi
          have a look at
          http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html

          its bascailly what you want to do,.
          but except of drawing stars , you will draw your custom buttons.

          @JonB
          Its setWidgetItem, he hates due to bad performance. ;)
          But yes you can make this with Delegate.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #9

          @mrjj said in add buttons in tablewidget's row:

          @JonB
          Its setWidgetItem, he hates due to bad performance. ;)
          But yes you can make this with Delegate.

          Damn!
          Isn't there anything you can't do with delegates and which do require setting the widget item? :)

          mrjjM 1 Reply Last reply
          0
          • JonBJ JonB

            @mrjj said in add buttons in tablewidget's row:

            @JonB
            Its setWidgetItem, he hates due to bad performance. ;)
            But yes you can make this with Delegate.

            Damn!
            Isn't there anything you can't do with delegates and which do require setting the widget item? :)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @JonB
            Not really, but the delegate is kinda involving to make if you have
            multiple widgets and need to reflect various states. You have to go digging around in
            Qt source to see how to mirror the states to draw them with right QStyle calls.
            Also,say you want hover effect on your (drawn widgets) widgets
            then you suddenly have to add extra logic to handle and so on.
            So first time hurts a lot. ;)

            1 Reply Last reply
            4
            • S Offline
              S Offline
              saber
              wrote on last edited by
              #11

              @mrjj @VRonin
              i need help.
              i added the code in sparate header file and used this in the QTableWidget.
              by this way

              ui->boklist->setItemDelegateForColumn(1, new TailButtonsDelegate(ui->boklist));
              }
              

              here is the header file

              #ifndef TAILBUTTONSDELEGATE_H
              #define TAILBUTTONSDELEGATE_H
              
              #include <QApplication>
              #include <QTableView>
              #include <QStandardItemModel>
              #include <QStyledItemDelegate>
              #include <QHeaderView>
              #include <QPushButton>
              
              class TailButtonsDelegate : public QStyledItemDelegate {
                  Q_OBJECT
                  Q_DISABLE_COPY(TailButtonsDelegate)
              public:
                  explicit TailButtonsDelegate(QObject* parent = Q_NULLPTR)
                      :QStyledItemDelegate(parent)
                  {}
              
                  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      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);
                      if(!(option.state & QStyle::State_Selected))
                          return;
                      QStyleOptionButton editButtonOption;
                      editButtonOption.text = QString(QChar(0x270D)); //use emoji for text, optionally you can use icon + iconSize
                      editButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(2*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;
                      QStyleOptionButton removeButtonOption(editButtonOption);
                      removeButtonOption.text = QString(QChar(0x274C)); //use emoji for text, optionally you can use icon + iconSize
                      removeButtonOption.rect = QRect(option.rect.left()+option.rect.width()-option.rect.height(),option.rect.top(),option.rect.height(),option.rect.height());
                      style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
                      style->drawControl(QStyle::CE_PushButton, &removeButtonOption, painter, widget);
                  }
                  QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      const QSize baseSize = QStyledItemDelegate::sizeHint(option,index);
                      return QSize(baseSize.width()+(2*baseSize.height()),baseSize.height());
                  }
                  QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      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()-(2*option.rect.height()),option.rect.height());
                      QPushButton* editButton = new QPushButton(/*QChar(0x270D),*/ result);
                      editButton->setObjectName("editButton");
                      editButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
                      connect(editButton,&QPushButton::clicked,[](){qDebug("Edit");});
                      QPushButton* removeButton = new QPushButton(QChar(0x274C),result);
                      removeButton->setObjectName("removeButton");
                      removeButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                      connect(removeButton,&QPushButton::clicked,[](){qDebug("Remove");});
                      return result;
                  }
                  void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                      Q_ASSERT(baseEditor);
                      QStyledItemDelegate::setEditorData(baseEditor,index);
                  }
                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                      Q_ASSERT(baseEditor);
                      QStyledItemDelegate::setModelData(baseEditor,model,index);
                  }
                  void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      Q_UNUSED(index)
                      editor->setGeometry(option.rect);
                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                      Q_ASSERT(baseEditor);
                      baseEditor->setGeometry(0,0,option.rect.width()-(2*option.rect.height()),option.rect.height());
                      QWidget* editButton = editor->findChild<QWidget*>("editButton");
                      Q_ASSERT(editButton);
                      editButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
                      QWidget* removeButton = editor->findChild<QWidget*>("removeButton");
                      Q_ASSERT(removeButton);
                      removeButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                      editor->setGeometry(option.rect);
                  }
              };
              
              #endif // TAILBUTTONSDELEGATE_H
              
              

              it works but there is some problem.

              1. i want to set the icon from a resource file . how can i do that?
                0_1536403880465_a.png
              2. when i clicked in the row that is going in the edit mode.(only second column)
                0_1536403890495_a2.png

              please help.

              NT:

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #12

                hi

                i want to set the icon from a resource file . how can i do that?

                Add it to the res file and
                use syntax
                ":/tractor.png"
                To make sure syntax is correct, RIGHt click file after adding it.
                it show the path to use.

                -when i clicked in the row that is going in the edit mode.(only second column)
                Im not sure what issue is. you never edit a "row" just a cell.

                S 1 Reply Last reply
                0
                • mrjjM mrjj

                  hi

                  i want to set the icon from a resource file . how can i do that?

                  Add it to the res file and
                  use syntax
                  ":/tractor.png"
                  To make sure syntax is correct, RIGHt click file after adding it.
                  it show the path to use.

                  -when i clicked in the row that is going in the edit mode.(only second column)
                  Im not sure what issue is. you never edit a "row" just a cell.

                  S Offline
                  S Offline
                  saber
                  wrote on last edited by saber
                  #13

                  @mrjj
                  my mistake .
                  second cell is going in edit mode.
                  here is the ui vriable
                  0_1536405278895_s.png

                  NT : icon not showing.just showing a part of the text.
                  0_1536405506382_e.png

                  /editButtonOption.text = QString(":/icons/edit.svg");
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    saber
                    wrote on last edited by saber
                    #14

                    ok. i found the fix to show icon .

                     editButtonOption.icon = QIcon(":/icons/edit.svg");
                    

                    but the icon is not in center.
                    0_1536407763919_k.png

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      edwin1234
                      wrote on last edited by edwin1234
                      #15

                      i have done making the button inside the table but i cannot click it

                      mrjjM 1 Reply Last reply
                      0
                      • E edwin1234

                        i have done making the button inside the table but i cannot click it

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @edwin1234
                        hi
                        Did you use the TailButtonsDelegate ?

                        E 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @edwin1234
                          hi
                          Did you use the TailButtonsDelegate ?

                          E Offline
                          E Offline
                          edwin1234
                          wrote on last edited by
                          #17

                          @mrjj yes i did i used this as a header file

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            edwin1234
                            wrote on last edited by
                            #18
                            #ifndef TAILBUTTONSDELEGATE_H
                            #define TAILBUTTONSDELEGATE_H
                            #include <QApplication>
                            #include <QTableView>
                            #include <QStandardItemModel>
                            #include <QPushButton>
                            #include <QIcon>
                            #include <QSize>
                            #include <QDebug>
                            #include <QStyledItemDelegate>
                            #include <QHeaderView>
                            #include <QPushButton>
                            class TailButtonsDelegate : public QStyledItemDelegate {
                                Q_OBJECT
                                Q_DISABLE_COPY(TailButtonsDelegate)
                            private slots:
                                void on_push_button_editprodorder();
                            public:
                                explicit TailButtonsDelegate(QObject* parent = Q_NULLPTR)
                                    :QStyledItemDelegate(parent)
                                {}
                            
                                void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    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);
                                    if(!(option.state & QStyle::State_Selected))
                                        return;
                            
                                    QStyleOptionButton editButtonOption;
                                    editButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/fill.svg"); //use emoji for text, optionally you can use icon + iconSize
                                    editButtonOption.iconSize = QSize(10,10);
                                    editButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(5*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;
                                    QStyleOptionButton removeButtonOption(editButtonOption);
                                    removeButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/delete-left.svg"); //use emoji for text, optionally you can use icon + iconSize
                                    removeButtonOption.iconSize = QSize(10,10);
                                    removeButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(4*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                    QStyleOptionButton SKUButtonOption(editButtonOption);
                                    SKUButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/code-commit.svg"); //use emoji for text, optionally you can use icon + iconSize
                                    SKUButtonOption.iconSize = QSize(10,10);
                                    SKUButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(3*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                    QStyleOptionButton StartButtonOption(editButtonOption);
                                    StartButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/calendar-plus.svg"); //use emoji for text, optionally you can use icon + iconSize
                                    StartButtonOption.iconSize = QSize(10,10);
                                    StartButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(2*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                    QStyleOptionButton CloseButtonOption(editButtonOption);
                                    CloseButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/stop.svg"); //use emoji for text, optionally you can use icon + iconSize
                                    CloseButtonOption.iconSize = QSize(10,10);
                                    CloseButtonOption.rect = QRect(option.rect.left()+option.rect.width()-option.rect.height(),option.rect.top(),option.rect.height(),option.rect.height());
                            
                                    style->drawControl(QStyle::CE_PushButton, &SKUButtonOption, painter, widget);
                                    style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
                                    style->drawControl(QStyle::CE_PushButton, &removeButtonOption, painter, widget);
                                    style->drawControl(QStyle::CE_PushButton, &StartButtonOption, painter, widget);
                                    style->drawControl(QStyle::CE_PushButton, &CloseButtonOption, painter, widget);
                            
                            
                                }
                                QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    const QSize baseSize = QStyledItemDelegate::sizeHint(option,index);
                                    return QSize(baseSize.width()+(2*baseSize.height()),baseSize.height());
                                }
                                QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    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()-(5*option.rect.height()),option.rect.height());
                                    QPushButton* editButton = new QPushButton(/*QChar(0x270D), */result);
                                    editButton->setObjectName("editButton");
                                    editButton->setGeometry(option.rect.width()-(5*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                    connect(editButton,&QPushButton::clicked,[](){qDebug() <<"Edit";});
                                    QPushButton* removeButton = new QPushButton(QChar(0x274C),result);
                                    removeButton->setObjectName("removeButton");
                                    removeButton->setGeometry(option.rect.width()-(4*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                    connect(removeButton,&QPushButton::clicked,[](){qDebug() <<"Remove";});
                                    QPushButton* SKUButton = new QPushButton(QChar(0x274C),result);
                                    SKUButton->setObjectName("SKUButton");
                                    SKUButton->setGeometry(option.rect.width()-(3*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                    connect(SKUButton,&QPushButton::clicked,[](){qDebug() <<"Sub Part SKU";});
                                    QPushButton* StartButton = new QPushButton(QChar(0x274C),result);
                                    StartButton->setObjectName("StartButton");
                                    StartButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                    connect(StartButton,&QPushButton::clicked,[](){qDebug() <<"Start Plan Order";});
                                    QPushButton* CloseButton = new QPushButton(QChar(0x274C),result);
                                    CloseButton->setObjectName("CloseButton");
                                    CloseButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                                    connect(CloseButton,&QPushButton::clicked,[](){qDebug() << "Close Plan Order";});
                                    return result;
                                }
                                void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                    Q_ASSERT(baseEditor);
                                    QStyledItemDelegate::setEditorData(baseEditor,index);
                                }
                                void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                    Q_ASSERT(baseEditor);
                                    QStyledItemDelegate::setModelData(baseEditor,model,index);
                                }
                                void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                    Q_UNUSED(index)
                                    editor->setGeometry(option.rect);
                                    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                    Q_ASSERT(baseEditor);
                                    baseEditor->setGeometry(0,0,option.rect.width()-(5*option.rect.height()),option.rect.height());
                            
                                    QWidget* editButton = editor->findChild<QWidget*>("editButton");
                                    Q_ASSERT(editButton);
                                    editButton->setGeometry(option.rect.width()-(5*option.rect.height()), 0, option.rect.height(),option.rect.height());
                            
                                    QWidget* removeButton = editor->findChild<QWidget*>("removeButton");
                                    Q_ASSERT(removeButton);
                                    removeButton->setGeometry(option.rect.width()-(4*option.rect.height()), 0, option.rect.height(),option.rect.height());
                            
                                    QWidget* SKUButton = editor->findChild<QWidget*>("SKUButton");
                                    Q_ASSERT(SKUButton);
                                    SKUButton->setGeometry(option.rect.width()-(3*option.rect.height()), 0, option.rect.height(),option.rect.height());
                            
                                    QWidget* StartButton = editor->findChild<QWidget*>("StartButton");
                                    Q_ASSERT(StartButton);
                                    StartButton->setGeometry(option.rect.width()-(2 *option.rect.height()), 0, option.rect.height(),option.rect.height());
                            
                                    QWidget* CloseButton = editor->findChild<QWidget*>("CloseButton");
                                    Q_ASSERT(CloseButton);
                                    CloseButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                                    editor->setGeometry(option.rect);
                                }
                            };
                            #endif // TAILBUTTONSDELEGATE_H
                            
                            
                            mrjjM 1 Reply Last reply
                            0
                            • E edwin1234
                              #ifndef TAILBUTTONSDELEGATE_H
                              #define TAILBUTTONSDELEGATE_H
                              #include <QApplication>
                              #include <QTableView>
                              #include <QStandardItemModel>
                              #include <QPushButton>
                              #include <QIcon>
                              #include <QSize>
                              #include <QDebug>
                              #include <QStyledItemDelegate>
                              #include <QHeaderView>
                              #include <QPushButton>
                              class TailButtonsDelegate : public QStyledItemDelegate {
                                  Q_OBJECT
                                  Q_DISABLE_COPY(TailButtonsDelegate)
                              private slots:
                                  void on_push_button_editprodorder();
                              public:
                                  explicit TailButtonsDelegate(QObject* parent = Q_NULLPTR)
                                      :QStyledItemDelegate(parent)
                                  {}
                              
                                  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      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);
                                      if(!(option.state & QStyle::State_Selected))
                                          return;
                              
                                      QStyleOptionButton editButtonOption;
                                      editButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/fill.svg"); //use emoji for text, optionally you can use icon + iconSize
                                      editButtonOption.iconSize = QSize(10,10);
                                      editButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(5*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;
                                      QStyleOptionButton removeButtonOption(editButtonOption);
                                      removeButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/delete-left.svg"); //use emoji for text, optionally you can use icon + iconSize
                                      removeButtonOption.iconSize = QSize(10,10);
                                      removeButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(4*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                      QStyleOptionButton SKUButtonOption(editButtonOption);
                                      SKUButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/code-commit.svg"); //use emoji for text, optionally you can use icon + iconSize
                                      SKUButtonOption.iconSize = QSize(10,10);
                                      SKUButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(3*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                      QStyleOptionButton StartButtonOption(editButtonOption);
                                      StartButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/calendar-plus.svg"); //use emoji for text, optionally you can use icon + iconSize
                                      StartButtonOption.iconSize = QSize(10,10);
                                      StartButtonOption.rect = QRect(option.rect.left()+option.rect.width()-(2*option.rect.height()),option.rect.top(),option.rect.height(),option.rect.height());
                                      QStyleOptionButton CloseButtonOption(editButtonOption);
                                      CloseButtonOption.icon = QIcon(":/icon/fontawesome-free-6.2.0-desktop/svgs/solid/stop.svg"); //use emoji for text, optionally you can use icon + iconSize
                                      CloseButtonOption.iconSize = QSize(10,10);
                                      CloseButtonOption.rect = QRect(option.rect.left()+option.rect.width()-option.rect.height(),option.rect.top(),option.rect.height(),option.rect.height());
                              
                                      style->drawControl(QStyle::CE_PushButton, &SKUButtonOption, painter, widget);
                                      style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
                                      style->drawControl(QStyle::CE_PushButton, &removeButtonOption, painter, widget);
                                      style->drawControl(QStyle::CE_PushButton, &StartButtonOption, painter, widget);
                                      style->drawControl(QStyle::CE_PushButton, &CloseButtonOption, painter, widget);
                              
                              
                                  }
                                  QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      const QSize baseSize = QStyledItemDelegate::sizeHint(option,index);
                                      return QSize(baseSize.width()+(2*baseSize.height()),baseSize.height());
                                  }
                                  QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      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()-(5*option.rect.height()),option.rect.height());
                                      QPushButton* editButton = new QPushButton(/*QChar(0x270D), */result);
                                      editButton->setObjectName("editButton");
                                      editButton->setGeometry(option.rect.width()-(5*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                      connect(editButton,&QPushButton::clicked,[](){qDebug() <<"Edit";});
                                      QPushButton* removeButton = new QPushButton(QChar(0x274C),result);
                                      removeButton->setObjectName("removeButton");
                                      removeButton->setGeometry(option.rect.width()-(4*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                      connect(removeButton,&QPushButton::clicked,[](){qDebug() <<"Remove";});
                                      QPushButton* SKUButton = new QPushButton(QChar(0x274C),result);
                                      SKUButton->setObjectName("SKUButton");
                                      SKUButton->setGeometry(option.rect.width()-(3*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                      connect(SKUButton,&QPushButton::clicked,[](){qDebug() <<"Sub Part SKU";});
                                      QPushButton* StartButton = new QPushButton(QChar(0x274C),result);
                                      StartButton->setObjectName("StartButton");
                                      StartButton->setGeometry(option.rect.width()-(2*option.rect.height()), 0, option.rect.height(),option.rect.height());
                                      connect(StartButton,&QPushButton::clicked,[](){qDebug() <<"Start Plan Order";});
                                      QPushButton* CloseButton = new QPushButton(QChar(0x274C),result);
                                      CloseButton->setObjectName("CloseButton");
                                      CloseButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                                      connect(CloseButton,&QPushButton::clicked,[](){qDebug() << "Close Plan Order";});
                                      return result;
                                  }
                                  void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                      Q_ASSERT(baseEditor);
                                      QStyledItemDelegate::setEditorData(baseEditor,index);
                                  }
                                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                      Q_ASSERT(baseEditor);
                                      QStyledItemDelegate::setModelData(baseEditor,model,index);
                                  }
                                  void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                                      Q_UNUSED(index)
                                      editor->setGeometry(option.rect);
                                      QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
                                      Q_ASSERT(baseEditor);
                                      baseEditor->setGeometry(0,0,option.rect.width()-(5*option.rect.height()),option.rect.height());
                              
                                      QWidget* editButton = editor->findChild<QWidget*>("editButton");
                                      Q_ASSERT(editButton);
                                      editButton->setGeometry(option.rect.width()-(5*option.rect.height()), 0, option.rect.height(),option.rect.height());
                              
                                      QWidget* removeButton = editor->findChild<QWidget*>("removeButton");
                                      Q_ASSERT(removeButton);
                                      removeButton->setGeometry(option.rect.width()-(4*option.rect.height()), 0, option.rect.height(),option.rect.height());
                              
                                      QWidget* SKUButton = editor->findChild<QWidget*>("SKUButton");
                                      Q_ASSERT(SKUButton);
                                      SKUButton->setGeometry(option.rect.width()-(3*option.rect.height()), 0, option.rect.height(),option.rect.height());
                              
                                      QWidget* StartButton = editor->findChild<QWidget*>("StartButton");
                                      Q_ASSERT(StartButton);
                                      StartButton->setGeometry(option.rect.width()-(2 *option.rect.height()), 0, option.rect.height(),option.rect.height());
                              
                                      QWidget* CloseButton = editor->findChild<QWidget*>("CloseButton");
                                      Q_ASSERT(CloseButton);
                                      CloseButton->setGeometry(option.rect.width()-option.rect.height(), 0, option.rect.height(),option.rect.height());
                                      editor->setGeometry(option.rect);
                                  }
                              };
                              #endif // TAILBUTTONSDELEGATE_H
                              
                              
                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #19

                              Hi
                              So what do you mean by "inside the table but i cannot click it" ?

                              It's currently connected to a lambda that prints "Edit" in application output.

                              connect(editButton,&QPushButton::clicked,{qDebug("Edit");});

                              This part is not working ? or what is not working ?

                              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