Trigger a paint event inside the enter event in Qt - python

Q: I am trying to trigger a paint event inside the enter event in Qt but I am getting an error, basically can't call the painter inside the enter mouse event. What I need is to darken the image(as button) as I hover the mouse. Is this even possible? Thank you.
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
class PicButton(QtWidgets.QAbstractButton):
def __init__(self, pixmap, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), self.pixmap)
painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,0)))
def enterEvent(self, event):
#error with the line below: 'PySide2.QtGui.QEnterEvent' object has no attribute 'rect'
#painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,128)))
print('hovering')
def sizeHint(self):
return self.pixmap.size()
window_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()
myPixmap = QtGui.QPixmap("image.jpg")
my_button = PicButton(myPixmap)
my_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
my_button.setMaximumSize(200,100)
vlayout_wid.addWidget(my_button)
window_wid.setLayout(vlayout_wid)
window_wid.show()

Ok, I solved the problem with underMouse() within the paintEvent. Not sure if it's the best solution but it's working now. Just wish I was able to brighten the image instead of darkening.
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
class PicButton(QtWidgets.QAbstractButton):
def __init__(self, pixmap, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pressed.connect(self.update)
self.released.connect(self.update)
def paintEvent(self, event):
#pix = self.pixmap_hover if self.underMouse() else self.pixmap
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), self.pixmap)
pix = painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,128))) if self.underMouse() else self.pixmap
def enterEvent(self, event):
self.update()
def leaveEvent(self, event):
self.update()
def sizeHint(self):
return QtCore.QSize(200, 100)
window_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()
pixmap = QtGui.QPixmap("image.jpg")
my_button = PicButton(pixmap)
my_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
#my_button.setMaximumSize(200,100)
vlayout_wid.addWidget(my_button)
window_wid.setLayout(vlayout_wid)
window_wid.show()

Related

Python Qt - Connect function to each button

I am trying to build an hdri browser in Maya with python/Qt but I have limited knowledge on this subject. Basically I am trying to print the path of each "jpg" on a folder.
I built an UI with buttons as images and when I click on them I need them to print the respective path.
Ex: Button_01 > prints D:/Hdris/HDRI_01.jpg, Button_02 > prints D:/Hdris/HDRI_02.jpg, etc...
I understand that my code is a bit of a mess right now, that's why I can't connect the button press to the loop I have where I create the buttons. How can I organize this better?
Thank you.
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import glob, os
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance( int(main_window_ptr), QtWidgets.QWidget )
class PicButton(QtWidgets.QAbstractButton):
def __init__(self, pixmap, parent=None ):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pressed.connect( self.press )
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), self.pixmap)
pix = painter.fillRect( event.rect(), QtGui.QBrush( QtGui.QColor (150,150,150,128) ) ) if self.underMouse() else self.pixmap
def enterEvent(self, event):
self.update()
def leaveEvent(self, event):
self.update()
def press(self):
print('Button image Pressed! To do...')
def sizeHint(self):
return QtCore.QSize(200, 100)
class TestDialog(QtWidgets.QDialog):
def __init__(self, parent=maya_main_window()):
super(TestDialog, self).__init__(parent)
self.setWindowTitle('Test Dialog')
self.setMinimumWidth(200)
self.create_widgets()
def create_widgets(self):
main_layout = QtWidgets.QVBoxLayout(self)
pathToHdris = "D:/CG_CONTENT/HDRIS/HDRI_BROWSER/"
os.chdir(pathToHdris)
for item, hdri in enumerate( glob.glob("*.jpg") ):
newPath = pathToHdris+str(hdri)
pixmap = QtGui.QPixmap(newPath)
self.my_button = PicButton(pixmap)
self.my_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.my_button.setMaximumSize(200,100)
main_layout.addWidget(self.my_button)
if __name__ == "__main__":
try:
test_dialog.close()
test_dialog.deleteLater()
except:
pass
test_dialog = TestDialog()
test_dialog.show()

pyqt add rectangle in Qgraphicsscene

