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 properly resize a costum QTableView with a costum QHeaderView
Forum Updated to NodeBB v4.3 + New Features

How to properly resize a costum QTableView with a costum QHeaderView

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.9k 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.
  • F Offline
    F Offline
    FlyingHubert
    wrote on last edited by VRonin
    #1

    Hi,

    I've wrote some code to generate a costum TableView. It displays data from a costum TableModel which works fine. Because the data is often just "yes" or "no" I wanted to create a Costum HeaderView with vertical Text in it. That worked well! But now I want the view to resize the columns perperly but it doesn't work at all.

    I tried many different variants with all the (virtual) methods given by QHeaderView and QTableView. But the TableView doesn't resize properly. I want the columns with "yes" and "no" to be as small as possible but they are way to big. And I also want the adress columns to be at the proper size for their content.

    My question now is:
    Which methods do I have to call/overwrite to resize the TableView to its contents?

    The only real thing I coded and that worked was:

    void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    {
        const int grosse = 12;
        painter->setRenderHint(QPainter::Antialiasing);
    
        QString title = model()->headerData(logicalIndex,Qt::Horizontal,Qt::DisplayRole).toString();
    
        QLinearGradient gradient(rect.topLeft(), rect.bottomRight());
        gradient.setColorAt(0, Qt::gray);
        gradient.setColorAt(1, Qt::lightGray);
        painter->fillRect(rect, gradient);
        painter->drawRect(rect);
    
    
        QFont f("Cambria Math",grosse);
        f.setBold(true);
        painter->setFont(f);
    
        painter->translate(rect.x() + rect.width()/2-grosse/2, rect.y());
        painter->rotate(90);
        painter->drawText(0,0, title);
    }
    
    QSize MyHeader::sizeHint() const
    {
        int height = 350;
        int width  = 20;
    
        return QSize(width,height);
    }
    

    result image

    F 1 Reply Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @FlyingHubert
      May be
      http://doc.qt.io/qt-5/qtableview.html#resizeColumnsToContents
      http://doc.qt.io/qt-5/qtableview.html#resizeRowsToContents
      and for Header tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

      --Alles ist gut.

      1 Reply Last reply
      0
      • F FlyingHubert

        Hi,

        I've wrote some code to generate a costum TableView. It displays data from a costum TableModel which works fine. Because the data is often just "yes" or "no" I wanted to create a Costum HeaderView with vertical Text in it. That worked well! But now I want the view to resize the columns perperly but it doesn't work at all.

        I tried many different variants with all the (virtual) methods given by QHeaderView and QTableView. But the TableView doesn't resize properly. I want the columns with "yes" and "no" to be as small as possible but they are way to big. And I also want the adress columns to be at the proper size for their content.

        My question now is:
        Which methods do I have to call/overwrite to resize the TableView to its contents?

        The only real thing I coded and that worked was:

        void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
        {
            const int grosse = 12;
            painter->setRenderHint(QPainter::Antialiasing);
        
            QString title = model()->headerData(logicalIndex,Qt::Horizontal,Qt::DisplayRole).toString();
        
            QLinearGradient gradient(rect.topLeft(), rect.bottomRight());
            gradient.setColorAt(0, Qt::gray);
            gradient.setColorAt(1, Qt::lightGray);
            painter->fillRect(rect, gradient);
            painter->drawRect(rect);
        
        
            QFont f("Cambria Math",grosse);
            f.setBold(true);
            painter->setFont(f);
        
            painter->translate(rect.x() + rect.width()/2-grosse/2, rect.y());
            painter->rotate(90);
            painter->drawText(0,0, title);
        }
        
        QSize MyHeader::sizeHint() const
        {
            int height = 350;
            int width  = 20;
        
            return QSize(width,height);
        }
        

        result image

        F Offline
        F Offline
        FlyingHubert
        wrote on last edited by
        #3

        @FlyingHubert

        Hmm okay, when i code it like this it does not work. Is that what you meant ?

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            MyTableView *view = new MyTableView(this);
            MyHeader* header = new MyHeader(view);
            MyXmlReader* reader = new MyXmlReader;
            MyModel* model = new MyModel(reader->data());
        
            view->setModel(model);
            view->setHorizontalHeader(header);
        
            view->resizeColumnsToContents();
            view->resizeRowsToContents();
            view->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
        
            setCentralWidget(view);
        

        }

        1 Reply Last reply
        0
        • F Offline
          F Offline
          FlyingHubert
          wrote on last edited by
          #4

          Hi,

          I found my problem.
          I forgot the reimplement the Qt::SizeHintRole for the "QAbstractTableModel::headerData(...)" method. therefore the QHeaderView didn't know which size each section had and made wrong assumptions!

          Thanks anyway for th reading of the post.

          Greetings FlyingHubert

          1 Reply Last reply
          1

          • Login

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