qt svg显示

qt 显示svg图片,点击弹出dialog显示。可放缩。

#ifndef SVGWIDGET_H
#define SVGWIDGET_H

#include <QWidget>
#include <QtSvg/QtSvg>
#include <QtSvg/QGraphicsSvgItem>
#include <QtSvg/QSvgRenderer>

#include <stdio.h>

class SvgQGraphicsView : public QGraphicsView {
Q_OBJECT
public:
    explicit SvgQGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) {}
    void setSvg(const QString& svgPath);
signals:
    void clicked();

protected:
    virtual void resizeEvent(QResizeEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
private:
    QSvgRenderer*     _renderer = nullptr;
    QGraphicsSvgItem* _svgItem  = nullptr;
    QGraphicsScene*   _scene    = nullptr;
};


class SvgDialog : public QDialog {
Q_OBJECT

public:
    explicit SvgDialog(QWidget *parent = nullptr) : QDialog(parent) { init(); }
    void setSvg(const QString& svgPath);
    void setMinMaxWH(int minW, int minH, int maxW, int maxH) { _minW = minW; _minH = minH; _maxW = maxW; _maxH = maxH; }
    void setWHRatio(double ratio) { _ratio = ratio > 0 ? ratio : _ratio; }

protected:
    void init();
    void wheelEvent(QWheelEvent *event) override;

private:
    int _minW     = 50;
    int _minH     = 50;
    int _maxW     = 800;
    int _maxH     = 800;
    double _ratio = 1;

    QVBoxLayout*      _qvBoxLayout      = nullptr;
    SvgQGraphicsView* _svgQGraphicsView = nullptr;
};



class SvgWidget : public QWidget {
Q_OBJECT
public:
    explicit SvgWidget(QWidget* parent = nullptr) : QWidget(parent) { init(); }
    void setSvg(const QString& svgPath);
    SvgDialog* getSvgDialog() { return _svgDialog; }

protected:
    bool init();
protected slots:
    void openSvgDialog();

private:
    QVBoxLayout*      _qvBoxLayout      = nullptr;
    SvgQGraphicsView* _svgQGraphicsView = nullptr;
    SvgDialog*        _svgDialog        = nullptr;
    QString           _svgPath;
};


#endif  //SVGWIDGET_H

#include "SvgWidget.h"

//-------------------SvgQGraphicsView--------------------------

void SvgQGraphicsView::setSvg(const QString &svgPath) {
    if (_svgItem == nullptr) {
        _svgItem = new QGraphicsSvgItem();
    }
    if (_scene == nullptr) {
        _scene = new QGraphicsScene(this);
        _scene->addItem(_svgItem);
    }
    _renderer = new QSvgRenderer(svgPath, _scene);
    _svgItem->setSharedRenderer(_renderer);
    setScene (_scene);
}

void SvgQGraphicsView::resizeEvent(QResizeEvent *event) {
    fitInView(_svgItem, Qt::KeepAspectRatio);
}

void SvgQGraphicsView::mousePressEvent(QMouseEvent *event) {
    emit clicked();
}

//-------------------SvgDialog--------------------------

void SvgDialog::init() {
    if (_qvBoxLayout == nullptr) {
        _qvBoxLayout = new QVBoxLayout(this);
    }
    if (_svgQGraphicsView == nullptr) {
        _svgQGraphicsView = new SvgQGraphicsView(this);
    }
    _qvBoxLayout->addWidget(_svgQGraphicsView);
}

void SvgDialog::setSvg(const QString &svgPath) {
    _svgQGraphicsView->setSvg(svgPath);
}

void SvgDialog::wheelEvent(QWheelEvent *event) {
    QWidget::wheelEvent(event);
    int d = event->delta();
    int dw = d > 0 ? 20 : -20;
    int width = this->width() + dw;
    width = std::max(std::min(width, _maxW), _minW);
    int height = int(width / _ratio);
    height = std::max(std::min(height, _maxH), _minH);
    this->resize(width, height);
}

//-------------------SvgWidget--------------------------

bool SvgWidget::init() {
    if (_qvBoxLayout == nullptr) {
        _qvBoxLayout = new QVBoxLayout(this);
    }
    if (_svgQGraphicsView == nullptr) {
        _svgQGraphicsView = new SvgQGraphicsView(this);
    }
    if (_svgDialog == nullptr) {
        _svgDialog = new SvgDialog(this);
    }
    _qvBoxLayout->addWidget(_svgQGraphicsView);
    connect(_svgQGraphicsView, SIGNAL(clicked()), this, SLOT(openSvgDialog()));
    return true;
}

void SvgWidget::setSvg(const QString &svgPath) {
    _svgPath = svgPath;
    if (_svgQGraphicsView != nullptr) {
        _svgQGraphicsView->setSvg(_svgPath);
    }
    if (_svgDialog != nullptr) {
        _svgDialog->setSvg(_svgPath);
    }
}

void SvgWidget::openSvgDialog() {
    if (_svgDialog == nullptr) {
        _svgDialog = new SvgDialog(this);
        _svgDialog->setSvg(_svgPath);
    }
    _svgDialog->show();
}

文章目录