问题描述
我正在使用带有特定标头的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,&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)我尝试了使用horizontalHeader()
提供的QTableWidget
的选项,但是并没有提供任何改进,实际上我获得了第一个屏幕截图的效果(在我手动调整之前,“何时服用”列均已压缩)
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()
的属性,但并未对其中的任何内容进行任何更改最终结果。
感谢您的关注,并为如何解决问题提供了指导。
解决方法
我尝试使用resizeRowsToContents()
来举一个小例子,对我来说效果很好。
在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()
{
}
结果: