QHBoxLayout (size, resize, move) - python

Task:
For example (the code below) we create QHBoxLayout in which there are two buttons.
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
hbox = QHBoxLayout()
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
self.setLayout(hbox)
self.setGeometry(100, 100, 500, 500)
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Necessary:
Set the size for hbox (QHBoxLayout) (W_pix, H_pix) and its coordinates (X_pos, Y_pos) on the main window (they do not fit, I did not find such functions in the documentation for the description for QHBoxLayout, QWidget).
v.2
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import (QWidget, QPushButton,
QHBoxLayout, QApplication, QMainWindow, QVBoxLayout)
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.hbox = QVBoxLayout()
self.okButton = QPushButton("OK")
self.cancelButton = QPushButton("Cancel")
self.hbox.addWidget(self.okButton)
self.hbox.addWidget(self.cancelButton)
self.setGeometry(100, 100, 500, 500)
self.setLayout(self.hbox)
self.hbox.setGeometry(QtCore.QRect(200, 200, 300, 300))
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

A QXLayout is not a visual element, so establishing the geometry (position and size) cannot be applied directly but must be interpreted.
The task of QHBoxLayout is to distribute widgets horizontally using as information the sizeHint, sizePolicy, minimum and maximum sizes, etc. and will use the maximum available size that gives the widget where it was established.
Considering the last point you can extrapolate your requirement to that widget since as described the geometry that handles the layout is that of that widget. So in this case a QWidget used as a container is created.
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QMainWindow, QPushButton, QWidget
class Example(QMainWindow):
def __init__(self):
super().__init__()
x_pos, y_pos = 10, 10
w_pix, h_pix = 150, 150
container = QWidget(self)
container.setContentsMargins(0, 0, 0, 0)
container.setFixedSize(w_pix, h_pix)
container.move(x_pos, y_pos)
container.setStyleSheet("background-color:salmon;")
hbox = QHBoxLayout(container)
hbox.setContentsMargins(0, 0, 0, 0)
self.okButton = QPushButton("OK")
self.cancelButton = QPushButton("Cancel")
hbox.addWidget(self.okButton)
hbox.addWidget(self.cancelButton)
self.resize(640, 480)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

Related

PyQt: Multiple QGridLayout

How can I have multiple QGridLayouts on a single widget? I want to have one grid layout on the left and one on the right.
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class FormWidget(QWidget):
def __init__(self):
super(FormWidget, self).__init__( )
self.grid = QGridLayout(self)
self.grid2 = QGridLayout(self)
self.grid.addWidget(self.grid2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FormWidget()
sys.exit(app.exec_())
If you want to place 2 layouts horizontally then you must use a QHBoxLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout)
lay.addLayout(right_grid_layout)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())
Update:
If you want to set a weight you must set it in the stretch.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QTextEdit, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
# for testing
left_grid_layout.addWidget(QTextEdit())
right_grid_layout.addWidget(QTextEdit())
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout, stretch=1)
lay.addLayout(right_grid_layout, stretch=2)
lay.setContentsMargins(
0, # left
100, # top
0, # right
100 # bottom
)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())

How to set window and it's graphicScene transparent(using slider) and leave only QPushButton visible

