I have a database consisting of a table with some fields. I am using QSqlTableModel to get the data from the database and displaying it in a QTableView. Now, the database has 3 fields i.e. tracks, artist and time. All of the data in these fields is coming from the model and being displayed properly.
I have added another column to the QTableView consisting of QCheckBox for each row (See attached image). The QCheckBox is not part of the database or model. I am using the following code for attaching QCheckBox to each row:
{
for (int i = 0; i < m_pModel->rowCount(); ++i) {
// Enable autofill of the widget background to hide the model data
cbox->setAutoFillBackground(TRUE);
// Set up slots
connect(cbox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxClicked(int)));
// Add checkboxes in the 4th column of QTableView
table->setIndexWidget(m_pModel->index(i, 3), cbox);
}
}
void MyClass::createCheckItems(QTableView *table)
{
for (int i = 0; i < m_pModel->rowCount(); ++i) {
QCheckBox *cbox = new QCheckBox(QObject::tr (""));
// Enable autofill of the widget background to hide the model data
cbox->setAutoFillBackground(TRUE);
// Set up slots
connect(cbox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxClicked(int)));
// Add checkboxes in the 4th column of QTableView
table->setIndexWidget(m_pModel->index(i, 3), cbox);
}
}
To copy to clipboard, switch view to plain text mode
I am able to retrieve the current row and get the track name when the user clicks on a check box.
My queries:
1) I am using setIndexWidget() to display checkboxes in the 4th column and my code is working fine. Is it ok or should i use QItemDelegate to add checboxes?
2) Currently the QTableView displays data directly received from the model (database). But suppose, i want to format the data received from the model before displaying on the QTableView, how can i do it? For e.g. suppose the "time" field is stored as seconds in the database and i want to display it in "minutes:seconds" in the QTableView, how can i do that?
3) I want to do custom drawing in the "tracks" field, for e.g. the currently playing track might have an icon next to the track name. How to do that?
Thanks in advance!
Bookmarks