wysota, the problem of not being able to move the horizontal header to the bottom is caused by:
view.
verticalHeader()->setSectionResizeMode
(QHeaderView::Stretch);
view.
horizontalHeader()->setSectionResizeMode
(QHeaderView::Stretch);
view.verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
To copy to clipboard, switch view to plain text mode
- I want the rows/columns to both automatically fill the space, and I also want the horizontal header on the bottom.
- Do I need to manually implement the "setSectionResizeMode(QHeaderView::Stretch)" code in my application?
Here is the full duplication to see the issue (drag the window larger and smaller to see the flicker):
#include <QtWidgets>
#include <QTableView>
{
public:
virtual ~PlotTableView() {}
protected:
{
QRect verticalHeaderRect
= verticalHeader
()->geometry
();
QRect horizontalHeaderRect
= horizontalHeader
()->geometry
();
setViewportMargins(
verticalHeaderRect.width(), // left
horizontalHeaderRect.height(), // top
0, // right
horizontalHeaderRect.height()); // bottom
// Move the horizontal header to the bottom
horizontalHeader
()->setGeometry
(QRect( verticalHeaderRect.width(),
this->viewport()->geometry().height() + horizontalHeaderRect.height(),
horizontalHeaderRect.width(),
horizontalHeaderRect.height()));
}
};
int main(int argc, char **argv) {
window->setCentralWidget(q);
PlotTableView view;
view.setModel(&model);
view.resizeColumnsToContents();
view.resizeRowsToContents();
// These two lines cause the horizontalHeader()->setGeometry request to flicker and fail
view.
verticalHeader()->setSectionResizeMode
(QHeaderView::Stretch);
view.
horizontalHeader()->setSectionResizeMode
(QHeaderView::Stretch);
hBox->addWidget(&view);
hBox->addWidget(bottomRightWidget);
hSplitter->addWidget(&view);
hSplitter->addWidget(bottomRightWidget);
sizes << 1 << 1;
hSplitter->setSizes(sizes);
vBox->addWidget(topWidget);
vBox->addWidget(hSplitter);
q->setLayout(vBox);
window->show();
return app.exec();
}
#include <QtWidgets>
#include <QTableView>
class PlotTableView : public QTableView
{
public:
PlotTableView(QWidget *parent = NULL) : QTableView(parent) { };
virtual ~PlotTableView() {}
protected:
virtual void resizeEvent(QResizeEvent *event)
{
QTableView::resizeEvent(event);
QRect verticalHeaderRect = verticalHeader()->geometry();
QRect horizontalHeaderRect = horizontalHeader()->geometry();
setViewportMargins(
verticalHeaderRect.width(), // left
horizontalHeaderRect.height(), // top
0, // right
horizontalHeaderRect.height()); // bottom
// Move the horizontal header to the bottom
horizontalHeader()->setGeometry(QRect(
verticalHeaderRect.width(),
this->viewport()->geometry().height() + horizontalHeaderRect.height(),
horizontalHeaderRect.width(),
horizontalHeaderRect.height()));
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
QStandardItemModel model(4,4);
QMainWindow *window = new QMainWindow();
QWidget *topWidget = new QWidget(window);
QWidget *bottomRightWidget = new QWidget(window);
bottomRightWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QWidget * q = new QWidget();
window->setCentralWidget(q);
PlotTableView view;
view.setModel(&model);
view.resizeColumnsToContents();
view.resizeRowsToContents();
view.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
// These two lines cause the horizontalHeader()->setGeometry request to flicker and fail
view.verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
QHBoxLayout *hBox = new QHBoxLayout();
hBox->addWidget(&view);
hBox->addWidget(bottomRightWidget);
QSplitter *hSplitter = new QSplitter(Qt::Horizontal);
hSplitter->addWidget(&view);
hSplitter->addWidget(bottomRightWidget);
QList <int> sizes;
sizes << 1 << 1;
hSplitter->setSizes(sizes);
QVBoxLayout *vBox = new QVBoxLayout();
vBox->addWidget(topWidget);
vBox->addWidget(hSplitter);
q->setLayout(vBox);
window->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks