#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QPoint>
#include <QLabel>
#include <QLayout>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
{
QList<QPoint> current_pos; //<<<<<<<<<<<<<<<<
bool drawing;
public:
protected:
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QPoint>
#include <QLabel>
#include <QLayout>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
QGridLayout *layout;
QLabel *lbl;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
class Label : public QLabel
{
QList<QPoint> current_pos; //<<<<<<<<<<<<<<<<
bool drawing;
public:
explicit Label(QWidget * parent);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtWidgets>
#include <QtGui>
#include <QtCore>
#include <QDebug>
#include <QPixmap>
#include <QLayout>
#include <QLabel>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Setup Attribute for drawing
this->setAttribute(Qt::WA_OpaquePaintEvent);
/*
* Setup Layout, Label
* Attach Label to Layout
* Setup Layout to WIndow and set it as the central Widget
*/
lbl = new Label(this); //<<<<<<<<<<<<<<<<<<<<<<
layout->addWidget(lbl, 0, 0);
this->setLayout(layout);
setCentralWidget(lbl);
/*
* Setup filename for Bitma and create a Pixmap
*/
bmp_filename = "C:/Users/syiu/VideoAd/QT/Test_Paint_Bitmap/images/test.bmp";
pm
= new QPixmap(bmp_filename,
0,
0);
/*
* Bring the Pixmap to scale of the Window's dimensions and set this Pixmap to Label
*/
lbl->setPixmap(pm->scaled(this->width(), this->height(), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation));
}
MainWindow::~MainWindow()
{
delete ui;
}
{
// Setup Boolean to indicate User is dragging Cursor and thus OK for PaintEvent to Draw
drawing = false;
}
{
QLabel::paintEvent(event
);
//<<<<<<<<<<<<<<< if (!drawing)
return;
QPen pen
(Qt
::green,
4, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
painter.setPen(pen);
foreach
(QPoint point, current_pos
) //<<<<<<<<<<<<<<< painter.drawPoint(point);
}
{
drawing = true;
}
{
qDebug() << "Main Window MouseRelease Event";
drawing = false;
}
{
current_pos << event->pos(); //<<<<<<<<<<<<<<<<<
this->repaint();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtWidgets>
#include <QtGui>
#include <QtCore>
#include <QDebug>
#include <QPixmap>
#include <QLayout>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QString bmp_filename;
QPixmap *pm;
ui->setupUi(this);
//Setup Attribute for drawing
this->setAttribute(Qt::WA_OpaquePaintEvent);
/*
* Setup Layout, Label
* Attach Label to Layout
* Setup Layout to WIndow and set it as the central Widget
*/
layout = new QGridLayout(this);
lbl = new Label(this); //<<<<<<<<<<<<<<<<<<<<<<
lbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
layout->addWidget(lbl, 0, 0);
this->setLayout(layout);
setCentralWidget(lbl);
/*
* Setup filename for Bitma and create a Pixmap
*/
bmp_filename = "C:/Users/syiu/VideoAd/QT/Test_Paint_Bitmap/images/test.bmp";
pm = new QPixmap(bmp_filename, 0, 0);
/*
* Bring the Pixmap to scale of the Window's dimensions and set this Pixmap to Label
*/
lbl->setPixmap(pm->scaled(this->width(), this->height(), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation));
}
MainWindow::~MainWindow()
{
delete ui;
}
Label::Label(QWidget * parent)
: QLabel(parent)
{
// Setup Boolean to indicate User is dragging Cursor and thus OK for PaintEvent to Draw
drawing = false;
}
void Label::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event); //<<<<<<<<<<<<<<<
if (!drawing)
return;
QPainter painter(this);
QPen pen(Qt::green, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
foreach(QPoint point, current_pos) //<<<<<<<<<<<<<<<
painter.drawPoint(point);
}
void Label::mousePressEvent(QMouseEvent *event)
{
drawing = true;
}
void Label::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "Main Window MouseRelease Event";
drawing = false;
}
void Label::mouseMoveEvent(QMouseEvent *event)
{
current_pos << event->pos(); //<<<<<<<<<<<<<<<<<
this->repaint();
}
To copy to clipboard, switch view to plain text mode
Bookmarks