Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    9
    2309
    Loading More Posts
    • 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
      pnmrvvtl last edited by pnmrvvtl

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 1
        • P
          pnmrvvtl @Chris Kawa last edited by pnmrvvtl

          @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 Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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 Reply Quote 1
            • P
              pnmrvvtl @Chris Kawa last edited by

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

              1 Reply Last reply Reply Quote 0
              • Chris Kawa
                Chris Kawa Moderators last edited by

                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 Reply Quote 0
                • P
                  pnmrvvtl @Chris Kawa last edited by

                  @Chris-Kawa i use QStandardItemModel.

                  1 Reply Last reply Reply Quote 0
                  • Chris Kawa
                    Chris Kawa Moderators last edited by

                    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 Reply Quote 1
                    • P
                      pnmrvvtl @Chris Kawa last edited by

                      @Chris-Kawa thank you for help.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post