qt


How to loop over QAbstractItemView indexes?


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 (!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();
});
}

Related Links

Audio Recording from a microphone to a wav file using Qt
Is there a guarantee that no signals are delivered from a different thread after QObject::disconnect()?
Cursor shape changed even over an overlapping rectangle in QML
How do I set the minimum size of a QML widget?
Does Qt have combo boxes like Word?
**reading url from browser using qt**
Modify color QTreeWidget
OpenGL ARB_framebuffer_object extension is not available
i have a error with Notepad in QT
Qt PNG image not displaying
QListWidget collapsing/expanding of item's content
Adjust Brightness for qvideowidget inserted into the QGraphicsscene and QGraphicsView
Need to draw Transparent qimage that includes drawing of a circle using Qt
Need help connecting signal from thread to slot in GUI
Crash on using the opengl graphics system
UI problems when changing the screen-resolution

Categories

HOME
flask
firebase
powerbi
automapper
mpdf
ontouchlistener
website
swift2
google-search-console
gprs
scheduler
grocery-crud
ex
reselect
user-defined-types
string-formatting
salt-cloud
home
cmake-gui
batch-rename
eclipse-emf
firemonkey-style
mod-wsgi
opentext
router
tracing
offline-caching
tortoisehg
amazon-cloudtrail
basic-authentication
mdns
substance
netbios
series
private-key
devtools
dreamfactory
love2d
eclipse-gmf
ng-repeat
mars-simulator
utf
grails3.2.0
asihttprequest
swift2.3
sonarqube5.2
aws-kinesis-firehose
entropy
scala-collections
glimpse
1010
apache-modules
zend-server
ltrace
d3v4
gawk
metabase
tablespace
nsrunloop
coovachilli
piping
gulp-uglify
seadragon
ioctl
json-spirit
arbre
largenumber
nette
ambiguity
android-studio-import
domino-designer-eclipse
flow-js
mojo
jolie
cout
webmethod
radius-protocol
chartfx
cmath
cakeyframeanimation
jdom
dropshadow
infopath-2007
mygeneration
solandra
ninject-interception
pinax
downcasting
icon-language

Resources

Encrypt Message