首页 > 解决方案 > 如果位置大于屏幕尺寸,如何在 QLayout 内移动小部件?

问题描述

例子

#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());
}

标签: c++qtqwidgetqlayout

解决方案


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


推荐阅读