I want my window and it's QGraphicsScene to be transparent which is controlled by QSlider.
I tried looking for some answers here and there, but it's mainly setting background color to 0 :
self.setStyleSheet("background-color:rgba(0, 0, 0, 0);")
or like that :
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
Which does it job, but I would like to make slider which will control level of transparency and I don't see how can I do it with commands above.
Can somebody please give me some advise how to do it ? Thank you
I will attach my test code here below(I want to be able to control transparency of everything , but do not touch slider and button):
from PySide2.QtGui import QBrush, QColor
from PySide2.QtCore import QSize, Qt
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QApplication, QSlider, QPushButton
class MainWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.mainLayout = QVBoxLayout()
self.graphicsWidget = MainGraphicsWidget()
self.window = 'transp_test'
self.title = 'transparent UI'
self.size = (1000, 650)
self.create()
def create(self, **kwargs):
self.setWindowTitle(self.title)
self.resize(QSize(*self.size))
self.setLayout(self.mainLayout)
self.mainLayout.addWidget(self.graphicsWidget)
class MainGraphicsWidget(QGraphicsView):
def __init__(self, parent=None):
super(MainGraphicsWidget, self).__init__(parent)
self._scene = QGraphicsScene(backgroundBrush = Qt.gray)
self.setScene(self._scene)
self.transpSlider = QSlider()
self.transpSlider.setRange(0,100)
self.mainButton = QPushButton('I want it to be "Test" button')
self._scene.addWidget(self.mainButton)
self._scene.addWidget(self.transpSlider)
self.transpSlider.move(300, 100)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
self.setFrameShape(QFrame.NoFrame)
self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(600, 300, 600, 600)
window.show()
sys.exit(app.exec_())
Here is one way.
class MainWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
# these two lines are needed to get a transparent background in windows
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# rest of class definition unchanged
class MainGraphicsWidget(QGraphicsView):
def __init__(self, parent=None):
super(MainGraphicsWidget, self).__init__(parent)
# set transparent background color for the widget itself
self.setStyleSheet("background-color: #00000000")
self._scene = QGraphicsScene()
self.setScene(self._scene)
# create of slider and connect to slot for changing opacity.
self.transpSlider = QSlider()
self.transpSlider.setStyleSheet('background-color: #00000000')
self.transpSlider.setRange(0,255)
self.transpSlider.valueChanged.connect(self.set_opacity)
self.transpSlider.setValue(255)
# rest of __init__ unchanged
def set_opacity(self, value):
brush = self.backgroundBrush()
color = brush.color()
color.setAlpha(value)
brush.setColor(color)
self.setBackgroundBrush(color)
Note that I've changed the range of the slider to 0-255 to make changing the opacity from fully opaque to fully transparent easier.
Try it:
#from PySide2.QtGui import QBrush, QColor
#from PySide2.QtCore import QSize, Qt
#from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QApplication, QSlider, QPushButton
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene,
QFrame, QSizePolicy, QApplication, QSlider, QPushButton)
class MainGraphicsWidget(QGraphicsView):
def __init__(self, parent=None):
super(MainGraphicsWidget, self).__init__(parent)
self._scene = QGraphicsScene()
self.setScene(self._scene)
self.transpSlider = QtWidgets.QSlider(
QtCore.Qt.Horizontal,
minimum=10,
maximum=100,
value=100,
valueChanged=self.onValueChanged,
)
self.mainButton = QPushButton('I want it to be "Test" button \n QUIT')
self.mainButton.resize(150, 150)
self.mainButton.clicked.connect(parent.close)
self._scene.addWidget(self.mainButton)
self._scene.addWidget(self.transpSlider)
self.transpSlider.move(300, 100)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
c = QColor(220, 30, 30)
c.setAlphaF(1)
self.setBackgroundBrush(QBrush(c))
self.setFrameShape(QFrame.NoFrame)
self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
#QtCore.pyqtSlot(int)
def onValueChanged(self, value):
c = QColor(220, 30, 30)
c.setAlphaF(value * 0.01)
self.setBackgroundBrush(QBrush(c))
window.setWindowOpacity(value * 0.03)
self.setStyleSheet("MainGraphicsWidget {{background-color: rgba(0, 215, 55, {});}}".format(value))
class MainWindow(QDialog):
def __init__(self):
super().__init__()
self.setAttribute(Qt.WA_NoSystemBackground, False)
self.setStyleSheet("MainWindow {background-color: rgba(0, 215, 55, 70);}")
self.graphicsWidget = MainGraphicsWidget(self)
self.window = 'transp_test'
self.title = 'transparent UI'
self.size = (1000, 650)
self.setWindowTitle(self.title)
self.resize(QSize(*self.size))
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
self.mainLayout.addWidget(self.graphicsWidget)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(600, 100, 600, 600)
window.show()
sys.exit(app.exec_())

PyQt5, I need not to print continuously but instead it only changes the QLabel

I need it to be not continuously printing but instead it only change the QLabel,
I dont need to add more, just whenever you write in Line edit it should replace the existing text. I need it like a stocks
This is the code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.hbox = QHBoxLayout()
self.game_name = QLabel("Stocks:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Print", self)
self.search_button.clicked.connect(self.on_click)
self.hbox.addWidget(self.game_name)
self.hbox.addWidget(self.game_line_edit)
self.hbox.addWidget(self.search_button)
self.setLayout(self.hbox)
self.show()
#pyqtSlot()
def on_click(self):
game = QLabel(self.game_line_edit.text(), self)
self.hbox.addWidget(game)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
You have to create a QLabel, set it in the layout and only update the text with setText():
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.game_name = QLabel("Stocks:")
self.game_line_edit = QLineEdit()
self.search_button = QPushButton("Print")
self.search_button.clicked.connect(self.on_click)
self.game = QLabel()
hbox = QHBoxLayout(self)
hbox.addWidget(self.game_name)
hbox.addWidget(self.game_line_edit)
hbox.addWidget(self.search_button)
hbox.addWidget(self.game)
self.show()
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Adding custom widgets to a QScrollArea in pyqt5 does not make scrollbar appear

