Как добавить несколько значков к элементу дерева в модели дерева - PullRequest
0 голосов
/ 16 июня 2020

Это моя текущая функция данных модели дерева

QVariant TreeModel::data(const QModelIndex &index, int role) const
{

    if (!index.isValid())
        return QVariant();

    TreeItem *item = getItem(index);
    if (item)
    {
        switch (role)
        {
            case Qt::DisplayRole: case Qt::EditRole:
                return QString::fromStdString(item->data().GetName());
                break;
            case Qt::DecorationRole:
            {                           
                Container *cont = item->GetContainer();
                if (cont->GetGeometry()->isValid())
                {                           
                    QString qstrIconName =  cont->GetGeometry()->GetType().c_str();
                    QString qstrIconPath = QCoreApplication::applicationDirPath();
                    QPixmap pixmap;
                    QIcon icon;                 
                    qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
                    pixmap.load(qstrIconPath);
                    icon.addPixmap(pixmap, QIcon::Normal, QIcon::On);               
                    return icon;
                }
                 // Function returns from here since we have returned the icon
                 // How can i add next icon to the same tree item

                int numberOfFunctions = cont->getNumberOfFunctions();
                if (numberOfFunctions > 0)
                {
                    QString qstrIconName = "FUNCTION";
                    QString qstrIconPath = QCoreApplication::applicationDirPath();
                    QPixmap pixmap;
                    QIcon icon;
                    qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
                    pixmap.load(qstrIconPath);
                    icon.addPixmap(pixmap, QIcon::Normal, QIcon::On);
                    return icon;
                }
                break;              
            }
        }       
        return QVariant();
    }
}

///////////////////////////// ////////////////////////////////////////////////// ///

Я могу установить только значок, как я могу установить несколько значков в элементе дерева.

Я могу установить любой из значков в модели дерева, но не обе иконки.

1 Ответ

0 голосов
/ 18 июня 2020

Я использовал QStyledItemDelegate, чтобы добиться этого, и это функция рисования

void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option , const QModelIndex &index) const
{
    int offset = 0;
    int offsetIcon = 0;
    const QAbstractItemModel *model = index.model();
    const TreeModel *myModel = (TreeModel*)(model);
    TreeItem* item = myModel->getItem(index);
    Container* cont = item->GetContainer(); 
    if (option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(Qt::NoPen);
    painter->translate(option.rect.x(), option.rect.y());
    painter->scale(.4, .4);
    if (option.state & QStyle::State_Selected)
        painter->setBrush(option.palette.highlightedText());
    else
        painter->setBrush(option.palette.text());

    if (cont->GetGeometry()->GetType() != "NO_TYPE")
    {
        QString qstrIconName = cont->GetGeometry()->GetType().c_str();
        QString qstrIconPath = QCoreApplication::applicationDirPath();
        QPixmap pixmap;
        qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
        pixmap.load(qstrIconPath);
        QImage image = pixmap.toImage();
        painter->drawImage(0, 6, image);
        offset += 20;
        offsetIcon += 50;
    }
    if (cont->getNumberOfFunctions() > 0)
    {
        QString qstrIconName = "FUNCTION";
        QString qstrIconPath = QCoreApplication::applicationDirPath();
        QPixmap pixmap;
        qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
        pixmap.load(qstrIconPath);
        QImage image = pixmap.toImage();
        painter->drawImage(offsetIcon, 6, image);
        offset += 20;
        offsetIcon += 50;
    }
    if (cont->GetNumberOfAnimationChannels() > 0)
    {
        QString qstrIconName = "ANIMATION";
        QString qstrIconPath = QCoreApplication::applicationDirPath();
        QPixmap pixmap;
        qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
        pixmap.load(qstrIconPath);
        QImage image = pixmap.toImage();
        painter->drawImage(offsetIcon, 6, image);
        offset += 20;
    }           
    painter->restore();
    QStyleOptionViewItem newoption = option;
    newoption.rect.setX(option.rect.x() + offset + 3);  
    newoption.rect.setY(option.rect.y());
    QStyledItemDelegate::paint(painter, newoption, index);
}
...