python - 为什么我的 PyQt 小部件重叠?

标签 python qt pyqt

def __init__(self,parent=None):
    QMainWindow.__init__(self,parent)
    self.setWindowTitle("test")

    self.createActions()

    self.mainWidget=QWidget(self)
    self.mainLayout = QHBoxLayout(self.mainWidget)

    self.createListView()
    self.lv.setMinimumSize(200,200)

    self.textEdit = QTextEdit()
    self.textEdit.setMinimumSize(200,200)

    self.mainLayout.addWidget(self.lv)
    self.mainLayout.addWidget(self.textEdit)

这一切所做的就是创建一个带有工具栏的窗口,并将两个小部件放入 QHBoxLayout、一个 QListView 和一个 QTextEdit 中。然而出于某种原因,这三件事似乎是重叠的。 (工具栏在小部件下方可见)

output

最佳答案

您需要将这些小部件和布局放在 QMainWindow中央小部件中。下面是 QMainWindow 的组成:

enter image description here

(图片来自PyQt文档:http://pyqt.sourceforge.net/Docs/PyQt4/qmainwindow.html#centralWidget)

在您的情况下,解决方案是将 QHBoxLayout 设置为中央小部件的布局:

self.centralwidget.setLayout(self.mainLayout)

(无需创建另一个QWidget 来保存它,您可以利用现有的中央小部件。)

关于python - 为什么我的 PyQt 小部件重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408878/

相关文章:

python - 如何使按钮图像拉伸(stretch)、透明、宽度高度完全缩放适合?

c++ - 将自定义 qt 小部件动态添加到框架中,而无需每次都更改布局

python - QDialog 未从主窗口打开(pyQt)

python - 以最小尺寸分割的字符串

python - Django with mysql安装模块macos

c++ - 如何在 mac os x 上使用 g++ 编译一个简单的 Qt 和 c++ 应用程序?

c++ - 在 OS X 上部署 Qt 应用程序时出错

python - PyQt4 如何通过单击按钮使用多处理

python - PyQt4 本地目录 View ,带有选择文件夹的选项

python - 在 Windows 上使用 pip 安装 zbar 失败