图元的旋转、切变、缩放、位移

程序

主窗口函数(主窗口的设置、图片的加载、四个槽函数完成四个功能)

.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QFrame>
#include "pixitem.h"

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();
    void createControLFrame();
private:
    int angel;
    qreal scaleValue;
    qreal shearValue;
    qreal translateValue;
    QGraphicsView *view;
    QFrame *ctrlFrame;
    PixItem *pixitem;
public slots:
    void slotRotate(int);
    void slotScale(int);
    void slotShear(int);
    void slotTranslate(int);

};

#endif // MAINWIDGET_H
.cpp
#include "mainwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSlider>
#include <QGroupBox>
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    angel=0;
    scaleValue =5;
    shearValue =5;
    translateValue =50;
    QGraphicsScene *scene=new QGraphicsScene;
    scene->setSceneRect(-200,-200,400,400);
    QPixmap *pixmap=new QPixmap("image.png");
    pixitem=new PixItem(pixmap);
    scene->addItem(pixitem);
    pixitem->setPos(0,0);
    view=new QGraphicsView;
    view->setScene(scene);
    view->setMinimumSize(400,400);
    ctrlFrame=new QFrame;
    createControLFrame();
    QHBoxLayout *mainLayout=new QHBoxLayout;
    mainLayout->setMargin(10);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(view);
    mainLayout->addWidget(ctrlFrame);
    setLayout(mainLayout);
    setWindowTitle("Graphics Item Transformation");
}

MainWidget::~MainWidget()
{
    
}

void MainWidget::createControLFrame()
{
    //旋转控制
    QSlider *rotateSlider =new QSlider;
    rotateSlider->setOrientation(Qt::Horizontal);
    rotateSlider->setRange(0,360);
    QHBoxLayout *rotateLayout = new QHBoxLayout;
    rotateLayout->addWidget(rotateSlider);
    QGroupBox *rotateGroup=new QGroupBox("Rotate");
    rotateGroup->setLayout(rotateLayout);
    //缩放控制
    QSlider *scaleSlider =new QSlider;
    scaleSlider->setOrientation(Qt::Horizontal);
    scaleSlider->setRange(0,2*scaleValue);
    scaleSlider->setValue(scaleValue);
    QHBoxLayout *scaleLayout = new QHBoxLayout;
    scaleLayout->addWidget(scaleSlider);
    QGroupBox *scaleGroup=new QGroupBox("scale");
    scaleGroup->setLayout(scaleLayout);
    //切边控制
    QSlider *shearSlider =new QSlider;
    shearSlider->setOrientation(Qt::Horizontal);
    shearSlider->setRange(0,2*shearValue);
    shearSlider->setValue(shearValue);
    QHBoxLayout *shearLayout = new QHBoxLayout;
    shearLayout->addWidget(shearSlider);
    QGroupBox *shearGroup=new QGroupBox("shear");
    shearGroup->setLayout(shearLayout);
    //位移控制
    QSlider *translateSlider =new QSlider;
    translateSlider->setOrientation(Qt::Horizontal);
    translateSlider->setRange(0,2*translateValue);
    translateSlider->setValue(translateValue);
    QHBoxLayout *translateLayout = new QHBoxLayout;
    translateLayout->addWidget(translateSlider);
    QGroupBox *translateGroup=new QGroupBox("translate");
    translateGroup->setLayout(translateLayout);
    QVBoxLayout *frameLayout = new QVBoxLayout;
    connect(rotateSlider,SIGNAL(valueChanged(int)),this,SLOT(slotRotate(int)));
    connect(scaleSlider,SIGNAL(valueChanged(int)),this,SLOT(slotScale(int)));
    connect(shearSlider,SIGNAL(valueChanged(int)),this,SLOT(slotShear(int)));
    connect(translateSlider,SIGNAL(valueChanged(int)),this,SLOT(slotTranslate(int)));
    frameLayout->setMargin(10);
    frameLayout->setSpacing(20);
    frameLayout->addWidget(rotateGroup);
    frameLayout->addWidget(scaleGroup);
    frameLayout->addWidget(shearGroup);
    frameLayout->addWidget(translateGroup);
    ctrlFrame->setLayout(frameLayout);
}
void MainWidget::slotRotate(int value)
{
    view->rotate(value-angel);
    angel = value;
}

void MainWidget::slotScale(int value)
{
    qreal s;
    if(value>scaleValue)
        s=pow(1.1,(value-scaleValue));
    else
        s=pow(1/1.1,(scaleValue-value));
    view->scale(s,s);
    scaleValue=value;
}

void MainWidget::slotShear(int value)
{
    view->shear((value-shearValue)/10.0,0);
    shearValue=value;
}

void MainWidget::slotTranslate(int value)
{
    view->translate(value-translateValue*10,value-translateValue*10);
    translateValue=value;
}

PixItem函数(图元设置、图片的绘制)

.h
#ifndef PIXITEM_H
#define PIXITEM_H
#include <QGraphicsItem>
#include <QPixmap>
#include <QPainter>

class PixItem : public QGraphicsItem
{
public:
    PixItem(QPixmap *pixmap);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
    QPixmap pix;
};

#endif // PIXITEM_H
.cpp
#include "pixitem.h"

PixItem::PixItem(QPixmap *pixmap)
{
    pix=*pixmap;
}
QRectF PixItem::boundingRect() const
{
    return QRectF(-2-pix.width()/2,-2-pix.height()/2,pix.width()+4, pix. height()+4);
}
void PixItem::paint(QPainter *painter, const QStyleOptionGraphicsItem  *option,QWidget *widget)
{
    painter->drawPixmap(-pix.width()/2,-pix.height()/2,pix);
}

效果展示

在这里插入图片描述

发布了31 篇原创文章 · 获赞 3 · 访问量 280

猜你喜欢

转载自blog.csdn.net/weixin_44011306/article/details/105497497