Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Invalid working of QHeaderView::QResizeToContents in QTableWidget



  • 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?


  • Lifetime Qt Champion

    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);
    }
    

Log in to reply