I have a tree model and i want to get all indices as QModelIndexList from the model.
But i am not able to find any function in the tree model which returns all the indices in the model.
Do i have to manually iterate through the indices in the tree model to get all the indices ?
void iterate(const QModelIndex & index, const QAbstractItemModel * model)
{
if (index.isValid())
{
// Add to list
}
if (!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren))
{
return;
}
auto rows = model->rowCount(index);
for (int i = 0; i < rows; ++i)
iterate(model->index(i, 0, index), model);
}
Related
I have a recursive function to find a treeitem in the treeview from its name.
bool SumCommandInterface::getTreeItem(const std::string &stdstrName, const QModelIndex & index, TreeModel *model, TreeItem** item)
{
if (index.isValid())
{
TreeItem* currentTreeItem = model->getItem(index);
if (currentTreeItem->getName() == stdstrName)
{
*item = currentTreeItem;
return true;
}
}
if(!model->hasChildren(index) || (index.flags() & Qt::ItemNeverHasChildren))
{
return false;
}
auto rows = model->rowCount(index);
for (int i = 0; i < rows; ++i)
getTreeItem(stdstrName , model->index(i, 0, index), model , item );
return false;
}
The function still runs even after it satisfies the condition.
The problem in you code is that you don't return from the function even if a recursive function call returns true, i.e. when the condition is met. The right way to implement the recursive call would be:
[..]
for (int i = 0; i < rows; ++i)
// Return if the condition is met.
if (getTreeItem(stdstrName , model->index(i, 0, index), model, item)) {
return true;
}
[..]
Currently i am iterating through all the child items of a Tree item using this code.
void iterate(const QModelIndex & index, const QAbstractItemModel * model)
{
if (index.isValid())
{
// Do action here
}
if (!model->hasChildren(index) || (index.flags() &
Qt::ItemNeverHasChildren))
{
return;
}
auto rows = model->rowCount(index);
for (int i = 0; i < rows; ++i)
iterate(model->index(i, 0, index), model);
}
i need to Iterate using TreeItem instead of index
void iterate(TreeItem *item, const QAbstractItemModel * model)
One method i can think of is getting the index of the item using createIndex function and the above code will work with it.
Is there any other method to do so ?
If you are using the QStandardItemModel you can use the indexFromItem methods:
QModelIndex QStandardItemModel::indexFromItem(const QStandardItem *item) const
In case you are using your own implementation of you might provide your own indexFromItem and itemFromIndex methods.
Another solution might involve calling QAbstractItemModel::createIndex.
I want to fire QAbstractItemView::doubleClicked slot programaticaly for an item that has specific text. I want to do this using QAbstractItemView class and not it's implementations if possible.
This task boils down to looping over items and comparing strings. But I cannot find any method that would give me all QModelIndexes. The only method that gives any QModelIndex without parameters is QAbstractItemView::rootIndex. But when I look into QModelIndex docs, I again cannot see a way to access it's children and siblings.
So how to access all QModelIndexes in QAbstractItemView?
The indexes are provided by the model, not by the view. The view provides the rootIndex() to indicate what node in the model it considers as root; it might be an invalid index. Otherwise it has nothing to do with the data. You have to traverse the model itself - you can get it from view->model().
Here's a depth-first walk through a model:
void iterate(const QModelIndex & index, const QAbstractItemModel * model,
const std::function<void(const QModelIndex&, int)> & fun,
int depth = 0)
{
if (index.isValid())
fun(index, depth);
if ((index.flags() & Qt::ItemNeverHasChildren) || !model->hasChildren(index)) return;
auto rows = model->rowCount(index);
auto cols = model->columnCount(index);
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
iterate(model->index(i, j, index), model, fun, depth+1);
}
The functor fun gets invoked for every item in the model, starting at root and going in depth-row-column order.
E.g.
void dumpData(QAbstractItemView * view) {
iterate(view->rootIndex(), view->model(), [](const QModelIndex & idx, int depth){
qDebug() << depth << ":" << idx.row() << "," << idx.column() << "=" << idx.data();
});
}
I have a matrix of data, I simply stored it as a QList of QStringLists, all containing an equal number of QStrings. In this way, the data looks almost like a spreadsheet.
I use a QTableView to present this data to the user:
void DialogwitQTableView::setData(QList<QStringList> s)
{
/* Create the data model */
// 1. give it some headers
QStandardItemModel model = new QStandardItemModel(s.count(),25,this); //x Rows and 25 Columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column 1")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column 2")));
// ...
model->setHorizontalHeaderItem(24, new QStandardItem(QString("Column 25")));
// 2. populate the model with the data
for(int i = 0; i < s.count() ; i++)
{
for(int j = 0; j < s[i].count() ; j++)
model->setItem(i,j,new QStandardItem(QString(s[i][j])));
}
ui->NameOfTheTableView->setModel(model);
}
Now, if the user wants to change some of the data, he will just doubleclick in the QTableView in the Dialogbox and edits what he wants.
How do I get that edit also in the data? How can I adapt the QStringList with that new information?
If I search for documentation, I mostly find QTableViews linked to databases, but I don't see how this will work with a simple datastructure in memory. If I go to QtDesigner and click on "go to slots" for the TableView, I also do not see a slot called "datachanged" or anything similar.
Any thoughts? I feel pretty stuck and I am probably overviewing something, any tip is very welcome.
Looking at the doco, a QTableView inherits 6 signals from QAbstractItemView
http://doc.qt.digia.com/qt/qabstractitemview.html#signals
This class has all sorts of functionality for capturing edits, and edit triggers.
Once you can catch when the data is changed you can recommit it back to your model if you are using an MVC view. I am sure there are a lot of examples.
Hope that helps.
I think that for more complicated cases it's always best to use the abstract classes, more specifically QAbstractTableModel in this case.
Looking at this file, I just replaced Contact with StringList and changed the getters and setters. Check it out:
https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-tablemodel-cpp.html
TableModel::TableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
TableModel::TableModel(QList<QStringList> stringLists, QObject *parent) :
QAbstractTableModel(parent),
stringLists(stringLists)
{
}
int TableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return stringLists.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2; // put the amount of columns here
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= stringLists.size() || index.row() < 0) return QVariant();
if (role == Qt::DisplayRole) {
const auto &strings = stringLists.at(index.row());
if (index.column() == 0)
return strings[0];
else if (index.column() == 1)
return strings[1];
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("String 1");
case 1:
return tr("String 2");
default:
return QVariant();
}
}
return QVariant();
}
bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
stringLists.insert(position, { QString(), QString() });
endInsertRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
stringLists.removeAt(position);
endRemoveRows();
return true;
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();
auto strings = stringLists.at(row);
if (index.column() == 0)
strings[0] = value.toString();
else if (index.column() == 1)
contact[1] = value.toString();
else
return false;
stringLists.replace(row, contact);
emit dataChanged(index, index, {role});
return true;
}
return false;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
QList<QStringList> TableModel::getStringLists() const
{
return stringLists;
}
I also highly recommend that you read this:
https://doc.qt.io/qt-5/modelview.html
Hope it helps.
If I search for documentation, I mostly find QTableViews linked to
databases, but I don't see how this will work with a simple
datastructure in memory.
QTableView is a part of Qt's Model/View framework. There are bunch of examples of model-views.
How do I get that edit also in the data? How can I adapt the
QStringList with that new information?
At least these solutions exists:
You can grab all data from QStandardItemModel via item method.
Connect to QStandardItemModel::itemChanged signal.
You can make your own model via subclassing (and I suggest to base on QAbstractTableModel) and implement several methods (data, setData + several utility methods).
I'm trying to return a vector of selected rows from a QTableView widget(snippet copied below), however the values returned do not correspond to the selection and I believe I've not understood QModelIndexList/QModelIndex with respect to QTableView. Can you let me know where I'm wrong or the right way to access selected items from a QTableView?
C_model is of type QStandardItemModel
for(int i = 0; i < c_model->rowCount(); i++)
{
if (selectionModel->isRowSelected(i, QModelIndex()))
{
QStringList selection;
std::vector<std::string> currentIndexValues;
for (int j = 0; j < c_model->columnCount(); j++)
{
QVariant q = c_model->data(c_model->index(i, j));
selection.append(q.toString());
currentIndexValues.push_back(q.toString().toLocal8Bit().constData());
printf(" %s\t ", currentIndexValues[j].c_str());
}
printf("\n");
v_selectedItems.push_back(currentIndexValues);
}
}
Thanks
QAbstractItemView (the base class of QTableView) offers a QItemSelectionModel for this purpose. You can access that model via QTableView::itemSelectionModel() and then retrieve the selected rows via QItemSelectionModel::selectedRows():
QModelIndexList selectedRows = yourTableView->selectionModel()->selectedRows();
foreach( QModelIndex index, selectedRows )
{
int row = index.row();
}