Two images in column 0 in QtreeView followed by a text



  • I have a QtreeView
    is it possible to have two images side by side in column 0
    and then followed by a text in the same column



  • Of course!

    You can even merge the two images in 1 and save it in decoration role or save the 2 images in 2 different role and reimplement paint() in a QStyledItemDelegate subclass



  • Can u please code for
    ou can even merge the two images in 1 and save it in decoration role for the same for qt.4.3.3 version



  • QPixmap image1(":/Resources/Icon1.png");
    QPixmap image2(":/Resources/Icon2.png");
    
    QPixmap merged(image1.width()+image2.width(),qMax(image1.height(),image2.height());
    merged.fill(QColor(0,0,0,0));
    QPainter painter(&merged);
    painter.drawPixmap(QRect(0,0,image1.width(),image1.height()),image1,image1.rect()));
    painter.drawPixmap(QRect(image1.width(),0,image2.width(),image2.height()),image2,image2.rect()));
    
    model->setData(model->index(0,0),merged,Qt::DecorationRole);
    


  • I had a similar problem.
    If You are using QStandardItemModel, then my advice is to use a custom delegate, like in the Qt star delegate example (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html).
    Then reimplement the paint method, so that it handles two images.

    Mine looks like that:

    void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(index.column() == 2) {
        QStyleOptionViewItem options = option;
        initStyleOption(&options, index);
    
        painter->save();
    
        QString contactName = index.data(Qt::DisplayRole).toString();
        QString contactPhone = index.data(Qt::UserRole).toString();
        QString contactInfo = index.data(Qt::UserRole+1).toString();
    
        QTextDocument textDoc;
    
        QTextCursor textCursor(&textDoc);
    
        QImage stanPoint = index.data(Qt::UserRole+2).value<QImage>();
        bool wczytajfote = !stanPoint.isNull();
    
        textCursor.insertHtml(contactName);
        if(wczytajfote){
        textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
        textCursor.insertImage(stanPoint);
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        }
        textCursor.insertHtml(contactPhone);
        textCursor.insertHtml(contactInfo);
    
        options.text = "";
        options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
    
        painter->translate(options.rect.left(), options.rect.top());
    
        QRect clip(0,0,options.rect.width(), options.rect.height());
    
        textDoc.setTextWidth(clip.width());
    
        textDoc.drawContents(painter, clip);
        painter->restore();
        option.palette.color(QPalette::Background/*Highlight*/));
      }


  • @VRonin

    Hi Tried
    I tried this
    QPixmap image1 = QApplication::style()->standardPixmap(QStyle::SP_DirIcon);

    QPixmap pixmap(image1.width()-0.5,image1.height()-0.5);
    pixmap.fill(QColor(sc));
    QPainter painter(&pixmap);
    QPen pen(QColor(55,0,0));
    pen.setStyle(Qt::SolidLine);
    painter.setPen(pen);
    painter.drawRect(0,0,image1.width()-0.5,image1.height()-0.5);

    QPixmap image2 = pixmap;
    QPixmap merged(image1.width()+image2.width()+5,qMax(image1.height(),image2.height())+10);
    merged.fill(QColor(255,255,255,0));
    
    QPainter painter1(&merged);
    painter1.drawPixmap(QRect(0,0,image1.width(),image1.height()),image1,image1.rect());
    painter1.drawPixmap(QRect(image1.width()+5,0,image2.width(),image2.height()),image2,image1.rect());
    QPainter painter(&merged);
    painter.drawPixmap(QRect(0,0,image1.width(),image1.height()),image1,image1.rect());
    painter.drawPixmap(QRect(image1.width(),0,image2.width(),image2.height()),image2,image1.rect());
      return QIcon(merged);
    

    but the final image is shrinked can someone guide me how to get perfect merged images with some distanced between two images


Log in to reply
 

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