问题描述
我使用 Qt 设计器创建了一个主窗口,其中包含一个 tabwidget.我的问题是,当窗口最大化时,tabwidget 保持其原始大小 - 因此在其右侧留下了很多灰色空间.
I have created a main window using Qt designer that has a tabwidget in it. My problem is that when the window is maximized, the tabwidget remains its original size - thus leaving a lot of grey space to its right.
我希望主窗口始终最大化,那么如何调整 tabwidget 的大小以占用更多空间?我可以在以下代码中添加什么来实现这一点?
I would like for the main window to always be maximized, so how can I resize the tabwidget to occupy more space? What can I add to the following code to achieve this?
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setEnabled(True)
self.tabWidget.setGeometry(QtCore.QRect(20, 40, 601, 501))
self.tabWidget.setTabPosition(QtGui.QTabWidget.North)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
推荐答案
你需要使用某种QLayout
.
您可以在 Designer 中轻松完成此操作.只需右键单击表单并选择 Layout
和 Lay Out Horizontally
或 Lay Out Vertically
- 您需要表单上的其他小部件才能看到两者之间的区别.您将看到在对象检查器中添加了 QLayout
,并且您将能够像使用小部件一样调整其属性.
You can do this very easily in Designer. Just right click the form and choose Layout
and either Lay Out Horizontally
or Lay Out Vertically
- you'll need other widgets on the form to see the difference between the two. You'll see the QLayout
added in the Object Inspector and you'll be able to adjust its properties like you can with your widgets.
您还可以使用代码创建布局.这是一个工作示例:
You can also create layouts with code. Here's a working example:
import sys
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QApplication, QWidget, QTabWidget, QHBoxLayout
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
# Create the layout.
self.h_layout = QHBoxLayout()
# Create the QTabWidget.
self.tabWidget = QTabWidget()
self.tabWidget.setEnabled(True)
self.tabWidget.setGeometry(QRect(20, 40, 601, 501))
self.tabWidget.setTabPosition(QTabWidget.North)
self.tabWidget.setObjectName('tabWidget')
# Add the QTabWidget to the created layout and set the
# layout on the QWidget.
self.h_layout.addWidget(self.tabWidget)
self.setLayout(self.h_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
这篇关于在 PyQt4 中调整小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!