如果控件的位置大于屏幕尺寸,如何在QLayout中移动控件?

时间:2018-09-18 10:50:48

标签: c++ qt qwidget qlayout

Example

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
#include <QTableWidget>
#include <QHeaderView>
#include <QGridLayout>
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *widget = new QWidget(this);
    QGridLayout *layout = new QGridLayout();
    layout->setSizeConstraint(QLayout::SetFixedSize);

    for (int i = 0; i < 10; ++i) {
        QTableWidget *table = new QTableWidget(this);
        InitializeQtableWidget(table, 3, 5);
        layout->addWidget(table);
    }

    widget->setLayout(layout);
    setCentralWidget(widget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitializeQtableWidget(QTableWidget *table, const int &rows, const int &columns)
{
    table->setRowCount(rows);
    table->setColumnCount(columns);
    const int tableColumnWidth = 60;
    const int tableRowHeight = 35;
    for(int i = 0; i < columns; ++i)
       table->setColumnWidth(i, tableColumnWidth);
    table->verticalHeader()->setFixedWidth(tableColumnWidth);
    table->horizontalHeader()->setFixedHeight(tableRowHeight + 5);

    table->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    table->setFixedSize(table->horizontalHeader()->length() + table->verticalHeader()->width(),
                        table->verticalHeader()->length() + table->horizontalHeader()->height());
}

1 个答案:

答案 0 :(得分:0)

您需要为此编写自己的布局实现。或者,您可以检查一些开源解决方案并适应您的需求。看看this one