PDA

View Full Version : QSQLITE database, delete rows, frozen columns



carvi
10th April 2011, 21:12
Hello,

I am working on the project in QT using QSQLITE database. I have a table that contains informations about the cars. I've created a QSqlTableModel object and set it in QTableView object. I want to click on a cell(mark it) in the table, push button "delete" and delete whole row which contains that cell. I've no idea how to do it. I thought about emitting signal "activated" from QTableView object. Unfortunately it doesn't work.

connect(ui->tablecarsv,SIGNAL(activated(QModelIndex)),this, SLOT(do23 (QModelIndex)))

Next thing: How to set "read only" or frozen for column in table that is displays in QTableView?

Thanks in advance:)

viulskiez
11th April 2011, 02:11
Hi carvi,


I thought about emitting signal "activated" from QTableView object
Read the documentation of QAbstractItemView::activated(const QModelIndex& index) if you want to know when it emitted. But if you want a simple solution, try this:

Connect your button to a slot
Then in the body of the slot, use selection model form your view to find selected items, so you can delete these items


The code would be something like this:


// connect to a slot
connect(removeButton, SIGNAL(clicked()), this, SLOT(removeItems()));

.....

// slot definitions
void MyWidget::removeItems()
{
QItemSelectionModel *selectionModel = view->selectionModel();

// ex: delete first row in selection
QModelIndex index = selectionModel->selectedRows().first();
tableModel->removeRow(index.row());
tableModel->submit();
}



Next thing: How to set "read only" or frozen for column in table that is displays in QTableView?
I usually create a model that inherited from QSqlQueryModel, QSqlTableModel or QSqlRelationalTableModel, then reimplement data(), setData() and flags() method.

Qt Query Model Example (http://doc.trolltech.com/latest/sql-querymodel.html) is a good place to start creating your own custom SQL model.