Найти - Пользователи
Полная версия: Рисование в Qpainter по клику мышкой в PyQt5
Начало » GUI » Рисование в Qpainter по клику мышкой в PyQt5
1
sharkk
Не получается чтоб по клику мышкой вывелась нарисованная надпись.
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt

class Example(QMainWindow):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'

self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Draw text')
self.show()

def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
if self.bt == 1:
self.drawText(event, qp)
qp.end()

def drawText(self, event, qp):
qp.setPen(QColor(168, 34, 3))
qp.setFont(QFont('Decorative', 10))
qp.drawText(event.rect(), Qt.AlignCenter, self.text)

def mousePressEvent(self, event):
self.bt = event.button()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Rodegast
#!/usr/bin/python
# coding: utf-8
import sys
import PySide.QtGui as QtGui
import PySide.QtCore as QtCore
class Example(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.text = u"Текст на человеческом языке"
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Draw text')
        self.button = None
    def paintEvent(self, event):
        if self.button == 1:
           painter = QtGui.QPainter()
           painter.begin(self)
           painter.setPen(QtGui.QColor(168, 34, 3))
           painter.setFont(QtGui.QFont('Decorative', 10))
           painter.drawText(self.rect(), QtCore.Qt.AlignCenter, self.text)
           painter.end()
    def mousePressEvent(self, event):
        self.button = event.button()
        self.update()
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Example()
    win.show()
    sys.exit(app.exec_())
sharkk
Спасибо! Плюс ушел!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB