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):
After manual resizing, I obtain what I am looking for (which is the expected behavior):
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:
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(); }
I tried the option of using the
horizontalHeader()
provided by theQTableWidget
, 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(); }
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.
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.
I tried to make a small example using
resizeRowsToContents()
and it works well for me.Tested on Qt 5.15.1 MinGW.
Result: