Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Two images in column 0 in QtreeView followed by a text

Two images in column 0 in QtreeView followed by a text

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.0k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          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);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Q 1 Reply Last reply
          2
          • michalosM Offline
            michalosM Offline
            michalos
            wrote on last edited by michalos
            #5

            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*/));
              }
            
            1 Reply Last reply
            1
            • VRoninV VRonin
              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);
              
              Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved