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. [SOLVED] How can I get row and col of my child in QTreeView?

[SOLVED] How can I get row and col of my child in QTreeView?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.7k Views
  • 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.
  • P Offline
    P Offline
    pnmrvvtl
    wrote on last edited by pnmrvvtl
    #1

    I have QTreeView, if i click on first row signal clicked(QModelIndex) return row ==0 and col == 0, and if i clicked on first child in second row clicked(QModelIndex) return the same result. How i can know that i pick first child in second row neither first row?
    alt text
    alt text

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      row indicates a row in relation to the parent. Check parent of the index. If it isValid then it means you're in a child. Check the row of the parent to see where it is.

      P 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        row indicates a row in relation to the parent. Check parent of the index. If it isValid then it means you're in a child. Check the row of the parent to see where it is.

        P Offline
        P Offline
        pnmrvvtl
        wrote on last edited by pnmrvvtl
        #3

        @Chris-Kawa thanks, its works.
        here is code for solving:

        .........
             QObject::connect(ui->contentTree,SIGNAL(clicked(QModelIndex)),this,SLOT(setHelpFile(QModelIndex)));
        .........
           void QHelpDialog::setHelpFile(QModelIndex argIndex)
        {
           QModelIndex index = argIndex.parent();
           //  QMessageBox msg;
           if(index.isValid())
               //This is child
               switch(argIndex.row()) {
               case 0:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/1.html")));
                   break;
               case 1:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/2.html")));
                   break;
               case 2:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/3.html")));
                   break;
               case 3:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/4.html")));
                   break;
               case 4:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/5.html")));
                   break;
               case 5:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/6.html")));
                   break;
               default:
                   throw QString("Wrong item in help window");
               }
           else
               //This is not child
               switch(argIndex.row()) {
               case 0:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/7.html")));
                   break;
               case 1:
                   ui->contentBrowser->setSource(QUrl(QStringLiteral("qrc:/8.html")));
                   break;
               default:
                   throw QString("Wrong item in help window");
               }
           //msg.exec();
        }
        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'm not sure this is a good way to do this. If later on you decide to add one row at the top or add another level to some item you will have to re-number everything. Not to mention that the switch/case will grow.

          Consider adding the url as item data with setData. In that case the above code would become a lot simpler and tremendously easier to maintain:

          void QHelpDialog::setHelpFile(QModelIndex argIndex) {
              QUrl url = argIndex.data(Qt::UserRole).toUrl();
              if(url.isValid())
                  ui->contentBrowser->setSource(url);
              else
                 throw QString("Wrong item in help window");
          }
          
          P 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            I'm not sure this is a good way to do this. If later on you decide to add one row at the top or add another level to some item you will have to re-number everything. Not to mention that the switch/case will grow.

            Consider adding the url as item data with setData. In that case the above code would become a lot simpler and tremendously easier to maintain:

            void QHelpDialog::setHelpFile(QModelIndex argIndex) {
                QUrl url = argIndex.data(Qt::UserRole).toUrl();
                if(url.isValid())
                    ui->contentBrowser->setSource(url);
                else
                   throw QString("Wrong item in help window");
            }
            
            P Offline
            P Offline
            pnmrvvtl
            wrote on last edited by
            #5

            @Chris-Kawa how i can set html document for every item in model, and how to use Qt::UserRole?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I don't want to lead you in the wrong direction so first can you tell what kind of model do you use? Are you subclassing QAbstractItemModel and implementing data() or use one of the ready ones?

              P 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                I don't want to lead you in the wrong direction so first can you tell what kind of model do you use? Are you subclassing QAbstractItemModel and implementing data() or use one of the ready ones?

                P Offline
                P Offline
                pnmrvvtl
                wrote on last edited by
                #7

                @Chris-Kawa i use QStandardItemModel.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then you can use setItemData to set any additional info on an item you need. For example:

                  QMap<int, QVariant> data;
                  
                  //these are built-in roles:
                  data[Qt::DisplayRole] = "Hello world";
                  data[Qt::BackgroundRole] = QBrush(Qt::red);
                  //these are custom roles:
                  data[Qt::UserRole] = QUrl("qrc:/42.html");
                  data[Qt::UserRole + 1] = 42;
                  data[Qt::UserRole + 2] = QPixmap(":/a_picture_of_42.png");
                  
                  model->setItemData(someItemIndex, data);
                  

                  To read them back use data() member of the model index, like in my previous example.

                  P 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    Then you can use setItemData to set any additional info on an item you need. For example:

                    QMap<int, QVariant> data;
                    
                    //these are built-in roles:
                    data[Qt::DisplayRole] = "Hello world";
                    data[Qt::BackgroundRole] = QBrush(Qt::red);
                    //these are custom roles:
                    data[Qt::UserRole] = QUrl("qrc:/42.html");
                    data[Qt::UserRole + 1] = 42;
                    data[Qt::UserRole + 2] = QPixmap(":/a_picture_of_42.png");
                    
                    model->setItemData(someItemIndex, data);
                    

                    To read them back use data() member of the model index, like in my previous example.

                    P Offline
                    P Offline
                    pnmrvvtl
                    wrote on last edited by
                    #9

                    @Chris-Kawa thank you for help.

                    1 Reply Last reply
                    0

                    • Login

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