How to move picbutton? Pyqt5 python - python

I want to make a button from a picture.
I found this code in here, but i cant move the picture, how can i do that?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class PicButton(QAbstractButton):
def __init__(self, pixmap, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
def paintEvent(self, event):
painter = QPainter(self)
# painter.drawPixmap(100,1, self.pixmap)
painter.drawPixmap(500,1,100,100, self.pixmap,0,0,100,100)
def sizeHint(self):
return self.pixmap.size()
def close():
print("xxx")
app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout(window)
window.setFixedSize(740, 850) #window size.
button = PicButton(QPixmap("thinking.png"))
button.clicked.connect(close)
layout.addWidget(button)
window.show()
sys.exit(app.exec_())
I can move in x axis but cant in y axis.
How can i do that?
And is there any cursor style to make this more like button?
Thank you.
here is output

Related

QPaintEvent event rect for scrollable widget

I have a QPaintEvent override for a custom widget that has a fixed size set. This fixed size can change per instance but in this simple example, ive set it. however the PaintEvent doesn't take it into account so when the users scrolls to the right the rectangle shouldn't paint rounded corners since the widget extends past the visible viewport. How do i fix this?
Full widget painted correctly...
When i resize dialog and scroll right, you'll see rounded corners appear on the left side... when it should NOT.
They should look like this...
Code
import os
import sys
from PySide2 import QtGui, QtWidgets, QtCore, QtSvg
class Card(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Card, self).__init__(parent=parent)
self.label = QtWidgets.QLabel('Help This Paint Event Is Broken')
self.label.setFixedHeight(40)
self.label.setFixedWidth(300)
self.mainLayout = QtWidgets.QVBoxLayout(self)
self.mainLayout.addWidget(self.label)
# overrides
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
painter.setOpacity(1.0)
painter.setRenderHints(QtGui.QPainter.Antialiasing)
painter.setPen(QtGui.QColor(0, 0, 0, 128))
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtGui.QColor('#F44336'))
painter.drawRoundedRect(event.rect(), 12, 12)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.end()
class ListViewExample(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ListViewExample, self).__init__(parent)
self.resize(200,200)
self.listView = QtWidgets.QListWidget()
self.listView.setSpacing(10)
self.listView.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
self.listView.verticalScrollBar().setSingleStep(10)
# layout
self.mainLayout = QtWidgets.QVBoxLayout()
self.mainLayout.setContentsMargins(0,0,0,0)
self.mainLayout.addWidget(self.listView)
self.setLayout(self.mainLayout)
for x in range(50):
wgt = Card()
self.appendItem(wgt)
def appendItem(self, widget):
lwi = QtWidgets.QListWidgetItem()
lwi.setSizeHint(widget.sizeHint())
self.listView.addItem(lwi)
self.listView.setItemWidget(lwi, widget)
################################################################################
# Widgets
################################################################################
def unitTest_CardDelegate():
app = QtWidgets.QApplication(sys.argv)
window = ListViewExample()
window.show()
app.exec_()
if __name__ == '__main__':
pass
unitTest_CardDelegate()
QPaintEvent::rect() returns the visible rectangle, not the rectangle of the widget itself, so you observe this behavior. The solution is:
painter.drawRoundedRect(self.rect(), 12, 12)

How create a square at any position of canvas that can be moved at any position on the canvas

