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 form widget in QTableview using model view
Forum Update on Tuesday, May 27th 2025

Add form widget in QTableview using model view

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 3.9k Views 3 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.
  • J Offline
    J Offline
    JadeN001
    wrote on 25 Jun 2018, 09:49 last edited by
    #1

    I want to add a widget form ,in a cell of table every time a client gets connected.
    I have created a widget form and added it in QItemDelegate using function createEditor().But when I click on cell ,form is getting generated outside the cell.
    How to set size of QWidget form with respect to cell of table.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 25 Jun 2018, 10:59 last edited by
      #2

      I think you need to position the widget yourself by Implementing updateEditorGeometry() in your item delegate class.

      J 1 Reply Last reply 25 Jun 2018, 11:04
      0
      • M mpergand
        25 Jun 2018, 10:59

        I think you need to position the widget yourself by Implementing updateEditorGeometry() in your item delegate class.

        J Offline
        J Offline
        JadeN001
        wrote on 25 Jun 2018, 11:04 last edited by JadeN001
        #3

        @mpergand

        dialog.cpp

        #include "dialog.h"
        #include "ui_dialog.h"
        Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {
            ui->setupUi(this);
            D = new Delegate(this);
        
            model = new QStandardItemModel(4,2,this);
            ui->tableView->setModel(model);
            TableV->setModel(model);
            for(int row=0;row<4;++row)
            {
                for(int col=0;col<2;col++)
                {
                    QModelIndex index= model->index(row,col,QModelIndex());
                   ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                   ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                    model->setData(index,0);
                }
            }
        
            ui->tableView->resizeRowsToContents();
        
           ui->tableView->setItemDelegate(D);
        }
        
        Dialog::~Dialog()
        {
            delete ui;
        }
        
        
        

        this is how I have set the headerview .When i clicked on first cell,
        Output is :
        0_1529929236911_op2.png

        But it display when i create that particullar cell.
        So,Is there a way to create an Editor with delegate?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Jun 2018, 21:32 last edited by
          #4

          Hi,

          Can you show your Delegate class code ?

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

          J 1 Reply Last reply 26 Jun 2018, 04:45
          0
          • S SGaist
            25 Jun 2018, 21:32

            Hi,

            Can you show your Delegate class code ?

            J Offline
            J Offline
            JadeN001
            wrote on 26 Jun 2018, 04:45 last edited by
            #5

            @SGaist

            #include "delegate.h"
            #include<QDebug>
            Delegate::Delegate(QObject *parent):QItemDelegate(parent)
            {
            
            }
            
            Delegate::~Delegate() {
            
            }
            
            QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
            
                clientForm *CF=new clientForm(parent);
                qDebug()<<"in creatEditer";
                return CF;
            
            }
            
            void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
            {
            
            }
            
            void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
            {
            
            }
            
            QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                return QSize(256,192);
            }
            
            void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                qDebug()<<"in geometry..";
              editor->setGeometry(option.rect);
            
            }
            
            
            1 Reply Last reply
            0
            • J Offline
              J Offline
              JadeN001
              wrote on 26 Jun 2018, 11:40 last edited by
              #6

              how to add row and column as needed in Qtableview

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Jun 2018, 20:07 last edited by
                #7

                The QTableView shows what the model has to give. If the model says 8 column then the table view will show 8.

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

                J 1 Reply Last reply 27 Jun 2018, 05:07
                1
                • S SGaist
                  26 Jun 2018, 20:07

                  The QTableView shows what the model has to give. If the model says 8 column then the table view will show 8.

                  J Offline
                  J Offline
                  JadeN001
                  wrote on 27 Jun 2018, 05:07 last edited by JadeN001
                  #8

                  @SGaist okay It means I have to manage model.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JadeN001
                    wrote on 27 Jun 2018, 10:57 last edited by
                    #9

                    In my code i have used insertrow() in my QStandardItemModel.

                    void MainWindow::seteditor()
                    {
                    
                       model=new QStandardItemModel(this);
                    
                        QStandardItem *item=new QStandardItem(" ");
                        ui->tableView->setModel(model);
                    
                          for(int row=0;row<4;++row)
                          {
                               model->insertRow(row,item);
                               QModelIndex index= model->index(row,1,QModelIndex());
                             ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                            ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                               ui->tableView->setItemDelegate(D);
                               ui->tableView->openPersistentEditor(index);  '<- not working'
                    
                    
                          }
                    
                    
                    
                            }
                    

                    however if I use :

                       model=new QStandardItemModel(8,2,this);
                    
                    

                    instead of

                       model=new QStandardItemModel(this);
                    
                    

                    It works properly.But I want to use first approach.Where I am doing mistake?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 27 Jun 2018, 16:23 last edited by
                      #10

                      Hi
                      You mean like
                      model=new QStandardItemModel(this);
                      model->setRowCount(8):
                      model->setColumnCount(2):

                      J 1 Reply Last reply 28 Jun 2018, 04:41
                      1
                      • M mrjj
                        27 Jun 2018, 16:23

                        Hi
                        You mean like
                        model=new QStandardItemModel(this);
                        model->setRowCount(8):
                        model->setColumnCount(2):

                        J Offline
                        J Offline
                        JadeN001
                        wrote on 28 Jun 2018, 04:41 last edited by
                        #11

                        @mrjj I want to increase number of cells based on client connection.So for that i have to use counter for generating rows and columns.

                        It is helpful to use
                        model=new QStandardItemModel(this);
                        model->setRowCount(8):
                        model->setColumnCount(2):
                        Instead of model=new QStandardItemModel(8,2,this); so that i can set rows and column based on requirments.

                        I am on right track or not i don't.suggest me if there is ant better way for my scenario.

                        M 1 Reply Last reply 28 Jun 2018, 06:17
                        0
                        • J JadeN001
                          28 Jun 2018, 04:41

                          @mrjj I want to increase number of cells based on client connection.So for that i have to use counter for generating rows and columns.

                          It is helpful to use
                          model=new QStandardItemModel(this);
                          model->setRowCount(8):
                          model->setColumnCount(2):
                          Instead of model=new QStandardItemModel(8,2,this); so that i can set rows and column based on requirments.

                          I am on right track or not i don't.suggest me if there is ant better way for my scenario.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 28 Jun 2018, 06:17 last edited by
                          #12

                          @JadeN001
                          Hi
                          you should test if
                          void QStandardItemModel::appendRow(const QList<QStandardItem *> & items)
                          does not auto increase row count also.

                          J 1 Reply Last reply 29 Jun 2018, 10:22
                          1
                          • M mrjj
                            28 Jun 2018, 06:17

                            @JadeN001
                            Hi
                            you should test if
                            void QStandardItemModel::appendRow(const QList<QStandardItem *> & items)
                            does not auto increase row count also.

                            J Offline
                            J Offline
                            JadeN001
                            wrote on 29 Jun 2018, 10:22 last edited by JadeN001
                            #13

                            @mrjj thanks..
                            It does not auto increase row count

                            M 1 Reply Last reply 29 Jun 2018, 10:49
                            1
                            • J JadeN001
                              29 Jun 2018, 10:22

                              @mrjj thanks..
                              It does not auto increase row count

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 29 Jun 2018, 10:49 last edited by
                              #14

                              @JadeN001
                              ok, so u have to manage that yourself

                              JonBJ 1 Reply Last reply 29 Jun 2018, 11:51
                              0
                              • M mrjj
                                29 Jun 2018, 10:49

                                @JadeN001
                                ok, so u have to manage that yourself

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 29 Jun 2018, 11:51 last edited by
                                #15

                                @mrjj , @JadeN001

                                It does not auto increase row count

                                You guys are claiming that QStandardItemModel::rowCount() remains unchanged after QStandardItemModel::appendRow()? Surely something is wrong here....?

                                M 1 Reply Last reply 29 Jun 2018, 16:10
                                2
                                • JonBJ JonB
                                  29 Jun 2018, 11:51

                                  @mrjj , @JadeN001

                                  It does not auto increase row count

                                  You guys are claiming that QStandardItemModel::rowCount() remains unchanged after QStandardItemModel::appendRow()? Surely something is wrong here....?

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 29 Jun 2018, 16:10 last edited by
                                  #16

                                  @JonB
                                  Nope, i said check it as it really sound it would. ( append in name)
                                  But docs says
                                  "Appends a row containing items. If necessary, the column count is increased to the size of items."
                                  And no mention of rowCount.
                                  But i really suspect it would so if you says it does, i believe you over the docs.

                                  JonBJ 1 Reply Last reply 29 Jun 2018, 17:40
                                  0
                                  • M mrjj
                                    29 Jun 2018, 16:10

                                    @JonB
                                    Nope, i said check it as it really sound it would. ( append in name)
                                    But docs says
                                    "Appends a row containing items. If necessary, the column count is increased to the size of items."
                                    And no mention of rowCount.
                                    But i really suspect it would so if you says it does, i believe you over the docs.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 29 Jun 2018, 17:40 last edited by
                                    #17

                                    @mrjj

                                    "Appends a row containing items. If necessary, the column count is increased to the size of items."
                                    And no mention of rowCount.

                                    My belief is that this should be interpreted as: of course the row count increases, as it would for anything, that goes without saying; if the columns in the row exceed current columns then you might not realize but the column count is increased [too].

                                    The only other interpretation is that another is removed to maintain the row count, but then I think it would say and you would notice.

                                    But it's pure speculation.

                                    M 1 Reply Last reply 29 Jun 2018, 17:43
                                    1
                                    • JonBJ JonB
                                      29 Jun 2018, 17:40

                                      @mrjj

                                      "Appends a row containing items. If necessary, the column count is increased to the size of items."
                                      And no mention of rowCount.

                                      My belief is that this should be interpreted as: of course the row count increases, as it would for anything, that goes without saying; if the columns in the row exceed current columns then you might not realize but the column count is increased [too].

                                      The only other interpretation is that another is removed to maintain the row count, but then I think it would say and you would notice.

                                      But it's pure speculation.

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 29 Jun 2018, 17:43 last edited by
                                      #18

                                      @JonB
                                      Also my belief so i wanted poster to test it out :)
                                      by outputting rowCount before and after append.

                                      JonBJ 1 Reply Last reply 29 Jun 2018, 17:44
                                      1
                                      • M mrjj
                                        29 Jun 2018, 17:43

                                        @JonB
                                        Also my belief so i wanted poster to test it out :)
                                        by outputting rowCount before and after append.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on 29 Jun 2018, 17:44 last edited by
                                        #19

                                        @mrjj
                                        I find it hard to believe it did not increase. Unless the row did not get added.

                                        M 1 Reply Last reply 29 Jun 2018, 17:51
                                        1
                                        • JonBJ JonB
                                          29 Jun 2018, 17:44

                                          @mrjj
                                          I find it hard to believe it did not increase. Unless the row did not get added.

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 29 Jun 2018, 17:51 last edited by
                                          #20

                                          @JonB
                                          me too. Worst name ever if it didnt.
                                          So most likely poster had other issue.

                                          1 Reply Last reply
                                          0

                                          4/20

                                          25 Jun 2018, 21:32

                                          16 unread
                                          • Login

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