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. How to make a Text wrap delegate use the treeviews stylesheet

How to make a Text wrap delegate use the treeviews stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
qstyleditemdeletreeviewstylesheet
6 Posts 2 Posters 1.3k Views
  • 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.
  • S Offline
    S Offline
    sailord
    wrote on 15 Jun 2020, 17:27 last edited by
    #1

    Hi, I'm am trying to get a text wrapping delegate to use the stylesheet I am using for the treeview but am not sure how to do that.

    The style sheet my treeview is using looks like this:

    QTreeView{outline:0;}
    QTreeView::item:selected{background-color: #D5E9F8;color: black;}
    QTreeView::item{padding:5px;border: 1px solid rgb(0,0,0);}
    QTreeView::item:has-children{border: 1px solid rgb(0,0,0);}
    

    My delegates paint method looks like this:

    void TextWrapDelegate::paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const {
        QString text = index.model()->data(index).toString();
        QTextDocument doc;
    
            doc.setDefaultFont(QFont("Times New Roman",16) );
            doc.setHtml(text);
            doc.setTextWidth(option.rect.width() );
            
            painter->save();
    
            painter->translate(option.rect.x(),option.rect.y() );
    
            doc.drawContents(painter);
    
            painter->restore();
    }
    

    The size hint function looks like this

    QSize TextWrapDelegate::sizeHint(const QStyleOptionViewItem& option,const QModelIndex& index) const {
        QString text = index.model()->data(index).toString();
        QTextDocument doc;
    
            doc.setHtml(text);
            doc.setTextWidth(option.rect.width() );
    
            return QSize(doc.idealWidth()+this->v_wrapExtra,doc.size().height()+30);
    }
    

    I get the text wrap I want, but it does not use the stylesheet of the treeview how can I get it to use it?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Jun 2020, 17:40 last edited by mrjj
      #2

      Hi
      Store you text In EditRole and have DiaplyRole empty
      Then call the QItemDelgate::paint(painter, option, index);
      to have it draw its normal stuff and then
      draw your text as you do now.

      You are not calling the normal drawing so it cant use style currently as its just your draw code.

      You can see here what a normal paint does
      https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qitemdelegate.cpp.html#393

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sailord
        wrote on 15 Jun 2020, 18:21 last edited by
        #3

        Ok, it sorta works, but if I tried to set the DiaplyRole to empty and the EditRole to have the text it did not work.

        So I had to set the text as UserRole to get that part to work.

        But now the item is not high enough to contain the text when it is wrapped. How can I fix that?

        M 1 Reply Last reply 15 Jun 2020, 18:45
        0
        • S sailord
          15 Jun 2020, 18:21

          Ok, it sorta works, but if I tried to set the DiaplyRole to empty and the EditRole to have the text it did not work.

          So I had to set the text as UserRole to get that part to work.

          But now the item is not high enough to contain the text when it is wrapped. How can I fix that?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 15 Jun 2020, 18:45 last edited by mrjj
          #4

          @sailord
          Hi Normally it would get that from Delegate sizeHint
          but it dont respect that ?
          Else you can use SizeHintRole on the model.

          I assume you just want to make them all a bit taller ?

          there is also
          tableView->verticalHeader()->setDefaultSectionSize()

          S 1 Reply Last reply 15 Jun 2020, 19:10
          2
          • M mrjj
            15 Jun 2020, 18:45

            @sailord
            Hi Normally it would get that from Delegate sizeHint
            but it dont respect that ?
            Else you can use SizeHintRole on the model.

            I assume you just want to make them all a bit taller ?

            there is also
            tableView->verticalHeader()->setDefaultSectionSize()

            S Offline
            S Offline
            sailord
            wrote on 15 Jun 2020, 19:10 last edited by
            #5

            @mrjj
            Found the problem the amount I added to the hight in the size hint was to small thanx for the help

            M 1 Reply Last reply 15 Jun 2020, 19:11
            1
            • S sailord
              15 Jun 2020, 19:10

              @mrjj
              Found the problem the amount I added to the hight in the size hint was to small thanx for the help

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 15 Jun 2020, 19:11 last edited by
              #6

              @sailord
              So it did use SizeHint but it was just returning too small value ?
              Super :)

              1 Reply Last reply
              0

              5/6

              15 Jun 2020, 19:10

              • Login

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