Created
August 11, 2013 11:11
-
-
Save liuyanghejerry/6204406 to your computer and use it in GitHub Desktop.
This is a test case of tablet support for Qt 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "widget.h" | |
#include <QApplication> | |
#include <QScrollArea> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
QScrollArea area; | |
Widget w; | |
area.setWidget(&w); | |
area.setBackgroundRole(QPalette::Dark); | |
area.show(); | |
area.ensureWidgetVisible(&w); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT += core gui concurrent | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = PainterTest | |
TEMPLATE = app | |
CONFIG += c++11 | |
SOURCES += main.cpp\ | |
widget.cpp | |
HEADERS += widget.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "widget.h" | |
#include <QTabletEvent> | |
#include <QMouseEvent> | |
#include <QDebug> | |
Widget::Widget(QWidget *parent) : | |
QWidget(parent), | |
pic(new QPixmap(1366, 768)) | |
{ | |
pic->fill(Qt::white); | |
drawing = false; | |
this->grabMouse(); | |
} | |
Widget::~Widget() | |
{ | |
delete pic; | |
} | |
QSize Widget::sizeHint() const | |
{ | |
return QSize(720,480); | |
} | |
void Widget::paintEvent(QPaintEvent *) | |
{ | |
QPainter painter(this); | |
painter.drawLine(0,0,100,100); | |
painter.drawPixmap(0,0,*pic); | |
} | |
void Widget::tabletEvent(QTabletEvent *ev) | |
{ | |
QPainter painter(pic); | |
painter.setRenderHint(QPainter::Antialiasing); | |
QPen pen; | |
int maxWidth = 5; | |
pen.setWidth(maxWidth); | |
pen.setCapStyle(Qt::RoundCap); | |
pen.setJoinStyle(Qt::RoundJoin); | |
pen.setColor(Qt::blue); | |
painter.setPen(pen); | |
switch(ev->type()){ | |
case QEvent::TabletPress: | |
if(!drawing){ | |
lastPoint = ev->pos(); | |
drawing = true; | |
pen.setWidthF(maxWidth * ev->pressure()); | |
painter.setPen(pen); | |
painter.drawPoint(ev->pos()); | |
} | |
break; | |
case QEvent::TabletMove: | |
if(drawing && lastPoint != ev->pos()){ | |
pen.setWidthF(maxWidth * ev->pressure()); | |
painter.setPen(pen); | |
painter.drawLine(lastPoint, ev->pos()); | |
lastPoint = ev->pos(); | |
} | |
break; | |
case QEvent::TabletRelease: | |
if(drawing){ | |
drawing = false; | |
} | |
break; | |
default: | |
break; | |
} | |
ev->accept(); | |
update(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WIDGET_H | |
#define WIDGET_H | |
#include <QWidget> | |
#include <QPainter> | |
#include <QPen> | |
#include <QBrush> | |
class Widget : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit Widget(QWidget *parent = 0); | |
~Widget(); | |
QSize sizeHint() const; | |
protected: | |
void paintEvent(QPaintEvent *ev); | |
void tabletEvent(QTabletEvent *ev); | |
private: | |
QPixmap *pic; | |
bool drawing = false; | |
QPoint lastPoint; | |
}; | |
#endif // WIDGET_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment