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



  • 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


  • Moderators

    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.



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

  • Moderators

    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");
    }
    


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


  • Moderators

    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?



  • @Chris-Kawa i use QStandardItemModel.


  • Moderators

    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.



  • @Chris-Kawa thank you for help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.