Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problems to add QWidget to a Scroll Area...



  • I create one Widget that will be put inside a scrollarea, but my Widget is not appearing int the window.

    @
    WidgetSVG* widgetSVG = new WidgetSVG( );
    widgetSVG->setVeiculo( veic );
    ui->scrollAreaWidgetContents->layout()->addWidget( widgetSVG );
    @

    No error is showed, nothing wrong in the output...



  • I make this code, and the buttton appears.
    The problem appears to be on WidgetSVG!!!

    @
    QPushButton* teste = new QPushButton();
    teste->setText( "TESTE!!" );
    ui->scrollAreaWidgetContents->layout()->addWidget( teste );
    WidgetSVG* widgetSVG = new WidgetSVG( veiculo );
    ui->scrollAreaWidgetContents->layout()->addWidget( widgetSVG );
    @



  • .ui:
    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>WidgetSVG</class>
    <widget class="QWidget" name="WidgetSVG">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>Form</string>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
    <item>
    <widget class="QWebView" name="webView">
    <property name="url">
    <url>
    <string>http://www.google.com.br/</string>
    </url>
    </property>
    </widget>
    </item>
    </layout>
    </widget>
    <customwidgets>
    <customwidget>
    <class>QWebView</class>
    <extends>QWidget</extends>
    <header>QtWebKit/QWebView</header>
    </customwidget>
    </customwidgets>
    <resources/>
    <connections/>
    </ui>
    @

    .h
    @
    #ifndef WIDGETSVG_H
    #define WIDGETSVG_H

    #include <QWidget>
    #include <QSvgRenderer>
    #include <QWebFrame>

    #include "bo/veiculo.h"

    namespace Ui {
    class WidgetSVG;
    }

    class WidgetSVG : public QWidget
    {
    Q_OBJECT

    public:
    explicit WidgetSVG(QWidget parent = 0);
    // explicit WidgetSVG(Veiculo
    newVeiculo, QWidget parent = 0);
    ~WidgetSVG();
    void setVeiculo(Veiculo
    newVeiculo);
    Veiculo* getVeiculo();

    private:
    Ui::WidgetSVG ui;
    QSvgRenderer
    renderer;
    Veiculo* veiculo;

    int getComponentWidth();
    int getSVGWidth();
    double getFactor();
    double getSVGRatio();
    

    public slots:
    void svgClicked(QString message);
    void adjust();
    bool eventFilter(QObject *obj, QEvent *event);
    };

    #endif // WIDGETSVG_H
    @

    .cpp
    @
    #include "widgetsvg.h"
    #include "ui_widgetsvg.h"

    #include "QtCore/QDebug"

    WidgetSVG::WidgetSVG(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetSVG)
    {
    ui->setupUi(this);
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("sender", this);
    installEventFilter(this);
    }
    //WidgetSVG::WidgetSVG(Veiculo *newVeiculo, QWidget *parent) :
    // QWidget(parent),
    // ui(new Ui::WidgetSVG)
    //{
    // ui->setupUi(this);
    // setVeiculo(newVeiculo);
    // ui->webView->setUrl( getVeiculo()->getFileUrl() );
    // ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("sender", this);
    // installEventFilter(this);
    // qDebug() << "Passou aqui!" << getVeiculo()->getFileUrl();
    //}
    WidgetSVG::~WidgetSVG()
    {
    delete veiculo;
    delete ui;
    }
    void WidgetSVG::adjust()
    {
    QString url = ui->webView->url().toString().remove(0, 7);
    renderer = new QSvgRenderer(url);
    ui->webView->setZoomFactor( getFactor() );
    // qDebug() << "Factor: " << getFactor();
    // qDebug() << "Ratio: " << getSVGRatio();
    // double teste = (double) ui->webView->size().height() / ui->webView->size().width();
    // qDebug() << "Now Ratio: " << teste;
    }
    void WidgetSVG::svgClicked(QString message)
    {
    qDebug() << "CLICOU --> " << message;
    }
    bool WidgetSVG::eventFilter(QObject *obj, QEvent *event)
    {
    if (event->type() == QEvent::Resize)
    {
    adjust();
    }
    return true;
    }
    int WidgetSVG::getComponentWidth()
    {
    return ui->webView->size().width();
    }
    int WidgetSVG::getSVGWidth()
    {
    return renderer->defaultSize().width();
    }
    double WidgetSVG::getSVGRatio()
    {
    return (double) renderer->defaultSize().height() / renderer->defaultSize().width();
    }
    double WidgetSVG::getFactor()
    {
    return (double) getComponentWidth() / getSVGWidth();
    }
    void WidgetSVG::setVeiculo(Veiculo newVeiculo)
    {
    this->veiculo = newVeiculo;
    }
    Veiculo
    WidgetSVG::getVeiculo()
    {
    return this->veiculo;
    }
    @



  • When I add this line:

    @
    installEventFilter(this);
    @

    I got no error but my widget is not showed on the screen!


Log in to reply