I am trying to create a simple application with PySide, but it seems that I didn't read the docs properly. That is my code:
from PySide.QtGui import *
from PySide.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.scene = QGraphicsScene()
self.view1 = QGraphicsView(self.scene, self)
self.view2 = QGraphicsView(self.scene, self)
self.gridLayout = QGridLayout()
self.gridLayout.addWidget(self.view1, 0, 0, Qt.AlignLeft)
self.gridLayout.addWidget(self.view2, 0, 1, Qt.AlignRight)
self.gridLayout.setColumnMinimumWidth(0, 300)
self.gridLayout.setColumnMinimumWidth(1, 300)
self.setLayout(self.gridLayout)
self.view1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.view2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.scene.addLine(0, 0, 1000, 1000)
if __name__ == "__main__":
app = QApplication(())
window = Window()
window.showMaximized()
app.exec_()
The code executes, but it should display a window with two QGraphicsViews, which should divide the window in half, but I only get one QGraphicsView in its minimum size. Can someone help me with this?
Thanks in advance.
You need to create a central widget for a QMainWindow, and then set the layout on that. Simplifying your example:
from PySide.QtGui import *
class Window(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.scene = QGraphicsScene()
self.view1 = QGraphicsView(self.scene, self)
self.view2 = QGraphicsView(self.scene, self)
self.view1.setFrameShape(QFrame.NoFrame)
self.view2.setFrameShape(QFrame.NoFrame)
widget = QWidget(self)
layout = QGridLayout(widget)
layout.addWidget(self.view1, 0, 0)
layout.addWidget(self.view2, 0, 1)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.setCentralWidget(widget)
self.scene.addLine(0, 0, 1000, 1000)
if __name__ == "__main__":
app = QApplication(())
window = Window()
window.showMaximized()
app.exec_()
Related
I am using QGridLayout in my project, and the code is:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
widget = QWidget()
self.setCentralWidget(widget)
layout = QGridLayout()
widget.setLayout(layout)
lay1 = QVBoxLayout()
lay1Header = QHBoxLayout()
lay1Header.addWidget(QLabel('lay1'))
lay1.addLayout(lay1Header)
label1 = QLabel('label1')
label1.setStyleSheet('background: rgb(255, 0, 0)')
lay1.addWidget(label1)
lay2 = QVBoxLayout()
lay2Header = QHBoxLayout()
lay2Header.addWidget(QLabel('lay2'))
lay2Header.addWidget(QLineEdit())
lay2.addLayout(lay2Header)
label2 = QLabel('label2')
label2.setStyleSheet('background: rgb(0, 0, 255)')
lay2.addWidget(label2)
layout.addLayout(lay1, 0, 0, 1, 1)
layout.addLayout(lay2, 0, 1, 1, 1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
And the result is:
My environment is:
win 10
anaconda
pyqt5 5.14.0
How can I make the size of label1/label2 the same?
The size of the items in a QGridLayout depends on all the items in the column or row, not just one of them. So for this case the solution is to set the same alignment factor for the columns and set the height of the first QLabel to be that of the QLineEdit:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
widget = QWidget()
self.setCentralWidget(widget)
layout = QGridLayout(widget)
lbl1 = QLabel("lay1")
lay1 = QVBoxLayout()
lay1Header = QHBoxLayout()
lay1Header.addWidget(lbl1)
lay1.addLayout(lay1Header)
label1 = QLabel("label1")
label1.setStyleSheet("background: rgb(255, 0, 0)")
lay1.addWidget(label1)
le1 = QLineEdit()
lay2 = QVBoxLayout()
lay2Header = QHBoxLayout()
lay2Header.addWidget(QLabel("lay2"))
lay2Header.addWidget(le1)
lay2.addLayout(lay2Header)
label2 = QLabel("label2")
label2.setStyleSheet("background: rgb(0, 0, 255)")
lay2.addWidget(label2)
layout.addLayout(lay1, 0, 0, 1, 1)
layout.addLayout(lay2, 0, 1, 1, 1)
layout.setColumnStretch(0, 1)
layout.setColumnStretch(1, 1)
lbl1.setFixedHeight(le1.sizeHint().height())
Layout structure:
self.mainLayout = QGridLayout()
self.subLayout1 = QGridLayout()
self.subLayout2 = QVBoxLayout()
...
...
...
self.mainLayout.addLayout(subLayout1,0,0)
I have tried:
self.mainLayout = QGridLayout()
self.panel = QWidget()
self.subLayout1 = QGridLayout(self.panel)
self.mainLayout.addLayout(subLayout1,0,0)
self.panel.setStylesheet("background-color: red;")
Error: QLayout::addChildLayout: layout "" already has a parent
This is how I have set my mainLayout color
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.black)
self.setPalette(p)
which needs on back when showing subLayout1, subLayout2 etc
How do I do that ?
Edit : Added MCVE ( A Minimal, Complete, verifiable example )
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle('MCVE')
'''Window Background'''
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
'''Core Layouts'''
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.panel = QWidget()
self.profileInfo = QGridLayout(self.panel)
'''Nested Layout'''
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
'''Widgets'''
self.display = QLabel('QHBoxLayout()')
self.download = QLabel('QHBoxLayout()')
self.subs = QLabel('QVBoxLayout()')
self.fileInfo = QLabel('QGridLayout()')
'''AddWidgets'''
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.fileInfo, 0, 0)
'''Stylesheet'''
self.panel.setStyleSheet("background-color: red;")
'''Initiating mainLayout '''
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == '__main__':
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()
As said above, the code error QLayout::addChildLayout: layout "" already has a parent, if Stylesheet section is commented out, QGridLayout() label shows up on top of that darkyellow background and I want that background section to be red.
A QXLayout is not a visual element but a class that controls the position and especially the size of the widget that is assigned. So wanting to set a color to that class that is not a visual element makes no sense.
On the other hand, the error is caused because the 2 following codes are equivalent:
x = QXLayout(w)
x = QXLayout()
w.setLayout(x)
In your case you have indicated that the layout "profileInfo" belongs to "panel" with the following code: self.profileInfo = QGridLayout(self.panel), but then you indicate that the same layout belongs to "mainLayout" through the following code: self.mainLayout.addLayout(self.profileInfo, 0, 0). So to avoid the warning you must pass replace the last code with self.mainLayout.addWidget(self.panel, 0, 0).
Update:
If you want a widget to be inside another one then it must be a child of the second one, so the "fileInfo" must be a child of "panel", but since you want it to occupy the entire size of the parent then you must use a layout.
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle("MCVE")
"""Window Background"""
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
"""Core Layouts"""
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.profileInfo = QGridLayout()
"""Nested Layout"""
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
"""Widgets"""
self.display = QLabel("QHBoxLayout()")
self.download = QLabel("QHBoxLayout()")
self.subs = QLabel("QVBoxLayout()")
self.fileInfo = QLabel("QGridLayout()")
self.panel = QWidget()
"""AddWidgets"""
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.panel, 0, 0)
lay = QVBoxLayout(self.panel)
lay.addWidget(self.fileInfo)
"""Stylesheet"""
self.panel.setStyleSheet("background-color: red;")
"""Initiating mainLayout """
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == "__main__":
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()
I'm trying to incorporate a QSplitter. The code works perfectly from a functionality standpoint, but the QSplitter itself doesn't appear correctly under the default PyQt style... possibly because it is itself embedded within a vertical splitter. This is confusing for the user.
If you uncomment out the line (and thus change the default PyQt style), the QSplitter visualizes correctly only when hovered over... however, I also don't want this other style.
Can anyone provide any guidance on this matter?
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
L_layout = QGridLayout()
R_layout = QGridLayout()
L_widgets = QWidget()
L_widgets.setLayout(L_layout)
R_widgets = QWidget()
R_widgets.setLayout(R_layout)
topleft = QFrame()
topleft.setFrameShape(QFrame.StyledPanel)
btn1 = QPushButton('btn1')
bottom = QFrame()
bottom.setFrameShape(QFrame.StyledPanel)
textedit = QTextEdit()
L_layout.addWidget(topleft, 0, 0, 1, 1)
L_layout.addWidget(btn1, 1, 0, 1, 1)
R_layout.addWidget(textedit)
splitter1 = QSplitter(Qt.Horizontal,frameShape=QFrame.StyledPanel,frameShadow=QFrame.Plain)
splitter1.addWidget(L_widgets)
splitter1.addWidget(R_widgets)
splitter1.setStretchFactor(1,1)
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
#QApplication.setStyle(QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter demo')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
EDIT: This is apparently a known macOS bug. When viewed on Linux, the bar of splitter1 has the same look as splitter2. I'll leave this topic open in case anyone else knows of a suitable workaround for mac.
Because the QPushButton has default minimum size, when you want to move splitter to left,
the button has reached its minimum size. So you can not move left anymore, otherwise the left will will collapse.
So if you want the left showing as you want, you can set the minimum size off button widget.
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
L_layout = QGridLayout()
R_layout = QGridLayout()
L_widgets = QWidget()
L_widgets.setLayout(L_layout)
R_widgets = QWidget()
R_widgets.setLayout(R_layout)
topleft = QFrame()
topleft.setFrameShape(QFrame.StyledPanel)
btn1 = QPushButton('btn1')
btn1.setMinimumWidth(1) # For example : set the minimum width to 1, then you can move left until the btn1 width is 1
bottom = QFrame()
bottom.setFrameShape(QFrame.StyledPanel)
textedit = QTextEdit()
L_layout.addWidget(topleft, 0, 0, 1, 1)
L_layout.addWidget(btn1, 1, 0, 1, 1)
R_layout.addWidget(textedit)
splitter1 = QSplitter(Qt.Horizontal,frameShape=QFrame.StyledPanel,frameShadow=QFrame.Plain)
splitter1.addWidget(L_widgets)
splitter1.addWidget(R_widgets)
splitter1.setStretchFactor(1,1)
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
#QApplication.setStyle(QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter demo')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Consider this example:
#!/usr/bin/env python
import sys,os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt
class MainWindow(QtWidgets.QMainWindow):
class ScrollAreaWheel(QtWidgets.QScrollArea): # SO:9475772
def __init__(self, parent=None):
super(MainWindow.ScrollAreaWheel, self).__init__(parent)
self.parent = parent
def wheelEvent(self, event):
print("wheelEvent", event.angleDelta().y())
def __init__(self):
#~ self.do_init = QtCore.QEvent.registerEventType()
QtWidgets.QMainWindow.__init__(self)
self.setMinimumWidth(1000)
self.setMinimumHeight(400)
self.frame1 = QtWidgets.QFrame(self)
self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame1layout = QtWidgets.QGridLayout(self.frame1)
self.frame1layout.setSpacing(0);
self.frame1layout.setContentsMargins(0,0,0,0);
self.frame1widget = QtWidgets.QWidget()
self.frame1widget.setLayout(QtWidgets.QGridLayout())
self.frame1layout.addWidget(self.frame1widget)
self.frame1scroll = MainWindow.ScrollAreaWheel(self) #QtWidgets.QScrollArea()
self.frame1scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.frame1scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.frame1widget.layout().addWidget(self.frame1scroll, 0, 0) #, Qt.AlignCenter)
#self.frame1scrolllayout = QtWidgets.QHBoxLayout(self.frame1scroll)
self.frame1scrolllayout = QtWidgets.QGridLayout(self.frame1scroll)
self.frame1scroll.setWidget(self.frame1scrolllayout.widget())
self.frame1scroll.setWidgetResizable(True)
self.frame1scroll.setAlignment(Qt.AlignCenter)
self.frame1label = QtWidgets.QLabel()
self.frame1scrolllayout.addWidget(self.frame1label, 0, 0, Qt.AlignCenter) ##
pixmap = QtGui.QPixmap(200, 100)
pixmap.fill(Qt.red)
self.frame1label.setPixmap(pixmap)
self.frame2 = QtWidgets.QFrame(self)
self.frame2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame2layout = QtWidgets.QHBoxLayout(self.frame2)
self.frame2layout.setSpacing(0);
self.frame2layout.setContentsMargins(0,0,0,0);
self.frame2scroll = QtWidgets.QScrollArea(self)
self.frame2scroll.setWidgetResizable(True)
self.frame2widget = QtWidgets.QWidget()
self.frame2widget.setLayout(QtWidgets.QGridLayout())
self.frame2scroll.setWidget(self.frame2widget)
self.frame2layout.addWidget(self.frame2scroll)
self.mainwid = QtWidgets.QWidget()
self.mainwid.setLayout(QtWidgets.QGridLayout())
self.setCentralWidget(self.mainwid)
self.splitter1 = QtWidgets.QSplitter(Qt.Horizontal)
self.splitter1.addWidget(self.frame1)
self.splitter1.addWidget(self.frame2)
self.splitter1.setSizes([600, 600]); # equal splitter at start
self.mainwid.layout().addWidget(self.splitter1)
self.mainwid.layout().update()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
main = MainWindow()
main.show()
sys.exit(app.exec_())
It generates this (Ubuntu 18.04):
I want to use mousewheel only on the left QScrollArea, for which I've made a separate class. However, its wheelEvent fires only when I'm outside the red box, not when I hover over it. How can I make ScrollAreaWheel.wheelEvent fire even when mouse is over the child label (the red box)?
You are the QLabel placing on top of the QScrollArea instead of placing it inside, visually it is the same but at the level of events it is not.
from PyQt5 import QtCore, QtGui, QtWidgets
class ScrollAreaWheel(QtWidgets.QScrollArea):
def wheelEvent(self, event):
print("wheelEvent", event.angleDelta().y())
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setMinimumSize(1000, 400)
frame1 = QtWidgets.QFrame(frameShape=QtWidgets.QFrame.StyledPanel)
scrollarea1 = ScrollAreaWheel(widgetResizable=True)
scrollarea1.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollarea1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
widget1 = QtWidgets.QWidget()
scrollarea1.setWidget(widget1)
label_lay = QtWidgets.QGridLayout(widget1)
lay1 = QtWidgets.QVBoxLayout(frame1)
lay1.addWidget(scrollarea1)
pixmap = QtGui.QPixmap(200, 100)
pixmap.fill(QtCore.Qt.red)
label = QtWidgets.QLabel(pixmap=pixmap)
label_lay.addWidget(label, 0, 0, QtCore.Qt.AlignCenter)
#==============================
frame2 = QtWidgets.QFrame(frameShape=QtWidgets.QFrame.StyledPanel)
scrollarea2 = QtWidgets.QScrollArea(widgetResizable=True)
scrollarea2.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollarea2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
widget2 = QtWidgets.QWidget()
scrollarea2.setWidget(widget2)
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(frame1)
splitter.addWidget(frame2)
splitter.setSizes([600, 600])
self.setCentralWidget(splitter)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
I have 2 QPushButton in the app window: btn1 needs to be 5x the height of btn2.
Problem: Tried setting the row span of self.btn1 to 5 using layout.addWidget but the height remains unchanged. did I miss out on a setting?
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
layout = QtGui.QGridLayout()
self.btn1 = QtGui.QPushButton('Hello')
self.btn2 = QtGui.QPushButton('World')
layout.addWidget(self.btn1, 1, 1, 5, 1)
layout.addWidget(self.btn2, 6, 1, 1, 1)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
def main():
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You'll need to change the buttons' size policy:
self.btn1.setSizePolicy(
QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Expanding)
self.btn2.setSizePolicy(
QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Preferred)
From Qt doc, by default:
Button-like widgets set the size policy to specify that they may
stretch horizontally, but are fixed vertically.
i.e. The default size policy of QPushButton is Minimum horizontally, and Fixed vertically.
In addition, a simpler way to achieve what you want in the example is to use a QVBoxLayout, and set the stretch factor when calling addWidget(). i.e.
def initUI(self):
layout = QtGui.QVBoxLayout()
self.btn1 = QtGui.QPushButton('Hello')
self.btn2 = QtGui.QPushButton('World')
self.btn1.setSizePolicy(
QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Expanding)
self.btn2.setSizePolicy(
QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Preferred)
layout.addWidget(self.btn1, 5)
layout.addWidget(self.btn2, 1)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)