QTableWidget:行大小不正确
QTableWidget: Row does not size properly
我正在使用 QTableWidget
和特定的 headers 来实现一个小示例。
但是,一旦我 运行 示例,行就不会正确拉伸,如下例所示(这是 错误行为 ):
手动调整大小后,我得到了我正在寻找的东西(这是预期的行为):
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) 我尝试按以下方式使用 headers,但没有给出预期的行为。
这种方法的问题是列同样是 spaced(我不是在寻找这种特定的行为,因为我需要用户根据需要调整它们)而且,最重要的是,该行占用整个 space 应用程序 window,使该行非常大。
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()
的选项,但没有提供任何改进,实际上我获得了第一张截图效果(“When 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 但其中 none 提供了解决问题的方法。
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()
{
}
结果:
我正在使用 QTableWidget
和特定的 headers 来实现一个小示例。
但是,一旦我 运行 示例,行就不会正确拉伸,如下例所示(这是 错误行为 ):
手动调整大小后,我得到了我正在寻找的东西(这是预期的行为):
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) 我尝试按以下方式使用 headers,但没有给出预期的行为。 这种方法的问题是列同样是 spaced(我不是在寻找这种特定的行为,因为我需要用户根据需要调整它们)而且,最重要的是,该行占用整个 space 应用程序 window,使该行非常大。
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()
的选项,但没有提供任何改进,实际上我获得了第一张截图效果(“When 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 但其中 none 提供了解决问题的方法。
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()
{
}
结果: