Add form widget in QTableview using model view



  • 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.



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



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


  • Lifetime Qt Champion

    Hi,

    Can you show your Delegate class code ?



  • @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);
    
    }
    
    


  • how to add row and column as needed in Qtableview


  • Lifetime Qt Champion

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



  • @SGaist okay It means I have to manage model.



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


  • Qt Champions 2017

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



  • @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.


  • Qt Champions 2017

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



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


  • Qt Champions 2017

    @JadeN001
    ok, so u have to manage that yourself



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


  • Qt Champions 2017

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



  • @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.


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.