aseylys aseylys - 1 year ago 112
C++ Question

Qt5 Widget Properties from UI

I'm in the process of transitioning from PyQt5 to Qt5 in C++ and I'm having a bit of a tough time. I created a simple UI that contains a Qwidget called

logo
. I'm trying to make this widget an SVG rendering widget with this code:

QSvgRenderer renderer(QString(":/LogoSVG.svg"));

QImage image(500, 200, QImage::Format_ARGB32);
image.fill(0x000000);

QPainter painter(&image);
renderer.render(&painter);


In Python, I'd create a simple widget class that renders the SVG then in the UI form loader class I'd do

self.logo = SVGRender(self)


I'm trying to do the same thing in C++ Qt, so here's what I have so far and it's returning the error
error: cannot convert ‘logoW’ to ‘QWidget*’ in assignment


example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include "ui_example.h"

class example : public QWidget
{
Q_OBJECT

public:
example(QWidget *parent = 0);

private:
Ui::example ui;

};

class logoW: public QWidget
{
Q_OBJECT

public:
logoW(QWidget *parent = 0);
};

#endif


example.cpp

#include <QtGui>
#include <QScreen>
#include <QApplication>
#include <QDesktopWidget>
#include <QCoreApplication>
#include <QSvgRenderer>
#include <QPainter>
#include <QImage>
#include <iostream>
#include "example.h"

using namespace std;


example::example(QWidget *parent)
: QWidget(parent)
{

ui.setupUi(this);

ui.logo = logoW(this)

}


logoW :: logoW(QWidget * parent = 0){

QSvgRenderer renderer(QString(":resources/LogoSvg.svg"));

QImage image(500, 200, QImage::Format_ARGB32);
image.fill(0xaaA08080);

QPainter painter(&image);

renderer.render(&painter);

}


Can someone show me what I'm doing wrong?

Answer Source

The variable ui.logo requires a pointer of the object, in your case it changes:

ui.logo = logoW(this);

to:

ui.logo = new logoW(this);

I also understand that you want to display the image in the logo widget. To do this you must implement the paintEvent method:

*.h

class logoW: public QWidget
{
    Q_OBJECT

public:
    logoW(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);
};

*.cpp

logoW::logoW(QWidget *parent):QWidget(parent)
{
}

void logoW::paintEvent(QPaintEvent *event){
    Q_UNUSED(event)
    QSvgRenderer renderer(QString(":resources/LogoSvg.svg"));

    QPainter painter(this);

    renderer.render(&painter);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download