The example shown below adds a very simple custom pyqt5 widget to a QScrollArea. When i move the custom widget "off" the area of the scroll area I would expect that scrollbars should appear. Obviously this is not the case.
#!/usr/bin/python3
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication, QScrollArea
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QPainter, QColor, QPen
import sys
class ExampleWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawWidget(qp)
qp.end()
def drawWidget(self, qp):
qp.setPen(QColor(255, 0, 0))
qp.setBrush(QColor(255, 0, 0))
qp.drawRect(0, 0, 100, 100)
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(300, 300, 300, 200)
self.scrollArea = QScrollArea(self)
self.setCentralWidget(self.scrollArea)
self.widget = ExampleWidget(self.scrollArea)
self.widget.setGeometry(250, 150, 100, 100)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
How can I make the scrollbars appear once a widget is moved so it's not visible completely anymore?
In the code that samples ExampleWidget is a child of QScrollArea: self.widget = ExampleWidget(self.scrollArea), that does not imply that the widget is handled with the properties of the QScrollArea, the QScrollArea has a viewport() that is the background widget and the QScrollArea only shows a part of it. So if you want a widget to be inside you have to use the setWidget() method. On the other hand that viewport() has the size of the content and how it calculates that size ?, by default it uses the sizeHint() of the widget, and in your case it does not have it so it will not show what you want.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class ExampleWidget(QtWidgets.QWidget):
def paintEvent(self, e):
qp = QtGui.QPainter(self)
self.drawWidget(qp)
def drawWidget(self, qp):
qp.setPen(QtGui.QColor(255, 0, 0))
qp.setBrush(QtGui.QColor(255, 0, 0))
qp.drawRect(0, 0, 100, 100)
def sizeHint(self):
return QtCore.QSize(500, 500)
class Example(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.scrollArea = QtWidgets.QScrollArea()
self.setCentralWidget(self.scrollArea)
self.widget = ExampleWidget()
self.scrollArea.setWidget(self.widget)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

Issue with QTextEdit, QPushButton one works with QMainWindow and the other with QWidget

Is there something in the QMainWindow class that won't allow to properly place a QPushButton where I want it to be?
Here the QPushButton "Quit" is at the wrong place and a little too big than usual.
My code is
import sys
from PyQt5.QtWidgets import (QApplication , QPushButton, QToolTip, QMessageBox, QMainWindow, QAction,
QHBoxLayout, QVBoxLayout, QWidget, QTextEdit)
from PyQt5.QtGui import QIcon, QFont
class QQtWidget(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = QPushButton("Quit", self)
btn.clicked.connect(self.close)
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(btn)
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(500, 500, 300, 150)
self.setWindowTitle("PyQt Test")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
f = DemoQt()
sys.exit(app.exec())
If I make the class inherit from QWidget instead of QMainWindow the PushButton will be properly placed, but I can't use some other function if I inherit from QWidget.
Another Issue is with the QTextEdit module. It seems I cannot edit anything
in it if I use this code
class OpenSavePath(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.textEdit = QTextEdit()
"I realise here that this might be the problem as there is no
setCentralWidget() module in QWidget"""
#self.setCentralWidget(self.textEdit)
save_btn = QPushButton("Save", self)
save_btn.clicked.connect(self.saveCSV)
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(save_btn)
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setGeometry(300, 300, 300, 250)
self.setWindowTitle("Rocket Packet Preview")
Notice here the QPushbutton that is also misplaced. By changing the parent class QWidget with QMainWindow and adding this following line
self.setCentralWidget(self.textEdit)
The text editor issue is gone and I can edit. But then my QPushButton appears. Is there anyway to solve this?
For the first problem: You must use QSpacerItem to place it in a corner.
Example:
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QSizePolicy, QSpacerItem, QPushButton
import sys
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
vbox = QVBoxLayout(self)
spacerItem = QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
vbox.addItem(spacerItem)
hbox = QHBoxLayout()
spacerItem1 = QSpacerItem(10, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
hbox.addItem(spacerItem1)
btn = QPushButton("Quit", self)
btn.clicked.connect(self.close)
hbox.addWidget(btn)
vbox.addLayout(hbox)
self.setGeometry(300, 300, 300, 250)
self.setWindowTitle("Rocket Packet Preview")
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec())
For this second problem: You must use QSpacerItem, QVBoxLayout, QHBoxLayout to organize the items. QMainWindow have loaded default widget like QStatusBar, QDockWidget, etc. So the setCentralWidget() function is used to place the main widget.
Example:
from PyQt5.QtWidgets import QWidget, QSpacerItem, QSizePolicy, QTextEdit, QApplication, QVBoxLayout, QHBoxLayout, QPushButton
import sys
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
vbox = QVBoxLayout(self)
self.textEdit = QTextEdit(self)
vbox.addWidget(self.textEdit)
hbox = QHBoxLayout()
spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
hbox.addItem(spacerItem)
save_btn = QPushButton("Save", self)
hbox.addWidget(save_btn)
vbox.addLayout(hbox)
self.setGeometry(300, 300, 300, 250)
self.setWindowTitle("Rocket Packet Preview")
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec())

Categories