I have created a square at random position in canvas, but I don't know how do I move it to somewhere else in canvas by dragging it to desired position, please suggest some edits or a new method to achieve the proposed task, I am learning while doing so.
P.S. Attached a screenshot of the output window.
import sys
from random import randint
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QPushButton,QWidget
from PyQt5 import QtGui
from PyQt5.QtCore import QRect,Qt
from PyQt5.QtGui import QPainter,QBrush, QPen
from PyQt5 import QtCore
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
title="TeeSquare"
left=500
top=200
width=500
height=400
iconName="square.jpg"
self.setWindowTitle(title)
self.setWindowIcon(QtGui.QIcon(iconName))
self.setGeometry(left, top, width, height)
self.should_paint_Rect = False
self.windowcomponents()
self.initUI()
self.show()
def initUI(self):
if self.should_paint_Rect:
self.label=QtWidgets.QLabel(self)
self.label.setText("circle")
def windowcomponents(self):
button=QPushButton("Add", self)
button.setGeometry(QRect(0, 0, 50, 28))
button.setIcon(QtGui.QIcon("Add.png"))
button.setToolTip("Create Square")
button.clicked.connect(self.paintRect)
def paintEvent(self, event):
super().paintEvent(event)
if self.should_paint_Rect:
painter = QtGui.QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
painter.drawRect(randint(0,500), randint(0,500), 100, 100)
self.initUI()
self.label.move(60,100)
def paintRect(self, painter):
self.should_paint_Rect = True
self.update()
app = QApplication(sys.argv)
Rect=Window()
Rect.show()
sys.exit(app.exec_())
The logic of creating a dynamic element is to indicate a set of specific characteristics that by modifying these, the element is modified.
In this case you could use the center of the square, the dimensions of the square, etc. and that data must be implemented through a data structure that can be created from scratch for example by creating a class that has the information of the rectangle, but in Qt it is not necessary to create that element since it already exists and is QRect.
Now that that element has been identified, you can create a QRect whose top-left is random when the button is pressed, and use that QRect to paint it.
For dragging the procedure is:
Get the mouse click position.
Verify that the click is inside the rectangle.
Calculate the position relative to the rectangle.
When moving the mouse, the position of the rectangle must be updated based on the position of the mouse press.
Considering all of the above, the solution is:
import random
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.rect = QtCore.QRect()
self.drag_position = QtCore.QPoint()
button = QtWidgets.QPushButton("Add", self)
button.clicked.connect(self.on_clicked)
self.resize(640, 480)
#QtCore.pyqtSlot()
def on_clicked(self):
if self.rect.isNull():
self.rect = QtCore.QRect(
QtCore.QPoint(*random.sample(range(200), 2)), QtCore.QSize(100, 100)
)
self.update()
def paintEvent(self, event):
super().paintEvent(event)
if not self.rect.isNull():
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtGui.QPen(QtCore.Qt.black, 5, QtCore.Qt.SolidLine))
painter.drawRect(self.rect)
def mousePressEvent(self, event):
if self.rect.contains(event.pos()):
self.drag_position = event.pos() - self.rect.topLeft()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if not self.drag_position.isNull():
self.rect.moveTopLeft(event.pos() - self.drag_position)
self.update()
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.drag_position = QtCore.QPoint()
super().mouseReleaseEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Rect = Window()
Rect.show()
sys.exit(app.exec_())

Mouseover Event in PyQt5

