QTableWidget row does not resize properly

311 views Asked by At

I am implementing a small example using a QTableWidget with specific headers.

However, as soon as I run the example, the rows do not stretch properly as it is possible to see in the following example (which is the wrong behavior):

table widget with wrongly sized rows that appear too shrinked

After manual resizing, I obtain what I am looking for (which is the expected behavior):

table widget with correctly sized rows with visible text

prescriptiondialog.h

class PrescriptionDialog : public QDialog
{
    Q_OBJECT

public:
    PrescriptionDialog();
    ~PrescriptionDialog();

    QPushButton *mAddButton;
    QPushButton *mRemoveButton;
    QLineEdit *durationEdit;
    QLabel *durationLbl;
    DrugTable *mTable;
};
#endif // PRESCRIPTIONDIALOG_H

prescriptiondialog.cpp

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHeaderView>

PrescriptionDialog::PrescriptionDialog()
{
    setWindowTitle("Drug Mixer");
    mTable = new DrugTable();
    mTable->horizontalHeader()->setStretchLastSection(4);
    mTable->verticalHeader()->setStretchLastSection(QHeaderView::Interactive);
    mTable->show();
    QObject::connect(mAddButton, &QPushButton::clicked, mTable, &DrugTable::addCustomRow);
    QObject::connect(mRemoveButton, &QPushButton::clicked, mTable, &DrugTable::removeCustomRow);
    setLayout(mLay);
    show();
}

What I have done so far:

  1. I tried to use the headers in the following way, but that did not give the expected behavior.
    The problem with this approach, is that columns are equally spaced (I am not looking for this specific behavior because I need the user to adjust them as they want). Most importantly, the row takes the whole space of the application window, making the row extremely big.

    PrescriptionDialog::PrescriptionDialog()
    {
        setWindowTitle("Drug Mixer");
        mTable = new DrugTable();
        mTable->horizontalHeader()->setStretchLastSection(4);
        mTable->verticalHeader()->setStretchLastSection(QHeaderView::Interactive);
        QHeaderView* header = mTable->horizontalHeader();
        header->setSectionResizeMode(QHeaderView::Stretch);
        QHeaderView* headerRows = mTable->verticalHeader();
        headerRows->setSectionResizeMode(QHeaderView::Stretch);
        mTable->show();
    }
    
  2. I tried the option of using the horizontalHeader() provided by the QTableWidget, but that didn't provide any improvements, and I actually obtained the effect of the first screenshot (the "When To Take" column is all compressed until I manually adjust).

    PrescriptionDialog::PrescriptionDialog()
    {
        setWindowTitle("Drug Mixer");
        mTable = new DrugTable();
        mTable->horizontalHeader()->setStretchLastSection(4);
        mTable->verticalHeader()->setStretchLastSection(QHeaderView::Interactive);
        mTable->resizeRowsToContents();
        mTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
        mTable->show();
    }
    
  3. I came across Qt Centre — How to resize qtablewidget rows, Qt Centre — how can i have QTableWidget with autosize row height, but found no solution.

  4. I went through Qt Centre — QTableView QTableWidget and resizeRowsToContents resizes wrongly, which is using the method resizeRowsToContents(), which I used in the example, but didn't change anything in the final result.

1

There are 1 answers

0
Minh On BEST ANSWER

I tried to make a small example using resizeRowsToContents() and it works well for me.
Tested on Qt 5.15.1 MinGW.

#include "mainwindow.h"

#include <QTableView>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStandardItemModel>
#include <QHeaderView>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QStandardItemModel *model = new QStandardItemModel{this};
    model->appendRow({new QStandardItem{tr("Drug")}, new QStandardItem{}});

    QTableView *view = new QTableView{this};
    view->setModel(model);

    QHBoxLayout *horz_layout = new QHBoxLayout;
    horz_layout->addWidget(new QPushButton{tr("Add when"), this});
    horz_layout->addWidget(new QPushButton{tr("Remove when"), this});

    QStandardItemModel *inner_model = new QStandardItemModel{this};
    inner_model->setHorizontalHeaderLabels({tr("Select"), tr("When to take")});

    QTableView *inner_view = new QTableView{this};
    inner_view->setModel(inner_model);

    QWidget *widget = new QWidget;
    QVBoxLayout *vert_layout = new QVBoxLayout{widget};
    vert_layout->addLayout(horz_layout);
    vert_layout->addWidget(inner_view);

    view->horizontalHeader()->setStretchLastSection(true);
    view->setIndexWidget(model->index(0, 1), widget);
    view->resizeRowToContents(0);

    this->setCentralWidget(view);
    this->resize(500, 500);
}

MainWindow::~MainWindow()
{
}

Result:

Result