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
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
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;
newRow.append(item1);
newRow.append(item2);
model->appendRow(newRow);
}
void MainWindow::onRemoveClicked()
{
qDebug() << "onRemoveClicked...";
QModelIndex currentIndex
= ui
->tableView
->selectionModel
()->currentIndex
();
model->removeRow(currentIndex.row());
}
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::SingleSelection);
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());
}
To copy to clipboard, switch view to plain text mode
will the QTableView always automatically update when i update the QStandardItemModel?
Bookmarks