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.
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 :
But it display when i create that particullar cell.
So,Is there a way to create an Editor with delegate? -
Hi,
Can you show your
Delegate
class code ? -
#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); }
-
The QTableView shows what the model has to give. If the model says 8 column then the table view will show 8.
-
The QTableView shows what the model has to give. If the model says 8 column then the table view will show 8.
-
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?
-
Hi
You mean like
model=new QStandardItemModel(this);
model->setRowCount(8):
model->setColumnCount(2): -
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.
-
@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.
-
@JadeN001
Hi
you should test if
void QStandardItemModel::appendRow(const QList<QStandardItem *> & items)
does not auto increase row count also. -
@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. -
@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."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.
-
"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.
-
@JonB
Also my belief so i wanted poster to test it out :)
by outputting rowCount before and after append.