qtsmartgraphics  2.1.1
A simple library that improves the user experience when displaying images by Qt's QLabel and QGraphicsView.
qsmartlabel.cpp
Go to the documentation of this file.
1 #include "qSmartLabel.h"
2 #include <QMouseEvent>
3 #include <QMessageBox>
4 #include <QFileDialog>
5 #include <QMenu>
6 QSmartLabel::QSmartLabel(QWidget *parent) :
7  QLabel(parent)
8 {
9  saveAction = new QAction("Save Image", this);
10  connect(saveAction, SIGNAL(triggered()), this, SLOT(on_saveAction_triggered()));
11 
12  this->setPixmap(QPixmap(QSize(0,0)));
13 }
14 
15 void QSmartLabel::mousePressEvent(QMouseEvent *ev)
16 {
17  if(ev->button() == Qt::LeftButton){
18  double c_x = this->width()/2;
19  double c_y = this->height()/2;
20  double img_width = this->pixmap()->width();
21  double img_height = this->pixmap()->height();
22  double send_x = ((ev->x()-c_x+0.0)/img_width);
23  double send_y = ((ev->y()-c_y+0.0)/img_height);
24  if(abs(send_x) > 0.5 || abs(send_y > 0.5))
25  return;
26  emit sendMouXY(send_x, send_y);
27  }
28 }
29 
30 void QSmartLabel::mouseReleaseEvent(QMouseEvent *ev)
31 {
32  if(ev->button() == Qt::RightButton){
33  QMenu m(this);
34  m.addAction(saveAction);
35  m.exec(ev->globalPos());
36  }
37 }
38 
39 void QSmartLabel::on_saveAction_triggered()
40 {
41  const QImage _img = this->pixmap()->toImage();
42  QFileInfo file_name(QFileDialog::getSaveFileName(0, "Img",0,"PNG (*.png);;BMP (*.bmp);;JPG (*.jpg)"));
43  if(file_name.fileName().isNull()) {
44  return;
45  }
46  if(!_img.isNull())
47  {
48  _img.save(file_name.absoluteFilePath());
49  }
50  else{QMessageBox::information(0, 0, "Can Not Save Image!!");}
51 }
52 
53 
void mouseReleaseEvent(QMouseEvent *ev)
Definition: qsmartlabel.cpp:30
QSmartLabel(QWidget *parent=0)
Definition: qsmartlabel.cpp:6
void sendMouXY(double x, double y)
Sends the mouse coordinate of the image when clicked the image.
void mousePressEvent(QMouseEvent *ev)
Definition: qsmartlabel.cpp:15