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. Invalid working of QHeaderView::QResizeToContents in QTableWidget
Forum Update on Monday, May 27th 2025

Invalid working of QHeaderView::QResizeToContents in QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 177 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.
  • V Offline
    V Offline
    Vectrom
    wrote on last edited by
    #1

    Hello. I have got a QTableWidget "_classView" with settings:

    _classView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    _classView->verticalHeader()->setVisible(false);
    _classView->horizontalHeader()->setVisible(false);
    _classView->horizontalHeader()->setStretchLastSection(true);
    _classView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    _classView->setColumnCount(3);
    

    Now, if text in cell is long, it creates a lot of padding on top and bottom of the text which makes the row much larger than it needs to be. Screen:
    0_1565353870039_table_big.PNG

    I would like to achieve similar effect (resizing, without scrollbar), but without unnecessary top and bottom paddings. Anyone knows how I can do it?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Are you sure you trimmed that large value and its not just extra spaces that does it ?
      Trying your setup code with a TableView did not produce same result.

      alt text

      auto text = R"(
      {
          "glossary": {
              "title": "example glossary",
              "GlossDiv": {
                  "title": "S",
                  "GlossList": {
                      "GlossEntry": {
                          "ID": "SGML",
                          "SortAs": "SGML",
                          "GlossTerm": "Standard Generalized Markup Language",
                          "Acronym": "SGML",
                          "Abbrev": "ISO 8879:1986",
                          "GlossDef": {
                              "para": "A meta-markup language, used to create markup languages such as DocBook.",
                              "GlossSeeAlso": ["GML", "XML"]
                          },
                          "GlossSee": "markup"
                      }
                  }
              }
          }
      }
      )";
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          auto _classView = ui->tableView;
          QStringListModel *model = new QStringListModel();
          QStringList list;
          list << "a" << "b" << "c" << text << "d" << "e" << "long line of text";
      
          model->setStringList(list);
          _classView->setModel(model);
          _classView->setEditTriggers(QAbstractItemView::NoEditTriggers);
          _classView->verticalHeader()->setVisible(false);
          _classView->horizontalHeader()->setVisible(false);
          _classView->horizontalHeader()->setStretchLastSection(true);
          _classView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
          // _classView->setColumnCount(3);
      }
      
      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