Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QModelIndex Question

QModelIndex Question

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeronimo
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      Pradeep KumarP 1 Reply Last reply
      3
      • sierdzioS sierdzio

        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.

        Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

        @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
        Qt,QML Developer

        J 1 Reply Last reply
        2
        • Pradeep KumarP Pradeep Kumar

          @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,

          J Offline
          J Offline
          Jeronimo
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            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;
            

            }

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved