Hi Qt lovers,
I am still in the learning phase of Model/View...
I have a class derived from QAbstractTableModel where I would add a QString to a specific cell at a specific time.
Internally the data of the model is stored in a QVector called m_reports but additionally I need to store some data that I need to retrieve from the MainWindow in a doubleclick event.
I though that setData is the correct function for that but I can't get it to work and I don't understand why.
Below the function that feed the model data
void SelectedListModel::appendReport(const QMultiMap<QString, QString> &report)
{
int row = m_reports.count();
QString xmlFile
= report.
value("xmlFilePath");
qDebug() << xmlFile; // xmlFile contains right value
setData
(index
(row,
0),
QVariant(xmlFile
), Qt
::UserRole + 1);
// <-- this does not work, no data is written m_reports.append(report);
endInsertRows();
}
void SelectedListModel::appendReport(const QMultiMap<QString, QString> &report)
{
int row = m_reports.count();
beginInsertRows(QModelIndex(), row, row);
QString xmlFile = report.value("xmlFilePath");
qDebug() << xmlFile; // xmlFile contains right value
setData(index(row, 0), QVariant(xmlFile), Qt::UserRole + 1); // <-- this does not work, no data is written
m_reports.append(report);
endInsertRows();
}
To copy to clipboard, switch view to plain text mode
In my MainWindow class, I catch the double-clicked event over the table view and I would get the value written with setData
?
connect(ui
->selectedItemsView,
SIGNAL(doubleClicked
(const QModelIndex &)),
this,
SLOT(showConfigureDialog
(const QModelIndex &)));
connect(ui->selectedItemsView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(showConfigureDialog(const QModelIndex &)));
To copy to clipboard, switch view to plain text mode
And the relative slot from where I would get the value I need to process
void MainWindow
::showConfigureDialog(const QModelIndex &index
) {
if (index.column() == 1)
{
return;
}
QString xmlFilePath
= model
->data
(model
->index
(index.
row(),
0), Qt
::UserRole+1).
toString();
qDebug() << "Cell Value: " << xmlFilePath; // This gives an empty output
}
void MainWindow::showConfigureDialog(const QModelIndex &index)
{
if (index.column() == 1)
{
return;
}
const QAbstractItemModel *model = index.model();
QString xmlFilePath = model->data(model->index(index.row(), 0), Qt::UserRole+1).toString();
qDebug() << "Cell Value: " << xmlFilePath; // This gives an empty output
}
To copy to clipboard, switch view to plain text mode
I can not understand what can be the problem.
I hope to get some help.
Franco
Bookmarks