列自动调整为QTableView的大小 [英] Columns auto-resize to size of QTableView

查看:1638
本文介绍了列自动调整为QTableView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的QT,我刚刚设法使一个QTableView与我的模型工作。它有固定的3列。当我打开一个窗口,它看起来确定,但当我调整窗口大小,QTableView本身调整大小,但列的宽度保持不变。有任何内置的方式,使其工作吗?

I am new to QT and I have just managed to make a QTableView work with my model. It has fixed 3 columns. When I open a window, it look ok but when i resize the window, the QTableView itself gets resized but columns' width remains the same. Is there any build-in way to make it work? I want columns to resize to fit the edges of QTableView every the the window gets resized.

推荐答案

有一个标题标志,以确保如果调整大小,QTableView的最后一列填充其父。您可以这样设置:

There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:

table_view->horizontalHeader()->setStretchLastSection(true);

但是,这不会按比例调整其他列的大小。如果你想做到这一点,你可以处理它的resizeEvent你的父母因此:

However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:

void QParent::resizeEvent(QResizeEvent *event) {
    table_view->setColumnWidth(0, this->width()/3);
    table_view->setColumnWidth(1, this->width()/3);
    table_view->setColumnWidth(2, this->width()/3);

    QMainWindow::resizeEvent(event);
}

QParent类是QMainWindow的子类。

QParent class is subclass of QMainWindow.

这篇关于列自动调整为QTableView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