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. 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. 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
widget contains only wrapper_widget
, which contains gridlayout
, not vbox
.
Try creating vbox
and add scroll
widget and hbox
to vbox
, and set central widget with QWidget
which has vbox
layout.
I modified some of your code, adding master_widget
which has vbox
layout that contains scroll
and hbox
layout. 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.