PDA

View Full Version : qtableview add row on buttonclicked



Surfman19
7th July 2015, 10:45
Hello,

How can i add a new row with 2 colums to a qtableview when a button was clicked?

I also plan to remove a single selected row on a button click.

I will have max. 20 rows aka 2 colums. Does it make sense to use a qtableview here or should i use a qtablewidget?

Thanks,
Surf

jefftee
7th July 2015, 10:53
In my opinion, your use case is simple enough that QTableWidget is sufficient. If you will be working with Qt and taking on more and more complex data structures and views, then the model/view paradigm will provide more flexibility and more control, etc.

So, do you want down and dirty for this one use case or do you want to invest some time to learn Qt's model/view classes which you can leverage for more complex projects down the road?

Surfman19
7th July 2015, 11:17
thanks for the quick reply.

i want to investigate more time with the model/view approach.

i currently use a QTableView with a QStandardItemModel.

once i create a new QStandardItem, will i need to manually delete the memory after having it allocated on the heap?

that how i do:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

model = new QStandardItemModel(0,2);

ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setModel(model);
ui->tableView->setVerticalScrollBar(new QScrollBar(ui->tableView));
ui->tableView->show();

QObject::connect(ui->pushButton_add, SIGNAL(clicked()), this, SLOT(onAddClicked()));
QObject::connect(ui->pushButton_remove, SIGNAL(clicked()), this, SLOT(onRemoveClicked()));
}

void MainWindow::onAddClicked()
{
qDebug() << "onAddClicked...";

QList<QStandardItem*> newRow;
QStandardItem *item1 = new QStandardItem(QString("col1"));
QStandardItem *item2 = new QStandardItem(QString("col2"));
newRow.append(item1);
newRow.append(item2);
model->appendRow(newRow);
}

void MainWindow::onRemoveClicked()
{
qDebug() << "onRemoveClicked...";

QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex();
model->removeRow(currentIndex.row());
}


will the QTableView always automatically update when i update the QStandardItemModel?

anda_skoa
7th July 2015, 11:59
once i create a new QStandardItem, will i need to manually delete the memory after having it allocated on the heap?

Only if you don't add them to the model. If you do, the model takes ownership.



will the QTableView always automatically update when i update the QStandardItemModel?
Yes, the model will signal all views operating on it when it has changed.

Cheers,
_

Surfman19
8th July 2015, 09:28
qtableview works fine!

i currently use the QStandardItemModel. are there any other models available and how the differ?

jefftee
8th July 2015, 09:37
I suggest that you do a little research regarding Qt's model/view classes. If you have specific questions regarding using the model/view classes, then come back here once you have read the docs and attempted to write some code.