mercredi 7 octobre 2020

QTableWidget: Row does not size properly

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):

yo

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

yo2

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) and, 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 actually I 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 this source, this other source but none of them provided light on how to solve the issue.

4) I dug more into the problem and went through this which is using the property of resizeRowsToContents() which I used in the example but didn't change anything in the final result.

Thanks for shedding light ob this and provide guidance on how to solve the problem.

Aucun commentaire:

Enregistrer un commentaire