简体   繁体   English

PyQT5 在 Gridview 小部件下方添加第二个布局

[英]PyQT5 Add 2nd Layout below Gridview widget

Good day!再会! I hope someone can tell me where I am going wrong with the following simple code.我希望有人能告诉我下面的简单代码哪里出错了。 I am displaying images in a gridview (shown as buttons for simplicity below) These use the usual row and columns to display in formation.我在 gridview 中显示图像(为简单起见,下面显示为按钮)这些使用通常的行和列来显示信息。 My issue is under the gridview I wish to add two independent buttons (ok/cancel).我的问题是在网格视图下我希望添加两个独立的按钮(确定/取消)。 These I don't want to be part of grid but on their own underneath.这些我不想成为网格的一部分,而是自己在下面。

From many hours of messing around I thought I would need to add my grid view to a vertical box ie top slot and then the buttons i've made to the bottom one but i'm not sure of the correct way to achieve this.经过数小时的混乱,我认为我需要将我的网格视图添加到一个垂直框,即顶部插槽,然后将我制作的按钮添加到底部,但我不确定实现此目的的正确方法。 My poor attempt at this code is below.我对这段代码的糟糕尝试如下。 One thing to mention is the completed gridview is passed into a scrollable area.值得一提的是,完成的 gridview 被传递到一个可滚动的区域。 I don't know if that's why I don't see the buttons appear in my example.我不知道这是否是我的示例中没有出现按钮的原因。 Thank you谢谢

Hoping for it to look like this希望它看起来像这样在此处输入图像描述

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        # Set layout
        self.setLayout(hbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        self.setCentralWidget(scroll)
        # Show
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

It's because you set scroll widget as central widget.这是因为您将scroll小部件设置为中心小部件。 scroll widget contains only wrapper_widget , which contains gridlayout , not vbox . scroll小部件仅包含wrapper_widget ,其中包含gridlayout ,而不是vbox

Try creating vbox and add scroll widget and hbox to vbox , and set central widget with QWidget which has vbox layout.尝试创建vbox并将scroll小部件和hbox添加到vbox ,并使用具有vbox布局的QWidget设置中央小部件。

I modified some of your code, adding master_widget which has vbox layout that contains scroll and hbox layout.我修改了您的一些代码,添加了具有vbox布局的master_widget ,其中包含scrollhbox布局。 Check this!检查这个!

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        # vbox.addLayout(hbox)
        # Set layout
        # self.setLayout(vbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        # scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        master_widget = QWidget()
        master_widget.setLayout(vbox)
        vbox.addWidget(scroll)
        vbox.addLayout(hbox)
        self.setCentralWidget(master_widget)
        # Show
        self.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM