Reputation: 4521
It is second time to post this question. Because first one didn't show any effort.
I want to create simple painting application which consist with just black/white pen and canvas. So to create my painting app I want to know what class should I use. Or is there painting soft which is open source and created by Qt?
I know there is QPainter class. So using QPainter is the right way? I heard it is low level graphical tool. So is there more useful one? And I think following source code is one of the implementation of painter app. But Is this right way? I think there is more good way.
My code: https://gist.github.com/keimina/469fa17508ae2c0c90c4#file-simplepaintapp-py
from PySide.QtGui import QApplication, QMainWindow, QAction, QActionGroup, QWidget, QCursor, QPainter
from PySide.QtCore import QTimer
import PySide.QtCore as QtCore
class W(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(400,400)
self.myIsMousePressing = False
self.p = QPainter(self)
self.autoFillBackground()
self.x = 0
self.y = 0
self.r = dict()#{(x,Y,49, 49):rect}
self.penColor = 1
def mousePressEvent(self, event):
self.myIsMousePressing = True
def mouseReleaseEvent(self, event):
self.myIsMousePressing = False
def myTimeOut(self):
if self.myIsMousePressing:
pos = self.mapFromGlobal(QCursor.pos())
self.x = pos.x()/50
self.y = pos.y()/50
self.r[(self.x*50, self.y*50, 49, 49)] = self.penColor
def paintEvent(self, event):
self.p.begin(self)
for k in self.r.keys():
if self.r[k] == 1:
self.p.setPen(QtCore.Qt.black)
self.p.setBrush(QtCore.Qt.black)
else:
self.p.setPen(QtCore.Qt.white)
self.p.setBrush(QtCore.Qt.white)
self.p.drawRect(*k)
self.p.end()
self.update()
class MyWidget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(400, 400)
self.initMenu()
self.w = W()
self.setCentralWidget(self.w)
self.t = QTimer(self.w)
self.t.timeout.connect(self.w.myTimeOut)
self.t.start(1)
def initMenu(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.editMenu = self.menuBar().addMenu("&Edit")
self.helpMenu = self.menuBar().addMenu("&Help")
self.fileMenuAction = QAction("&New", self)
self.editMenuAction1 = QAction("&Black", self)
self.editMenuAction2 = QAction("&White", self)
self.helpMenuAction = QAction("&About", self)
actGroup = QActionGroup(self)
actGroup.addAction(self.editMenuAction1)
actGroup.addAction(self.editMenuAction2)
self.editMenuAction1.setCheckable(True)
self.editMenuAction2.setCheckable(True)
self.editMenuAction1.setChecked(True)
self.fileMenu.addAction(self.fileMenuAction)
self.editMenu.addAction(self.editMenuAction1)
self.editMenu.addAction(self.editMenuAction2)
self.helpMenu.addAction(self.helpMenuAction)
self.editMenuAction1.triggered.connect(self.action1)
self.editMenuAction2.triggered.connect(self.action2)
def action1(self):
self.w.penColor = 1
def action2(self):
self.w.penColor = 2
app = QApplication([])
mainWin = MyWidget()
mainWin.show()
app.exec_()
Thanks.
P.S. I'm using PySide but any other Qt is OK.
Upvotes: 0
Views: 523
Reputation: 98435
QPainter
is essentially the only way, short of manipulating individual pixels in a QImage
, or using OpenGL, to paint something in Qt. So its use goes without saying and if you paint in Qt, you will have to use QPainter
. That's how painting is done. But this has nothing to do with an application that a human might use to "paint". The painting we're talking about is what your application has to do to show something to the user, no matter what the user is doing.
What you're asking is if there's something application-specific in Qt that would help with implementing a "drawing" application. If you're after a vector drawing application, then the graphics scene framework might be of use. Otherwise, there's nothing to help you. You'll have to start with a plain QWidget
and implement the behavior you need.
Upvotes: 2