【问题标题】:QTableWidget: Row does not size properlyQTableWidget:行大小不正确
【发布时间】: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 sourcethis other source,但他们都没有提供关于如何解决问题的说明。

4) 我更深入地研究了这个问题并查看了this,它使用了我在示例中使用的resizeRowsToContents() 的属性,但最终结果没有改变任何东西。

感谢您对此进行解释并提供有关如何解决问题的指导。

【问题讨论】:

  • 如何将小部件设置到第 4 列?

标签: qt c++11 qt5 qtablewidget qheaderview


【解决方案1】:

我尝试使用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()
{
}

结果:

【讨论】:

  • 感谢您的帮助! :) 你的例子有效,我没想过使用QStandardItemModel,这是个好主意!感谢您的光临和您的时间!如果您认为我的问题很清楚,请给我一个大拇指! :) 祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2019-12-20
相关资源
最近更新 更多