問題描述
我是 Qt 的新手,我剛剛設法使 QTableView 與我的模型一起工作.它有固定的 3 列.當我打開一個窗口時,它看起來不錯,但是當我調(diào)整窗口大小時,QTableView 本身會調(diào)整大小,但列的寬度保持不變.有什么內(nèi)置的方法可以讓它工作嗎?我希望每次調(diào)整窗口大小時都調(diào)整列的大小以適應 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 的最后一列在調(diào)整大小時填滿其父級.你可以這樣設置:
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);
但是,這不會按比例調(diào)整其他列的大小.如果你也想這樣做,你可以這樣在你父母的 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.
這篇關于列自動調(diào)整為 QTableView 的大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!