問題描述
我正在使用 QGridLayout,通過使用 setCentralWidget 函數,行和列 (0,0) 從窗口的中心開始,留下很多空白空間.
I'm using QGridLayout, and by using the setCentralWidget function, the row and column (0,0) start in the center of the window which leaves a lot of empty space.
如何讓它居中但從窗口頂部開始而不是在中間?
How can I get it centered but beginning from the top of the window and not in the middle?
我對 Qt 還很陌生,想知道我是否處理錯了?我應該改為為 QWidget 創建一個新類嗎?
I'm pretty new to Qt, and was wondering if I'm handling it all wrong? Should I instead make a new class for QWidget?
推薦答案
你必須在第2行建立一個Stretch,但在此之前你必須更正rowSpan:
You have to establish a Stretch in row 2, but before that you must correct the rowSpan in:
為了理解,讓我們回顧一下文檔:
To understand, let's review the docs:
void QGridLayout::addWidget(QWidget *widget, int fromRow, intfromColumn, int rowSpan, int columnSpan, Qt::Alignment 對齊 =...)
這是一個重載函數.
此版本將給定的小部件添加到單元格網格中,跨越多個行/列.單元格將從 fromRow 開始,fromColumn 跨越rowSpan 行和 columnSpan 列.小部件將具有給定的對齊.
This version adds the given widget to the cell grid, spanning multiple rows/columns. The cell will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns. The widget will have the given alignment.
如果 rowSpan 和/或 columnSpan 為 -1,則小部件將分別延伸到底部和/或右邊緣.
If rowSpan and/or columnSpan is -1, then the widget will extend to the bottom and/or right edge, respectively.
也就是說,rowSpan 表示小部件將占用多少行,但您將其指示為 0,因此布局將不再處理大小而僅處理位置,您必須將其更改為 1.
That is, the rowSpan indicates how many rows the widget will occupy, but you are indicating it to 0 so the size will no longer be handled by the layout but only the position, you must change it to 1.
這篇關于在QMainWindow中放置一個小部件,如何定位而不是setCentralWidget的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!