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. Problems to add QWidget to a Scroll Area...
Qt 6.11 is out! See what's new in the release blog

Problems to add QWidget to a Scroll Area...

Scheduled Pinned Locked Moved General and Desktop
4 Posts 1 Posters 2.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dcbasso
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dcbasso
      wrote on last edited by
      #2

      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 );
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dcbasso
        wrote on last edited by
        #3

        .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;
        }
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dcbasso
          wrote on last edited by
          #4

          When I add this line:

          @
          installEventFilter(this);
          @

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

          1 Reply Last reply
          0

          • Login

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