Qt开发记录15——QGraphicsView控件中图片处理(90度旋转、截图等)后居中显示

6 篇文章 0 订阅

QGraphicsView中图片处理后仍然居中显示

场景

GraphicsView 定义如下

GraphicsView m_graph = new GraphicsView(ui->label_img);

m_graph->setGeometry(ui->label_img->rect());
m_graph->setSceneRect(m_graph->PixItem()->boundingRect());
m_graph->setAlignment(Qt::AlignCenter);
m_graph->show();

ui->label_img是QLabel控件

QLabel *label_img;

GraphicsView 实现类

GraphicsView::GraphicsView(QWidget *parent) :
    QGraphicsView(parent)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setBackgroundBrush(QBrush(Qt::gray));
    // 矩形框项
    selector = new ShapeItem;
    // 表示Graphics View中的场景
    m_scene = new QGraphicsScene(this);
    // QPixmap类是一个幕下图像的表现,可以用作一个绘制设备
    m_item = new PixmapItem;
    this->setScene(m_scene);
    m_scene->addItem(m_item);
    this->setRenderHint(QPainter::Antialiasing);
    this->setMouseTracking(true);
}

GraphicsView::~GraphicsView()
{
    SafeDelete(m_item);
    SafeDelete(m_scene);
    SafeDelete(selector);
}

图片处理后居中显示

/**
 * @brief 顺时针旋转90度
 */
void Picture::ClockwiseRotate90()
{
    // 将图片置为已处理状态
    if (!inChange){
        inChange = true;
    }

    // 入栈克隆
    push_showImg_to_record();

    // opencv图片处理
    transpose(m_showImg, m_showImg);
    flip(m_showImg, m_showImg, 1);

	// 显示图片
    showImgToItem();

	// undo/redo
    QUndoCommand *command = new CommandImage(this, "顺时针旋转90度");
    m_undoStack->push(command);
}
/**
 * @brief Picture::showImgToItem 展示图片到控件
 */
void Picture::showImgToItem()
{
    QElapsedTimer mstimer;

    // 类型转换
    Mat dst;
    m_effect->toBlur(m_showImg, dst);
    QPixmap p_showImg = Mat2Pixmap(dst);
    dst.release();

    // 显示图片
    m_item->setPixmap(p_showImg);
    m_graph->setSceneRect(m_graph->PixItem()->boundingRect());
}

核心代码是

m_graph->setSceneRect(m_graph->PixItem()->boundingRect());

即必须在setPixmap后重新设置scene尺寸

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小言W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值