I have a scene like this
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
def mousePressEvent(self, event):
print('scene pressed')
self.wid = MyRect(event.pos(), event.pos())
self.addItem(self.wid)
self.wid.show()
I would like class MyRect(QtWidgets.QGraphicsRectItem) with painter, mouse event and so on to be a draggable rectangle.
all stuff in MyRect
So then I could have many Rectangle to the scene and even after draw line between them and so on (kind of diagram app), but keeping objects related editable options in MyRect, MyLine , ....
I thought :
class MyRect(QtWidgets.QGraphicsRectItem):
def __init__(self, begin, end, parent=None):
super().__init__(parent)
self.begin = begin
self.end = end
def paintEvent(self, event):
print('painting')
qp = QtGui.QPainter(self)
qp.drawRect(QtCore.QRect(self.begin, self.end))
def mousePressEvent(self, event):
self.begin = event.pos()
self.end = event.pos()
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.begin = event.pos()
self.end = event.pos()
self.update()
But I does not work (paint event not initiated whereas mousepressed event in scene is intiated)
I did not find what I wanted through the web so started totry do it by myself. I'm pretty sure it is a must known starting point but I cannot find it
First of all a QGraphicsItem is not a QWidget, so it has those events and does not handle them directly, that's what QGraphicsView and QGraphicsScene do. For example you say that you want to have a moveable rectangle because that task is simple is QGraphicsView, it is not necessary to overwrite:
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)
rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))
rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
scene.addItem(rect_item)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
If you want to change the way you paint the rectangle you must overwrite the paint() method as shown below:
from PyQt5 import QtCore, QtGui, QtWidgets
class RectItem(QtWidgets.QGraphicsRectItem):
def paint(self, painter, option, widget=None):
super(RectItem, self).paint(painter, option, widget)
painter.save()
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setBrush(QtCore.Qt.red)
painter.drawEllipse(option.rect)
painter.restore()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)
rect_item = RectItem(QtCore.QRectF(0, 0, 100, 100))
rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
scene.addItem(rect_item)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Update:
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsScene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(GraphicsScene, self).__init__(QtCore.QRectF(-500, -500, 1000, 1000), parent)
self._start = QtCore.QPointF()
self._current_rect_item = None
def mousePressEvent(self, event):
if self.itemAt(event.scenePos(), QtGui.QTransform()) is None:
self._current_rect_item = QtWidgets.QGraphicsRectItem()
self._current_rect_item.setBrush(QtCore.Qt.red)
self._current_rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.addItem(self._current_rect_item)
self._start = event.scenePos()
r = QtCore.QRectF(self._start, self._start)
self._current_rect_item.setRect(r)
super(GraphicsScene, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self._current_rect_item is not None:
r = QtCore.QRectF(self._start, event.scenePos()).normalized()
self._current_rect_item.setRect(r)
super(GraphicsScene, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self._current_rect_item = None
super(GraphicsScene, self).mouseReleaseEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene =GraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to draw on QGraphicsView from QMainWindow

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_())

How to change the background image of a QMainWindow central widget?

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:

PyQt Irregularly Shaped Windows (e.g. A circular without a border/decorations)

How do I create an irregularly shaped window in PyQt?
I found this C++ solution, however I am unsure of how to do that in Python.
Here you go:
from PyQt4 import QtGui, QtWebKit
from PyQt4.QtCore import Qt, QSize
class RoundWindow(QtWebKit.QWebView):
def __init__(self):
super(RoundWindow, self).__init__()
self.initUI()
def initUI(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
def sizeHint(self):
return QSize(300,300)
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
qp.setRenderHint(QtGui.QPainter.Antialiasing);
qp.setPen(Qt.NoPen);
qp.setBrush(QtGui.QColor(255, 0, 0, 127));
qp.drawEllipse(0, 0, 300, 300);
qp.end()
a = QtGui.QApplication([])
rw = RoundWindow()
rw.show()
a.exec_()
Screenshot
I've never written C++ in my life, but reading that code example was not that hard. You'll find that most Qt documentation online is in C++, so it's useful to at least be able to read.
Here is a PyQT5 example, which creates frameless, movable QWidget, with transparent png mask to generate irregularly shaped Window:
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint
class IrregularWindow(QtWidgets.QWidget):
def __init__(self):
super(IrregularWindow, self).__init__()
self.initUI()
def initUI(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
def sizeHint(self):
return QSize(107, 41) # Set this to the exact image resolution
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
pixmap = QtGui.QPixmap()
pixmap.load('image_with_transparency.png')
qp.drawPixmap(QPoint(0, 0), pixmap)
qp.end()
def mousePressEvent(self, event):
self.oldPos = event.globalPos()
def mouseMoveEvent(self, event):
delta = QPoint(event.globalPos() - self.oldPos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.oldPos = event.globalPos()
a = QtWidgets.QApplication([])
rw = IrregularWindow()
rw.show()
a.exec_()

Categories