#ifndef FACEWINDOW_H#define FACEWINDOW_H#include <QWidget>#include <QString>#include <QImage>#include <QPainter>#include <QRectF>#include <QtGui>#include <QLayout>#include <QGraphicsScene>#include <QGraphicsView>#include <QPixmap>#include <QGraphicsLineItem>class FaceWindow{public: FaceWindow(); QWidget m_facewindow; QString m_opendirectory; //Директория изображения QGraphicsScene m_scene; QGraphicsView m_view; QGraphicsLineItem line1,line2;};#endif // FACEWINDOW_H
#include "facewindow.h"#include <QFileDialog>#include <QLayout>#include <QGraphicsPixmapItem>FaceWindow::FaceWindow(){ QGraphicsPixmapItem *m_pItem = new QGraphicsPixmapItem; QPixmap *m_picture = new QPixmap; m_view.setScene(&m_scene); m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_opendirectory = QFileDialog::getOpenFileName(0,"Open Dialog","","*.jpg"); //загрузка изображения в pixmap m_picture->load(m_opendirectory); m_pItem->setPixmap(*m_picture); m_scene.addItem(m_pItem); m_view.scale(0.2,0.2); //тут добавить scale в реальном временни m_view.setFixedSize(m_view.sizeHint()); //Установка фиксированого размера для окна m_view.show();}
C++ (Qt)class MyCrossItem : public QGraphicsRectItem{public: MyCrossItem( const QRectF& rect, QGraphicsItem* parent = 0 ) : QGraphicsRectItem ( rect, parent ) {} virtual void ~MyCrossItem() {} protected: void paint( QPainter* painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) { painter->drawLine( rect().topLeft(), rect().bottomRight() ); painter->drawLine( rect().topRight(), rect().bottomLeft() ); }};
#ifndef EYE_H#define EYE_H#include <QGraphicsItemGroup>#include <QGraphicsLineItem>#include <QPen>#include <QGraphicsItem>class Eye : public QGraphicsItemGroup{public: Eye(int a,int b); private: QGraphicsLineItem mLineHorizontal,mLineVertical; QPen mPen; int x,y;};#endif // EYE_H
#include "eye.h"Eye::Eye(int a,int b){ mPen.setWidth(10); this->x=a; this->y=b; mLineVertical.setPen(mPen); mLineHorizontal.setPen(mPen); mLineVertical.setLine(x,y-10,x,y+10); mLineHorizontal.setLine(x-10,y,x+10,y); mLineVertical.show(); mLineHorizontal.show(); this->addToGroup(&mLineVertical); this->addToGroup(&mLineHorizontal); this->show();}
Eye eye1(100,100); mScene.addItem(&eye1);
C++ (Qt)#include <QApplication>#include <QGraphicsScene>#include <QGraphicsView>#include <QGraphicsRectItem> class CrossItem : public QGraphicsRectItem{public: CrossItem( const QRectF& rect, QGraphicsItem* parent = 0 ) : QGraphicsRectItem ( rect, parent ) {} virtual ~CrossItem() {} protected: void paint( QPainter* painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) { painter->drawLine( rect().topLeft(), rect().bottomRight() ); painter->drawLine( rect().topRight(), rect().bottomLeft() ); }}; int main( int argc, char** argv ){ QApplication app( argc, argv ); QGraphicsView gv; QGraphicsScene scene; gv.setScene( &scene ); CrossItem ci( QRectF( 0, 0, 200, 200 ) ); scene.addItem( &ci ); gv.show(); return app.exec();}
#include "facewindow.h"#include "eye.h"#include "crossitem.h"FaceWindow::FaceWindow(){ QWidget mFaceWindow; QGraphicsPixmapItem *mpItem = new QGraphicsPixmapItem; QPixmap *mPicture = new QPixmap; CrossItem ci( QRectF( 0, 10, 400, 400 ) ); mView.setScene(&mScene); mScene.addItem(&ci); mView.show();}