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. [Solved] How to display subscript text in header of QTableview.

[Solved] How to display subscript text in header of QTableview.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 11.6k 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.
  • raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    this is not supported by Qt, you would need to implement a QStyledItemDelegate and set it to your QHeaderView. (There are alot of examples on the web how to do this).
    In the paint() and sizeHint() use a QTextDocument:
    QTextDocument::drawContents() and QTextDocument::size()

    Note: Qt only supports a "HTML-subset":http://qt-project.org/doc/qt-4.8/richtext-html-subset.html.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CBurn
      wrote on last edited by
      #3

      I have the same problem. Delegates won't work, since the header of QTableView don't rely on delegates.

      Qt doc:
      Note: Each header renders the data for each section itself, and does not rely on a delegate

      At the moment, I think the only way is to override QHeaderView::paintSection method. But I don't know how. An example would be very nice! Or any other helpful suggestions (:

      Thanks in advance and best regards!

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #4

        [quote author="CBurn" date="1376395150"]I have the same problem. Delegates won't work, since the header of QTableView don't rely on delegates.
        [/quote]
        Indeed. Thanks for pointing that out!

        Ok ... the solution is analog to the item delegates approach:
        @
        void MyHeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
        {
        if (!rect.isValid())
        return;

        QStyleOptionHeader opt;
        initStyleOption(&opt);
        //....
        
        opt.text = "";   //IMPORTANT!
        
        // draw the section
        style()->drawControl(QStyle::CE_Header, &opt, painter, this);
        
        //now the paint the html text yourself 
        painter->save();
        
             QRect textRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
        
             m_TextDoc.setHtml(...);   //m_TextDoc is of type QTextDocument
             m_TextDoc.drawContents(painter, textRect);
        
        painter->restore();
        

        }
        @

        Note: i've only posted the relevant changes. Please take the rest from QHeaderView::paintSection() impleemntation source code.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CBurn
          wrote on last edited by
          #5

          Thank you very much for the quick answer!

          It works very well, but only for the first coloumn, although the text from the other columns is present in tmp.
          Also, the text won't be aligned in the middle of the column. Do you have any suggestions?

          This is what my paintSection() looks like:

          @void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
          {
          if (!rect.isValid())
          return;

          QStyleOptionHeader opt;
          initStyleOption(&opt);
          
          opt.rect = rect;
          opt.section = logicalIndex;
          opt.text = this->model()->headerData(logicalIndex, this->orientation(), Qt::DisplayRole).toString();
          opt.textAlignment = Qt::AlignCenter;
          
          // the section position
          
          int visual = visualIndex(logicalIndex);
          
          Q_ASSERT(visual != -1);
          
          if (count() == 1)
              opt.position = QStyleOptionHeader::OnlyOneSection;
          
          else if (visual == 0)
              opt.position = QStyleOptionHeader::Beginning;
          
          else if (visual == count() - 1)
              opt.position = QStyleOptionHeader::End;
          
          else
              opt.position = QStyleOptionHeader::Middle;
          
          QTextDocument TextDoc;
          QString tmp = opt.text;
          opt.text = "";   //IMPORTANT!
          
          // draw the section
          style()->drawControl(QStyle::CE_Header, &opt, painter, this);
          
          painter->save();
          
          QRect textRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
          TextDoc.setHtml(tmp);
          TextDoc.drawContents(painter, textRect);
          
          painter->restore();
          

          }@

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hareen Laks
            wrote on last edited by
            #6

            Thank you very much for both of you..

            I create my own paintsection() using the reply of raven-worx.

            My paintSection() look like below. Hope CBurn can found answer for alignment issue.

            @void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
            {
            int header_column_num_need_to_change =10;

            painter->save();
            QHeaderView::paintSection(painter, rect, logicalIndex);
            painter->restore();
            
            
            if (logicalIndex == header_column_num_need_to_change)
            {
                const QString column_header_text=   (model()->headerData(logicalIndex,Qt::Horizontal,Qt::DecorationRole).toString());
                QStaticText column_header_static_text(( const QString) column_header_text );
            
                QPoint display_location((rect.width()- column_header_text.length())/3,rect.height()/4);
            
                painter-> drawStaticText(rect.left()+display_location.rx(),display_location.ry(),column_header_static_text);
            
            }
            

            }@

            Thanx again..

            Note: I used QStaticText here because it can execute html code.

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #7

              yes you can also use QStaticText ... whatever is more convenient to you.

              QStaticText has methods for setting the text-width and also alignment:
              QStaticText::setTextWidth() and QStaticText::setTextOption()

              And so does QTextDocument: QTextDocument::setDefaultTextOption() and QTextDocument::setTextWidth()

              @CBurn:
              What do you mean it is only working for the first column? What happens with the others?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CBurn
                wrote on last edited by
                #8

                Thanks both of you for the reply!

                Only the first element is plotted by QTextDocument::drawContents(), although the QRect seems to be correct and the QTextDocument is not empty for the other columns.

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #9

                  maybe you need to add the following line (right before drawContents()):
                  @
                  painter->translate( opt.rect.topLeft() );
                  @

                  Otherwise can you please create a screenshot, i think i didn't get what the problem is exactly.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CBurn
                    wrote on last edited by
                    #10

                    I tried your suggestions with the painter->translate(...). I doesn't have any effect.

                    I made two screenshots.

                    The first is the result with the paintSection-code above:
                    !http://s3.imgimg.de/uploads/Capture114f95d8dJPG.jpg(Capture1)!

                    The second is the outcome when I comment line 34 out
                    @//opt.text = "";@

                    !http://s3.imgimg.de/uploads/Capture2711fbc7eJPG.jpg(Capture2)!

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #11

                      ok checked your code. Use this section and it will work:
                      @
                      painter->save();

                      QRect textRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
                      
                      painter->translate( textRect.topLeft() );
                      
                      TextDoc.setDocumentMargin(0);
                      TextDoc.setHtml(tmp);
                      TextDoc.drawContents(painter, QRect( QPoint(0,0), textRect.size()) );
                      
                      painter->restore();
                      

                      @

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        CBurn
                        wrote on last edited by
                        #12

                        Perfect! All works fine now, the alignment, too.

                        Thank you very much for your help!!!

                        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