Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Rotating a SVG image
QtWS25 Last Chance

Rotating a SVG image

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    codeaway
    wrote on 1 Oct 2020, 16:05 last edited by
    #1

    Greetings,

    I am trying to rotate a needle (an SVG image). The image does get loaded as expected, but it does not rotate; the paintEvent does not get called either.

    This document
    https://doc.qt.io/qt-5/qwidget.html#paintEvent

    states that calling update() will invoke the paintEvent()
    In my current situation that does not happen, which likely explains why it does not rotate the svg image.

    Any idea what could be wrong ?

    Thanks,

    This is my main.cpp

    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	Gauge gauge;
    
    	gauge.setSkin("Tachometer");
    	gauge.setNeedleOrigin(0.486, 0.486);
    	gauge.setMinimum(0);
    	gauge.setMaximum(120);
    	gauge.setStartAngle(-130);
    	gauge.setEndAngle(133);
    
    	gauge.setValue(0);
    
    	return a.exec();
    }
    

    gauge.cpp

    // constructor
    Gauge::Gauge(QWidget *parent)
    	: QWidget(parent),
    	m_minimum(0),
    	m_maximum(100),
    	m_value(0),
    	m_startAngle(0),
    	m_endAngle(100),
    	m_originX(0.5),
    	m_originY(0.5)
    {
    	setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    }
    
    // set skin for the widget
    void Gauge::setSkin(const QString &skin)
    {
    	m_skin = skin;
    
    	background = new QGraphicsSvgItem(":/Skins/Tachometer/background.svg");
    	background->setCachingEnabled(QGraphicsItem::NoCache);
    
    	shadow = new QGraphicsSvgItem(":/Skins/Tachometer/needle_shadow.svg");
    	shadow->setCachingEnabled(QGraphicsItem::NoCache);
    
    	needle = new QGraphicsSvgItem(":/Skins/Tachometer/needle.svg");
    	needle->setCachingEnabled(QGraphicsItem::NoCache);
    
    	overlay = new QGraphicsSvgItem(":/Skins/Tachometer/overlay.svg");
    	overlay->setCachingEnabled(QGraphicsItem::NoCache);
    
    	scene = new QGraphicsScene;
    	scene->setSceneRect(background->boundingRect());
    
    	view = new QGraphicsView(scene);
    
    	scene->addItem(background);
    	scene->addItem(shadow);
    	scene->addItem(needle);
    	scene->addItem(overlay);
    
    	// SVG item positioning
    	// position should be topLeft, if view->rect().center is used,
    	// the SVG top left corner will be set to the view.center
    	// That will not be the real center one would be expecting
    	background->setPos(view->rect().topLeft());
    	shadow->setPos(view->rect().topLeft());
    	needle->setPos(view->rect().topLeft());
    	overlay->setPos(view->rect().topLeft());
    
    	updateGeometry();
    	qDebug() << "update(), Call paintEvent";
    	update();						// call paintEvent
    
    	view->show();
    }
    
    // set actual value to widget
    void Gauge::setValue(int value)
    {
    	if (value < m_minimum)
    		value = m_minimum;
    
    	else if (value > m_maximum)
    		value = m_maximum;
    
    	m_value = value;
    	update();
    }
    
    void Gauge::paintEvent(QPaintEvent *event)
    {
    	QPainter painter(this);
    	Q_UNUSED(event);
    
    	qreal xc = needle->boundingRect().width() * 0.5;
    	qreal yc =needle->boundingRect().height() * 0.5;
    
    	qDebug() << "Hello from paintEvent";
    	painter.translate(xc, yc);
    	painter.rotate(45);
    	painter.translate(-xc, -yc);
    }
    
    

    gauge.h

    class Gauge : public QWidget
    {
    	Q_OBJECT
    public:
    	explicit Gauge(QWidget *parent = nullptr);
    	~Gauge();
    
    	void setSkin(const QString& skin);
    	QString skin() const;
    
    	void setMinimum(int minimum);
    	void setMaximum(int maximum);
    	void setNeedleOrigin(qreal x, qreal y);
    	void setStartAngle(qreal angle);
    	void setEndAngle(qreal angle);
    
    public slots:
    	void setValue(int value);
    
    signals:
    
    
    private:
    	QRectF availableRect(QGraphicsSvgItem *item) const;
    	QGraphicsSvgItem *background;
    	QGraphicsSvgItem *shadow;
    	QGraphicsSvgItem *needle;
    	QGraphicsSvgItem *overlay;
    
    	QGraphicsScene *scene;
    	QGraphicsView *view;
    
    	int m_minimum;						/** minimum possible value **/
    	int m_maximum;						/** maximum possible value **/
    	int m_value;						/** actual value **/
    
    	qreal m_startAngle;					/** smallest start angle **/
    	qreal m_endAngle;					/** largest end angle **/
    	qreal m_originX;					/** position x of needle **/
    	qreal m_originY;					/** position y of needle **/
    
    	QString m_skin;						/** name of actual skin **/
    protected:
    	void paintEvent(QPaintEvent *event) override;
    };
    

    GaugeWidget.PNG

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Oct 2020, 16:37 last edited by
      #2

      You completely overwrite paintEvent() and don't paint anything... !? Also I don't understand what QGraphicsScene and others should do.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply 1 Oct 2020, 17:50
      2
      • C Christian Ehrlicher
        1 Oct 2020, 16:37

        You completely overwrite paintEvent() and don't paint anything... !? Also I don't understand what QGraphicsScene and others should do.

        C Offline
        C Offline
        codeaway
        wrote on 1 Oct 2020, 17:50 last edited by
        #3

        Greetings,

        @Christian-Ehrlicher said in Rotating a SVG image:

        You completely overwrite paintEvent() and don't paint anything... !? Also I don't understand what QGraphicsScene and others should do.

        I am not sure, what else to be used in there ? The needle is a SVG image, should I use the setPen() or setBrush() ?

        Most likely, I'm missing something, you appear to be quite terse.
        Previously, the QT website itself had a fair amount examples, recently the code examples themselves are terse and confusing.

        Dont know what else to say!

        But, Thanks!

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Oct 2020, 18:06 last edited by Christian Ehrlicher 10 Jan 2020, 18:07
          #4

          I simply don't understand what your code tries to do - you create a QGraphicsScene but don't use it, you overwrite paintEvent() but don't paint anything in there so your widget can't display anything. You create a QGraphicsView inside a widget but show it as standalone widget - it's just confusing.
          btw: QGpraphicsObject has a rotation property.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          4/4

          1 Oct 2020, 18:06

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved