第7章:PlaceChange

1,图片的旋转,放大,倾斜等

2,源码:

#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);
}

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    angle           = 0;
    scaleValue      = 5;
    shearValue      = 5;
    translateValue  = 50;

    QGraphicsScene *scene = new QGraphicsScene;
    QPixmap *pixmap = new QPixmap(":/image/image.png");
    pixItem = new PixItem(pixmap);
    scene->addItem(pixItem);
    pixItem->setPos(0,0);
    scene->setSceneRect(-200, -200, 400, 400);

    view = new QGraphicsView;
    view->setScene(scene);
    view->setMinimumSize(400, 400);

    ctrlFrame = new QFrame;
    CreatControlFrame();

    //主窗口布局
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->setMargin(10);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(view);
    mainLayout->addWidget(ctrlFrame);
    ui->centralWidget->setLayout(mainLayout);


}

MainWindow::~MainWindow()
{
    delete ui;
}


//创建主窗体右侧的控制面板
void MainWindow::CreatControlFrame()
{
    //旋转
    QSlider *rotateSlider = new QSlider;
    rotateSlider->setOrientation(Qt::Horizontal);
    rotateSlider->setRange(0, 360);
    connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(slotRotate(int)));

    QHBoxLayout *rotateLayout = new QHBoxLayout;
    rotateLayout->addWidget(rotateSlider);

    QGroupBox *rotateGroup = new QGroupBox(tr("Rotate"));
    rotateGroup->setLayout(rotateLayout);

    //缩放
    QSlider *scaleSlider = new QSlider;
    scaleSlider->setOrientation(Qt::Horizontal);
    scaleSlider->setRange(0, 2*scaleValue);
    scaleSlider->setValue(scaleValue);
    connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(slotScale(int)));

    QHBoxLayout *scaleLayout = new QHBoxLayout;
    scaleLayout->addWidget(scaleSlider);

    QGroupBox *scaleGroup = new QGroupBox(tr("Scale"));
    scaleGroup->setLayout(scaleLayout);

    //切换控制
    QSlider *shearSlider = new QSlider;
    shearSlider->setOrientation(Qt::Horizontal);
    shearSlider->setRange(0, 2*shearValue);
    shearSlider->setValue(shearValue);
    connect(shearSlider, SIGNAL(valueChanged(int)), this, SLOT(slotShear(int)));

    QHBoxLayout *shearLayout = new QHBoxLayout;
    shearLayout->addWidget(shearSlider);

    QGroupBox *shearGroup = new QGroupBox(tr("Shear"));
    shearGroup->setLayout(shearLayout);

    //位移控制
    QSlider *translateSlider = new QSlider;
    translateSlider->setOrientation(Qt::Horizontal);
    translateSlider->setRange(0, 2*translateValue);
    translateSlider->setValue(translateValue);
    connect(translateSlider, SIGNAL(valueChanged(int)), this, SLOT(slotTranslate(int)));

    QHBoxLayout *translateLayout = new QHBoxLayout;
    translateLayout->addWidget(translateSlider);

    QGroupBox *translateGroup = new QGroupBox(tr("Translate"));
    translateGroup->setLayout(translateLayout);


    //控制面板布局
    QVBoxLayout *frameLayout = new QVBoxLayout;
    frameLayout->setMargin(10);
    frameLayout->setSpacing(20);
    frameLayout->addWidget(rotateGroup);
    frameLayout->addWidget(scaleGroup);
    frameLayout->addWidget(shearGroup);
    frameLayout->addWidget(translateGroup);

    ctrlFrame->setLayout(frameLayout);
}


//旋转
void MainWindow::slotRotate(int value)
{
    view->rotate(value-angle);
    angle = value;
}

//缩放
void MainWindow::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 MainWindow::slotShear(int value)
{
    view->shear((value-shearValue)/10.0, 0);
    shearValue = value;
}




//实现位移功能
void MainWindow::slotTranslate(int value)
{
    view->translate(value-translateValue, value-translateValue);
    translateValue = value;
}

3,效果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值