I want to get the position of mouse while it's hovering over a label. I read this but my problem is different. I need to grab the mouse position as it hovers over my label without clicking so, mouseMoveEvent doesn't help
here's my code:
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.WindowGUI()
self.level = "Image Not Loaded Yet"
self.mouseIsClicked = False
self.top = 90
self.left = 90
self.height = 1800
self.width = 1800
self.setGeometry(self.top, self.left, self.height, self.width)
self.setWindowTitle("Manual Contact Andgle")
self.setMouseTracking(True)
mainWidget = QWidget(self)
self.setCentralWidget(mainWidget)
mainWidget.setLayout(self.finalVbox)
self.show()
def WindowGUI(self):
self.finalVbox = QVBoxLayout() # Final Layout
self.mainHBox = QHBoxLayout() # Hbox for picLable and Buttons
self.mainVBox = QVBoxLayout() # VBox for two Groupboxes
self.lineVBox = QVBoxLayout() # VBox For Line Drawing Buttons
self.fileVBox = QVBoxLayout() # VBox for file Loading and Saving Buttons
self.lineGroupbox = QGroupBox("Drawing") # GroupBox For Line Drawing Buttons
self.fileGroupbox = QGroupBox("File") # GroupBox for File Loading and Saving Buttons
self.picLable = Label(self) # Lable For showing the Image
self.piclable_pixmap = QPixmap("loadImage.png") # Setting Pixmap
self.picLable.setPixmap(self.piclable_pixmap) # setting pixmap to piclable
def mouseMoveEvent(self, QMouseEvent):
print(QMouseEvent.pos())
If you want to detect the mouse position without pressing on the widget then you must enable mouseTracking that will make the mouseMoveEvent invoked when the mouse is pressed or not, if you want to verify that it is not pressed you must use the buttons() method:
import sys
from PyQt5 import QtWidgets
class Label(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
def mouseMoveEvent(self, event):
if not event.buttons():
print(event.pos())
super().mouseMoveEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Label()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
UPDATE:
Mouse events are propagated from children to parents if the children do not consume it, that is, if the child consumes it then the parent cannot consume it. So the QLabel is consuming that event so the window will not be notified, so in this case an eventFilter should be used:
import sys
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QEvent, QObject, QPoint
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
class HoverTracker(QObject):
positionChanged = pyqtSignal(QPoint)
def __init__(self, widget):
super().__init__(widget)
self._widget = widget
self.widget.setMouseTracking(True)
self.widget.installEventFilter(self)
#property
def widget(self):
return self._widget
def eventFilter(self, obj, event):
if obj is self.widget and event.type() == QEvent.MouseMove:
self.positionChanged.emit(event.pos())
return super().eventFilter(obj, event)
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Manual Contact Andgle")
self.picLable = QLabel(self)
self.picLable.setPixmap(QPixmap("loadImage.png"))
hover_tracker = HoverTracker(self.picLable)
hover_tracker.positionChanged.connect(self.on_position_changed)
#pyqtSlot(QPoint)
def on_position_changed(self, p):
print(p)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
You can use enterEvent.
Enter event gets called everytime the mouse is over the widget. With the event.pos() you can get the mouse coordinates.
self.label.enterEvent = lambda event: print(event.pos())

Creating Custom PyQt5 image-button

I'm trying to create a custom PyQt5 button, but am running across problems displaying it in a QMainWindow object. Here's the code I'm trying:
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QMainWindow
class PicButton(QAbstractButton):
def __init__(self, pixmap, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(event.rect(), self.pixmap)
def sizeHint(self):
return self.pixmap.size()
class App(QMainWindow):
def __init__(self):
super().__init__()
self.left = 0
self.top = 0
self.width = 800
self.height = 800
self.initUI()
def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)
btn = PicButton('/home/user/Desktop/Untitled.png')
btn.move(0, 0)
btn.resize(80,80)
self.show()
app = QApplication(sys.argv)
window = App()
The button will work if you just use window = Widget()and put the button object in there as is shown in this answer: how code a Image button in PyQt?
According to your code you must pass a QPixmap to your PicButton, In addition to them if you are going to move it should tell you where, if you pass the parent, it will place in that position relative to the father, but will not be drawn.
To solve the problem you must change:
btn = PicButton('/home/user/Desktop/Untitled.png')
to:
btn = PicButton(QPixmap('/home/user/Desktop/Untitled.png'), self)

pyqt drawing on an exsiting widget of GUI

I am new to pyqt. I am doing a program that allows you clicks on the picture and remember the coordinates of points you clicks and draw a stickfigure on a widget of the GUI. My code right now can prompt out a new window to show a polygon with 4 points. However, I hope it can be displayed on the ui file I alreay made by pyqt. The object name for the widget is called widget.I hope someone can help me to modify the code to display the polygon on the gui widget not prompting out a new window.
Thank you so much!!!
import sys
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from Main_window import *
global imgloc
imgloc = "1.jpg"
array = []
clicks = 0
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.local_image = QImage(imgloc)
self.imageLocation = imgloc
self.local_scene = QGraphicsScene()
self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)
self.ui.graphicsView_5.setScene( self.local_scene )
self.pixMapItem.mousePressEvent = self.pixelSelect
def pixelSelect(self,event):
global imgloc
a = event.pos().x()
b = event.pos().y()
global clicks
global array
if clicks != 4:
clicks += 1
point = QPoint(a,b)
array.append(point)
else:
clicks = 0
dialog = DialogBody()
dialog.show()
dialog.exec_()
array = []
class DialogBody(QDialog):
def __init__(self,parent=None):
super(QDialog,self).__init__(parent)
self.setGeometry(100, 100, QImage(imgloc).height(), QImage(imgloc).width())
def paintEvent(self,e):
qp = QtGui.QPainter()
qp.begin(self)
self.drawBody(qp)
qp.end()
def drawBody(self, qp):
qp.setPen(QtCore.Qt.red)
qp.drawPolygon(array[0],array[1],array[2],array[3])
qp.drawEllipse(array[0],2,2)
qp.drawEllipse(array[1],2,2)
qp.drawEllipse(array[2],2,2)
qp.drawEllipse(array[3],2,2)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())
Looks like you want to draw items on QGraphicsScene? In this case you could add items to the scene:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
self.pixmap_item.mousePressEvent = self.pixelSelect
self.click_positions = []
def pixelSelect(self, event):
self.click_positions.append(event.pos())
if len(self.click_positions) < 4:
return
pen = QtGui.QPen(QtCore.Qt.red)
self.scene.addPolygon(QtGui.QPolygonF(self.click_positions), pen)
for point in self.click_positions:
self.scene.addEllipse(point.x(), point.y(), 2, 2, pen)
self.click_positions = []
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = MainWidget()
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_())
QGraphicsScene has many features.
Read Graphics View Framework overview in Qt docs.

Categories

Resources