【发布时间】:2021-01-22 13:42:51
【问题描述】:
我正在使用带有特定标头的QTableWidget 实现一个小示例。
但是,一旦我运行示例,行就无法正确拉伸,因为可以在以下示例中看到(这是错误行为):
手动调整大小后,我得到了我正在寻找的东西(这是预期的行为):
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();
}
到目前为止我做了什么:
1) 我尝试按以下方式使用标头,但这并没有给出预期的行为。 这种方法的问题在于列是等间距的(我不是在寻找这种特定行为,因为我需要用户根据需要调整它们)并且,最重要的是,行占据了整个空间使该行变得非常大的应用程序窗口。
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) 我尝试了使用QTableWidget 提供的horizontalHeader() 选项,但这并没有提供任何改进,实际上我得到了第一个屏幕截图的效果(“当To Take" 列全部压缩,直到我手动调整)
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)我遇到了this source、this other source,但他们都没有提供关于如何解决问题的说明。
4) 我更深入地研究了这个问题并查看了this,它使用了我在示例中使用的resizeRowsToContents() 的属性,但最终结果没有改变任何东西。
感谢您对此进行解释并提供有关如何解决问题的指导。
【问题讨论】:
-
如何将小部件设置到第 4 列?
标签: qt c++11 qt5 qtablewidget qheaderview