I'm implementing an application, where a side widget can expand/shrink, so an another widget must shrink/expand. (Or the side widget can be shown over that widget, it doesn't matter, I accept both implementations). It can looks like that:
Here is a part of my code:
class AppView:
def __init__(self):
self._mainWindow = QDialog(None)
self._schedule = ScheduleView(self._mainWindow)
self._schedule.setMinimumWidth(25)
self._schedule.setMaximumWidth(250)
self._tutorial = TutorialView(self._mainWindow)
self._schedule.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self._tutorial.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
layout = QHBoxLayout()
layout.addWidget(self._schedule)
layout.addWidget(self._tutorial)
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 1)
self._mainWindow.setLayout(layout)
class TutorialView(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self._presenter = TutorialPresenter(self)
self.reqReprSections.connect(self.setModel)
self.reqReprTopics.connect(self.setModel)
self._widget = QQuickWidget(self)
self._widget.rootContext().setContextProperty('tutorialView', self)
self._widget.setSource(QUrl('modules/manual/manualForm/TutorialForm.qml'))
class ScheduleView(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self._presenter = SchedulePresenter(self)
self._widget = QQuickWidget(self)
self._widget.setResizeMode(QQuickWidget.SizeViewToRootObject)
self._widget.rootContext().setContextProperty('scheduleView', self)
self._widget.rootContext().setContextProperty('groupsModel', self)
self._widget.setSource(QUrl('modules/schedule/scheduleForm/ScheduleForm.qml'))
How can I do such resizes in code?
To get that behavior you can use a QHBoxLayout by embedding a rotated button in the middle of the side widgets. You must change the left widget's size policy so that it does not expand.
To implement the rotated button you must override the paintEvent method, in addition to modifying the size policy so that it expands vertically and not horizontally.
class ShrinkExpandButton(QPushButton):
def __init__(self, *args, **kwargs):
QPushButton.__init__(self, *args, **kwargs)
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
self.setFixedWidth(2*self.fontMetrics().height())
def paintEvent(self, event):
painter = QStylePainter(self)
painter.rotate(-90)
painter.translate(-self.height(), 0)
option = QStyleOptionButton()
self.initStyleOption(option)
size = option.rect.size()
size.transpose()
option.rect.setSize(size)
painter.drawControl(QStyle.CE_PushButton, option)
class ShrinkExpandWidget(QWidget):
def __init__(self, leftWidget, rightWiget, text, parent=None):
QWidget.__init__(self, parent)
button = ShrinkExpandButton(text, self)
self.setLayout(QHBoxLayout())
self.layout().setSpacing(0)
self.layout().addWidget(leftWidget)
self.layout().addWidget(button)
self.layout().addWidget(rightWiget)
leftWidget.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
button.clicked.connect(lambda: leftWidget.setVisible(not leftWidget.isVisible()))
Example:
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
listWidget = QListWidget()
for i in range(20):
listWidget.addItem("{}".format(i))
tableWidget = QTableWidget()
tableWidget.setColumnCount(10)
tableWidget.setRowCount(20)
for i in range(tableWidget.rowCount()):
for j in range(tableWidget.columnCount()):
tableWidget.setItem(i, j, QTableWidgetItem("({}, {})".format(i, j)))
listWidget.setFixedWidth(240)
w = ShrinkExpandWidget(listWidget, tableWidget, "Shrink - Expand")
w.resize(720, 480)
w.show()
sys.exit(app.exec_())
Output:
Related
I tried to make some simple local chatting app for practice. I make a button to send a message every time i push the button the message displayed but the scrollarea become more narrow and not expanded the scrollarea. Am I missing something or added something i shouldn't in my code? how can i fix this?
Here is my code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Bubble(QLabel):
def __init__(self,text):
super(Bubble,self).__init__(text)
self.setContentsMargins(5,5,5,5)
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing,True)
p.drawRoundedRect(0,0,self.width()-1,self.height()-1,5,5)
super(Bubble,self).paintEvent(e)
class MyWidget(QWidget):
def __init__(self,text,left=True):
super(MyWidget,self).__init__()
hbox = QHBoxLayout()
label = Bubble(text)
if not left:
hbox.addSpacerItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Preferred))
hbox.addWidget(label)
if left:
hbox.addSpacerItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Preferred))
hbox.setContentsMargins(0,0,0,0)
self.setLayout(hbox)
self.setContentsMargins(0,0,0,0)
class Chatting(QWidget):
def __init__(self, parent=None):
super(Chatting, self).__init__(parent)
self.vbox = QVBoxLayout()
for _ in range(20):
self.vbox.addWidget(MyWidget("Left side"))
widget = QWidget()
widget.setLayout(self.vbox)
scroll = QScrollArea()
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(False)
scroll.setWidget(widget)
#Scroll Area Layer add
vLayout = QVBoxLayout(self)
vLayout.addWidget(scroll)
send = QPushButton('')
send.setIcon(QIcon('images/send.png'))
send.setStyleSheet("background-color:#d00001; width: 44px")
send.setIconSize(QSize(84,20))
send.clicked.connect(self.send_messages)
vLayout.addWidget(send)
self.setLayout(vLayout)
def send_messages(self):
self.vbox.addWidget(MyWidget('testing'))
The problem is simple you have to enable the widgetResizable property to True since that property allows the QScrollArea to calculate the size of the content.
scroll.setWidgetResizable(True)
On the other hand I have taken the time to improve your code and I show it below:
from PyQt4 import QtGui, QtCore
class Bubble(QtGui.QLabel):
def __init__(self, text):
super(Bubble,self).__init__(text)
self.setContentsMargins(5, 5, 5, 5)
def paintEvent(self, e):
p = QtGui.QPainter(self)
p.setRenderHint(QtGui.QPainter.Antialiasing, True)
p.drawRoundedRect(self.rect().adjusted(0, 0, -1, -1), 5, 5)
super(Bubble, self).paintEvent(e)
class MyWidget(QtGui.QWidget):
def __init__(self, text, left=True):
super(MyWidget,self).__init__()
lay = QtGui.QVBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
self.setContentsMargins(0, 0, 0, 0)
label = Bubble(text)
lay.addWidget(label, alignment= QtCore.Qt.AlignLeft if left else QtCore.Qt.AlignRight)
class Chatting(QtGui.QWidget):
def __init__(self, parent=None):
super(Chatting, self).__init__(parent)
widget = QtGui.QWidget()
self.vbox = QtGui.QVBoxLayout(widget)
self.vbox.addStretch()
self.scroll = QtGui.QScrollArea(widgetResizable=True)
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scroll.setWidget(widget)
#Scroll Area Layer add
send = QtGui.QPushButton(icon= QtGui.QIcon('images/send.png'))
send.setStyleSheet("background-color:#d00001; width: 44px")
send.setIconSize(QtCore.QSize(84,20))
send.clicked.connect(self.on_clicked)
vLayout = QtGui.QVBoxLayout(self)
vLayout.addWidget(self.scroll)
vLayout.addWidget(send)
def send_message(self, text, direction=True):
widget = MyWidget(text, direction)
self.vbox.insertWidget(self.vbox.count()-1, widget)
scroll_bar = self.scroll.verticalScrollBar()
# move to end
QtCore.QTimer.singleShot(10, lambda: scroll_bar.setValue(scroll_bar.maximum()))
#QtCore.pyqtSlot()
def on_clicked(self):
self.send_message("testing")
if __name__ == '__main__':
import sys
import random
app = QtGui.QApplication(sys.argv)
w = Chatting()
def test():
for _ in range(8):
w.send_message("testing", random.choice((True, False)))
QtCore.QTimer.singleShot(1000, test)
w.resize(240, 480)
w.show()
sys.exit(app.exec_())
Current design shown below of QgraphicsView and QMainWindow class is an example of the design I have in a different software.
I had to add scrollbars to the QGraphicsView.
The original software has all mouse events handled in QMainWindow.
Questions: What is the way to draw on QGraphicsView through QMainWindow?
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
print "sdsads"
def mousePressEvent(self, ev):
item = QtGui.QGraphicsTextItem("")
item.setPos(ev.x(), ev.y())
self.scene.addItem(item)
print "ev.x() ", ev.x()
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
In a QGraphicsView it is normal to add items to the scene, for example in case you want to draw a polygon you must use QGraphicsPolygonItem, also if you want to get correct points you must use QGraphicsScene instead of QGraphicsView.
In the following example you can indicate the polygon points by left clicking and finish the drawing with the right click.
import sys
from PyQt4 import QtCore, QtGui
class GraphicsScene(QtGui.QGraphicsScene):
def __init__(self, *args, **kwargs):
QtGui.QGraphicsScene.__init__(self, *args, **kwargs)
self.polygon = None
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
self.polygon << ev.scenePos()
item = QtGui.QGraphicsPolygonItem(self.polygon)
item.setPen(QtGui.QPen(QtCore.Qt.red))
item.setBrush(QtGui.QBrush(QtCore.Qt.red))
self.addItem(item)
# or
# self.addPolygon(self.polygon, QtGui.QPen(QtCore.Qt.red), QtGui.QBrush(QtCore.Qt.red))
self.polygon = None
else:
if self.polygon is None:
self.polygon = QtGui.QPolygonF()
self.polygon << ev.scenePos()
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene =GraphicsScene(QtCore.QRectF(0, 0, 640, 480), self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
Output:
You have an XY problem, where you are looking for the solution for a solution of the main problem without knowing that it is the correct one, according to what you comment your main problem is to add QScrollBar to the QMainWindow, and in that element you want to make drawings, so for that it is not necessary to use a QGraphicsView but a QScrollArea.
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.polygon = None
self.setFixedSize(640, 480)
self.pixmap = None
def mousePressEvent(self, ev):
if self.polygon is None:
self.polygon = QtGui.QPolygon()
self.polygon << ev.pos()
self.update()
def paintEvent(self, ev):
painter = QtGui.QPainter(self)
painter.fillRect(self.rect(), QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
painter.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap("exit.png"))
if self.polygon is not None:
painter.setPen(QtCore.Qt.blue)
painter.drawPolyline(self.polygon)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
scroll = QtGui.QScrollArea()
scroll.setBackgroundRole(QtGui.QPalette.Dark)
scroll.setWidget(Window())
scroll.setWidgetResizable(True)
self.setCentralWidget(scroll)
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
I'm trying to change the background image of the QMainWindow central widget. The QMainWindow's background is fairly easy to change but I can't get the same result with it's central widget. What I tried is the code
self.mdi = QMdiArea()
self.options_window = Options()
self.central_widget = QStackedWidget()
self.mdi.setStyleSheet("{background-image: url(ninja.png);}")
self.setCentralWidget(self.central_widget)
self.central_widget.addWidget(self.mdi)
self.central_widget.addWidget(self.options_window)
self.central_widget.setCurrentWidget(self.mdi)
I also tried with this one
self.mdi = QMdiArea()
self.options_window = Options()
self.central_widget = QStackedWidget()
self.central_widget.setStyleSheet("{background-image: url(ninja.png);}")
self.setCentralWidget(self.central_widget)
self.central_widget.addWidget(self.mdi)
self.central_widget.addWidget(self.options_window)
self.central_widget.setCurrentWidget(self.mdi)
Could anyone give me a light in this problem?
To change the background image of a QWidget you should override the paintEvent method, in your case yours in a QStackedWidget, we create a class that inherits from this:
class StackedWidget(QStackedWidget):
def __init__(self, parent=None):
QStackedWidget.__init__(self, parent=parent)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), QPixmap("ninja.png"))
QStackedWidget.paintEvent(self, event)
And then you change:
self.central_widget = QStackedWidget()
to:
self.central_widget = StackedWidget()
Example:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class StackedWidget(QStackedWidget):
def __init__(self, parent=None):
QStackedWidget.__init__(self, parent=parent)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), QPixmap("image.png"))
QStackedWidget.paintEvent(self, event)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent=parent)
self.setCentralWidget(StackedWidget())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Screenshot:
The case of QStackedWidget is a special case since this is not shown, this serves to show other widgets, to those widgets you must change the background image.
From your code, the first widget they attach is a QMdiArea, this is also a special case since it has a viewport and this should be changed.
class MdiArea(QMdiArea):
def __init__(self, parent=None):
QMdiArea.__init__(self, parent=parent)
def paintEvent(self, event):
QMdiArea.paintEvent(self, event)
painter = QPainter(self.viewport())
painter.drawPixmap(self.rect(), QPixmap("image.png"))
In your code change:
self.mdi = QMdiArea()
to:
self.mdi = MdiArea()
Screenshots:
I am getting wrong layout in PyQT5. What am I doing wrong? Is there some predefined small field size or similar? I created main window as QMainWindow and inside it a widget as central widget. This is how it looks like:
class Main(QWidget):
"""The main widget with label and LineEdit"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize the UI of the main widget"""
self.mySourceLabel = QLabel("Select your file:")
self.mySourceLine = QLineEdit()
self.mySourceLine.setPlaceholderText("File name here")
# Set layout
grid = QGridLayout()
#grid.setSpacing(5)
grid.addWidget(self.mySourceLabel, 0, 0)
grid.addWidget(self.mySourceLine, 1, 0)
self.setLayout(grid)
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize UI of an application"""
# main window size, title
self.setGeometry(400, 300, 400, 300)
self.setWindowTitle("Version upgrade ")
# create instance of a class Main
self.main = Main(self)
# create central widget, create grid layout
centralWidget = QWidget()
centralLayout = QGridLayout()
centralWidget.setLayout(centralLayout)
When you pass the parent to a QWidget this will locate a position with respect to its parent and generate widgets like the ones you have obtained, to solve this, layouts are used, QMainWindow is a special QWidget since it has predefined elements, so it already has a layout:
In QMainWindow the widget must be added to the centralwidget with the setCentralWidget function, in your case:
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
[...]
centralWidget = Main(self)
self.setCentralWidget(centralWidget)
complete code:
class Main(QWidget):
"""The main widget with label and LineEdit"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize the UI of the main widget"""
self.mySourceLabel = QLabel("Select your file:")
self.mySourceLine = QLineEdit()
self.mySourceLine.setPlaceholderText("File name here")
# Set layout
grid = QGridLayout()
#grid.setSpacing(5)
grid.addWidget(self.mySourceLabel, 0, 0)
grid.addWidget(self.mySourceLine, 1, 0)
self.setLayout(grid)
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize UI of an application"""
# main window size, title
self.setGeometry(400, 300, 400, 300)
self.setWindowTitle("Version upgrade ")
# create central widget, create grid layout
centralWidget = Main(self)
self.setCentralWidget(centralWidget)
Screenshot:
i want to give index for QLineEdit's.
i have this code.
from PyQt4 import QtGui, QtCore
import sys
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
# main button
self.addButton = QtGui.QPushButton('button to add other widgets')
self.addButton.clicked.connect(self.addWidget)
self.savebutton = QtGui.QPushButton('Save')
# scroll area widget contents - layout
self.scrollLayout = QtGui.QFormLayout()
# scroll area widget contents
self.scrollWidget = QtGui.QWidget()
self.scrollWidget.setLayout(self.scrollLayout)
# scroll area
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setWidget(self.scrollWidget)
# main layout
self.mainLayout = QtGui.QVBoxLayout()
# add all main to the main vLayout
self.mainLayout.addWidget(self.addButton)
self.mainLayout.addWidget(self.scrollArea)
self.mainLayout.addWidget(self.savebutton)
# central widget
self.centralWidget = QtGui.QWidget()
self.centralWidget.setLayout(self.mainLayout)
# set central widget
self.setCentralWidget(self.centralWidget)
def addWidget(self):
self.scrollLayout.addRow(Test())
class Test(QtGui.QWidget):
def __init__( self, parent=None):
super(Test, self).__init__(parent)
self.kod = QtGui.QLineEdit()
layout = QtGui.QHBoxLayout()
layout.addWidget(self.kod)
self.setLayout(layout)
app = QtGui.QApplication(sys.argv)
myWidget = Main()
myWidget.show()
app.exec_()
when i clicked save button, savebutton sends just last QLineEdit widget.
image is here
like in this photo, i want self.kod[0].text()="aaaa" self.kod1="bbbb" self.kod[2]="cccc" and it just will go like this. kod[x] this x number will increase automatically, while i click add widgetbutton. or it can be like this: kod1,kod2,kod3, kodx. it doesnt matter, i want to just differ from eacht other and take text from them.
You can set a list in Main class, like this ['aaa', 'bbb', 'ccc'],
and set a var = 0.
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
#
self.lineText = ['aaa', 'bbb', 'ccc']
self.var = 0
...
def addWidget(self):
self.scrollLayout.addRow(Test(self, self.var))
self.var += 1
Then give Test class an arg,
class Test(QtGui.QWidget):
def __init__( self, parent=None, count):
super(Test, self).__init__(parent)
#
self.parent = parent
self.kod = QtGui.QLineEdit()
#
self.kod.setText(self.parent.lineText[count])
---
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
self.kod = []
...
def addWidget(self):
temp = Test()
self.kod.append(temp)
self.scrollLayout.addRow(temp)
and print(self.kod)
[<__main__.Test object at 0x00000000032EEC18>, <__main__.Test object at 0x00000000032EEDC8>, <__main__.Test object at 0x00000000032EEF78>]