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
QtWS25 Last Chance

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 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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 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?

        mrjjM 1 Reply Last reply
        0
        • S sailord

          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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          2
          • mrjjM mrjj

            @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 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

            mrjjM 1 Reply Last reply
            1
            • S sailord

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              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