2013-02-20 6 views
3

나는 그러나 단지 루트 노드의 아이콘을 표시하는 QTreeWidgetQTreeWidgetItem있는 항목을 가지고?내 QTreeWidgetIcons는 어디에 있습니까?</p> <p><img src="https://i.stack.imgur.com/KMI3t.png" alt="enter image description here"></p> <p>내가 무엇을 끌 수있는 내 머리를 긁적 봤는데, 어떤 힌트 :

newItem->setData(0, 1, QVariant(leftPath + QDir::separator() + subDir)); 

두 번째 인수는 1 (Qt는 :: DecorationRole)로 지정하는 항목 데이터 역할이다

ui->folderTree1->setUpdatesEnabled(false); 

    QTreeWidgetItem* treeRoot1 = new QTreeWidgetItem(ui->folderTree1); 
    treeRoot1->setIcon(0, QIcon(":/icons/black.png")); 
    treeRoot1->setText(0, tr("Root")); 
    treeRoot1->setExpanded(true); 
    addFoldersToTreeView(treeRoot1, ui->filePath1->text(), ui->filePath2->text()); 

    ui->folderTree1->setUpdatesEnabled(true); 
} 

void MainWindow::addFoldersToTreeView(QTreeWidgetItem* currentWidget, QString leftPath, QString rightPath) 
{ 
    qDebug() << "MainWindow::addFoldersToTreeView" << leftPath; 
    QDir dir(leftPath); 
    QDir dir2(rightPath); 
    /* Add the folders */ 
    foreach (QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) 
    { 
    QString sImageName = ":/icons/black.png"; 
    QTreeWidgetItem* newItem = new QTreeWidgetItem(currentWidget); 
    qDebug() << "MainWindow::addFoldersToTreeView.sImageName" << sImageName; 
    newItem->setIcon(0, QIcon(sImageName)); 
    newItem->setText(0, subDir); 
    newItem->setExpanded(true); 
    newItem->setData(0, 1, QVariant(leftPath + QDir::separator() + subDir)); 
    /* Recursively add sub-folders */ 
    addFoldersToTreeView(newItem, leftPath + QDir::separator() + subDir, rightPath + QDir::separator() + subDir); 
    } 

답변

6

문제는 라인입니다. Qt :: DecorationRole은 아이콘 형태로 꾸미기로 렌더링되는 데이터에 사용되어야합니다. 즉,이 행은 지정한 아이콘이 QVariant 객체로 대체되도록합니다.

해당 행을 제거하거나 항목 데이터 역할을 다른 것으로 변경하십시오.