Unsolved QModelIndex Question
-
I was searching in qt about this class QModelIndex. But if have one practical question:
If use in one slot this class what will happen? i'm not understand very well the concept of this class so for example if do this:
connect (ui->ejemplo,SIGNAL(clicked(QModelIndex)),this,SLOT(elegirejemplo()));How model indexes refer to items in models? They are always static or only one value. Some other option for this? Thx again!
-
QModelIndex is a bridge between your "visual" representation and concrete data.
So when you click (let's assume we have a list) row 42, a QModelIndex is created which points to the data under this row. When you pass this in a signal-slot connection, the index and data stay the same. In your slow you can call row() and will get 42, and you can call internalPointer() and you will get the data.
-
@sierdzio said
So when you click (let's assume we have a list) row 42, a QModelIndex is created which points to the data under this row. When you pass this in a signal-slot connection, the index and data stay the same. In your slot you can call row() and will get 42, and you can call internalPointer() and you will get the data.
And QModelIndex holds the index and the information of the model locations which contains data.
As using the connect statement below
connect(ui->emplo,SIGNAL(clicked(QModelIndex)),this,SLOT(elegirejemplo(QModelIndex)));
if u select the respective row and need to get the data of the particular row.
You can use the below sample code.void ModelProgram::elegirejemplo(QModelIndex model)
{
int row = model.row();qDebug () << model.sibling(row,0).data().toString(); qDebug () << model.sibling(row,1).data().toString();
}
You can get the data of the row which you selected.
Thanks,
-
@Pradeep-Kumar if for example i want to select one qtreewidgetview how will be?
Example:connect(ui->emplo,SIGNAL(clicked(QModelIndex)),this,SLOT(elegirejemplo(QModelIndex))); void MainWindow::elegirejemplo() { //i do this if(ui->arbolejemplo->currentItem()->parent() != NULL) { // and then here i select the item } }
Maybe i can select the row like you said.
-
Hi,
Here is the link for the examples of ModelView for QTreeView
http://doc.qt.io/qt-4.8/modelview.html
And for QTreeView also u can follow the same steps once you have constructed the model,
and added to view.
When u select the row and the respective data which u want to get.void ModelProgram::elegirejemplo(QModelIndex model)
{
int row = model.row();qDebug () << "row data :" << model.sibling(row,0).data().toString() << endl; qDebug () << "row data :" << model.sibling(row,1).data().toString() << endl;
}