#include <QApplication>
#include <QVBoxLayout>
#include <QString>
#include <QPushButton>
#include <QFileDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPixmap>
#include <QMouseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include "blackcat.h"
{
// GraphicsView data
// first image is loaded to create scene at start app
image1->setFixedSize(200, 120);
image1->setMouseTracking(true);
image1->setInteractive (true);
image1->setAcceptDrops(true);
bitmapa1->load(*file1);
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();
// test labels
positiontxt
= new QLabel("Test of position");
scenetxt
= new QLabel("Test of scene");
info
= new QLabel("scene Info");
// x & y drag's position
// width & height drag's position
//connecting buttons
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(open, SIGNAL(clicked()), this, SLOT(Open1()));
// test layout
layout->addWidget(image1);
layout->addWidget(open);
layout->addWidget(quit);
layout->addWidget(positiontxt);
layout->addWidget(scenetxt);
layout->addWidget(info);
layout->addWidget(selectedx);
layout->addWidget(selectedy);
layout->addWidget(selectedw);
layout->addWidget(selectedh);
setLayout(layout);
// data to test label 3
int wscene;
wscene = (int) image1->width();
int hscene;
hscene = (int) image1->height();
num1.setNum(wscene);
num2.setNum(hscene);
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}
void BlackCat::Open1()
{
//loading img
tr("Open file"), "../../", tr("Image files (*.png)") );
bitmapa1->load(file1);
// checking img width & height -> if to small -> create biggest scene
int maxw, maxh;
if (bitmapa1->width() < image1->width())
maxw = image1->width()-2;
else
maxw = bitmapa1->width();
if (bitmapa1->height() < image1->height())
maxh = image1->height()-2;
else
maxh = bitmapa1->height();
area1->setSceneRect(0,0,maxw,maxh);
// showing image
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();
// data to test label 3
int wscene;
wscene = (int) area1->width();
int hscene;
hscene = (int) area1->height();
num1.setNum(wscene);
num2.setNum(hscene);
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}
{
// get data to test label 1 - mouse position in window
QPoint position
= event
->pos
();
int posx = position.x();
int posy = position.y();
num1.setNum(posx);
num2.setNum(posy);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
this->positiontxt->setText(info);
//get data to test label 2 - mouse position in scene
QPoint posScene
= BlackCat
::image1->mapFromScene
(position
);
int posx2 = posScene.x()-BlackCat::image1->x();
int posy2 = posScene.y()-BlackCat::image1->y();
num1.setNum(posx2);
num2.setNum(posy2);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
//is cliced on image?
if (BlackCat::image1->itemAt(posx2, posy2))
isimg = "yes";
else
isimg = "no";
info += "; Obrazek:"+ isimg ;
this->scenetxt->setText(info);
// filling qtextedit - x & y of mousedrag in scene
this->selectedx->setText(num1);
this->selectedy->setText(num2);
}
{
QPoint position
= event
->pos
();
// temporarily not needed
//QPoint posScene = BlackCat::image1->mapFromScene(position);
int posx = position.x();
int posy = position.y();
num1.setNum(posx);
num2.setNum(posy);
// filling qtextedit - width & height of mousedrag in and out of scene
this->selectedw->setText(num1);
this->selectedh->setText(num2);
}
#include <QApplication>
#include <QVBoxLayout>
#include <QString>
#include <QPushButton>
#include <QFileDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPixmap>
#include <QMouseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include "blackcat.h"
BlackCat::BlackCat(QWidget *parent) : QWidget (parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
QPushButton *open = new QPushButton(tr("Open"));
// GraphicsView data
// first image is loaded to create scene at start app
QString *file1 = new QString("../../test3.png");
QPixmap *bitmapa1 = new QPixmap;
area1 = new QGraphicsScene;
image1 = new QGraphicsView;
image1->setFixedSize(200, 120);
image1->setMouseTracking(true);
image1->setInteractive (true);
image1->setAcceptDrops(true);
image1->setDragMode(QGraphicsView::NoDrag);
bitmapa1->load(*file1);
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();
// test labels
positiontxt = new QLabel("Test of position");
scenetxt = new QLabel("Test of scene");
info = new QLabel("scene Info");
// x & y drag's position
selectedx = new QLineEdit();
selectedy = new QLineEdit();
// width & height drag's position
selectedw = new QLineEdit();
selectedh = new QLineEdit();
//connecting buttons
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(open, SIGNAL(clicked()), this, SLOT(Open1()));
// test layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(image1);
layout->addWidget(open);
layout->addWidget(quit);
layout->addWidget(positiontxt);
layout->addWidget(scenetxt);
layout->addWidget(info);
layout->addWidget(selectedx);
layout->addWidget(selectedy);
layout->addWidget(selectedw);
layout->addWidget(selectedh);
setLayout(layout);
// data to test label 3
int wscene;
wscene = (int) image1->width();
int hscene;
hscene = (int) image1->height();
QString num1, num2;
num1.setNum(wscene);
num2.setNum(hscene);
QString inf;
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}
void BlackCat::Open1()
{
//loading img
QString file1 = QFileDialog::getOpenFileName(this,
tr("Open file"), "../../", tr("Image files (*.png)") );
QPixmap *bitmapa1 = new QPixmap;
area1 = new QGraphicsScene(this);
bitmapa1->load(file1);
// checking img width & height -> if to small -> create biggest scene
int maxw, maxh;
if (bitmapa1->width() < image1->width())
maxw = image1->width()-2;
else
maxw = bitmapa1->width();
if (bitmapa1->height() < image1->height())
maxh = image1->height()-2;
else
maxh = bitmapa1->height();
area1->setSceneRect(0,0,maxw,maxh);
// showing image
area1->addPixmap(*bitmapa1);
image1->setScene(area1);
image1->show();
// data to test label 3
int wscene;
wscene = (int) area1->width();
int hscene;
hscene = (int) area1->height();
QString num1, num2;
num1.setNum(wscene);
num2.setNum(hscene);
QString inf;
inf = "GraphicsScene width & height: w=" + num1 + " ,h=" + num2 ;
this->info->setText(inf);
}
void BlackCat::mousePressEvent(QMouseEvent *event)
{
// get data to test label 1 - mouse position in window
QString num1, num2, info;
QPoint position = event->pos();
int posx = position.x();
int posy = position.y();
num1.setNum(posx);
num2.setNum(posy);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
this->positiontxt->setText(info);
//get data to test label 2 - mouse position in scene
QPoint posScene = BlackCat::image1->mapFromScene(position);
int posx2 = posScene.x()-BlackCat::image1->x();
int posy2 = posScene.y()-BlackCat::image1->y();
num1.setNum(posx2);
num2.setNum(posy2);
info = "Cursor pos in window: x=" + num1 + " ,y=" + num2 ;
//is cliced on image?
QString isimg;
if (BlackCat::image1->itemAt(posx2, posy2))
isimg = "yes";
else
isimg = "no";
info += "; Obrazek:"+ isimg ;
this->scenetxt->setText(info);
// filling qtextedit - x & y of mousedrag in scene
this->selectedx->setText(num1);
this->selectedy->setText(num2);
}
void BlackCat::mouseReleaseEvent(QMouseEvent *event)
{
QPoint position = event->pos();
// temporarily not needed
//QPoint posScene = BlackCat::image1->mapFromScene(position);
int posx = position.x();
int posy = position.y();
QString num1, num2;
num1.setNum(posx);
num2.setNum(posy);
// filling qtextedit - width & height of mousedrag in and out of scene
this->selectedw->setText(num1);
this->selectedh->setText(num2);
}
To copy to clipboard, switch view to plain text mode
#ifndef BLACKCAT_H
#define BLACKCAT_H
#include <QWidget>
{
Q_OBJECT
public:
private:
public slots:
void Open1();
protected:
};
#endif
#ifndef BLACKCAT_H
#define BLACKCAT_H
#include <QWidget>
class QGraphicsView;
class QGraphicsScene;
class QLabel;
class QLineEdit;
class BlackCat : public QWidget
{
Q_OBJECT
public:
BlackCat(QWidget *parent = 0);
QLabel *positiontxt;
QLabel *scenetxt;
QLabel *info;
QLineEdit *selectedx;
QLineEdit *selectedy;
QLineEdit *selectedw;
QLineEdit *selectedh;
private:
QGraphicsView *image1;
QGraphicsScene *area1;
public slots:
void Open1();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include "blackcat.h"
int main(int argc, char *argv[])
{
BlackCat widget;
widget.show();
return app.exec();
}
#include <QApplication>
#include "blackcat.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
BlackCat widget;
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks