[Resolved] How to add a onRezise to QWidget Inherit Class?



  • I'm creating my own QWidget.
    Inside this widget I have the QWebView, and when the parent QWidget on the QWebView resizes, I want to handle with the event "onResize", on any other similar event.
    It's possible to make that?!



  • I'm not sure if I understand you correctly, but I think you want to "install a custom EventFilter":http://qt-project.org/doc/qt-4.8/qobject.html#eventFilter.



  • Maybe the "void resizeEvent(QResizeEvent *)":http://qt-project.org/doc/qt-4.8/qwidget.html#resizeEvent is what are you looking for?



  • two code snippet:

    @
    void YourWidget::resizeEvent(QResizeEvent * /* event */)
    {
    yourWebView->setGeometry(9, 9, 50, 25);
    }
    @

    @

    bool YourWidget::eventFilter(QObject *target, QEvent *event)
    {
    if (QResizeEvent resizeEvent = static_cast<QResizeEvent>(event))
    somesize.setWidth(width);
    else
    return QWidget::eventFilter(target, event); // this line is important!
    }
    @



  • Thank you all.
    The eventFilter Solve my problem.



  • Please add prefix "[SOLVED]" left to the topic subject. Thanks!



  • Well.. At the first look works... but know I got error!
    I use this code:
    @
    installEventFilter(this);
    @
    but now is not working... When I use this code the Widget is not showing, any error appears to me, just the widget is not showed!!!! If removed the Widget is showed normally!

    The code:

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

    #include <QWebFrame>
    #include "QtCore/QDebug"

    WidgetSVG::WidgetSVG(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetSVG)
    {
    ui->setupUi(this);
    }
    WidgetSVG::WidgetSVG(Veiculo *newVeiculo, QWidget *parent):
    QWidget(parent),
    ui(new Ui::WidgetSVG)
    {
    ui->setupUi(this);
    setVeiculo( newVeiculo );
    installEventFilter(this);
    }
    WidgetSVG::~WidgetSVG()
    {
    delete ui;
    }
    void WidgetSVG::setVeiculo(Veiculo newVeiculo)
    {
    this->veiculo = newVeiculo;
    ui->webView->setUrl( getVeiculo()->getFileUrl() );
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("sender", this);
    }
    Veiculo
    WidgetSVG::getVeiculo()
    {
    return this->veiculo;
    }
    void WidgetSVG::adjust()
    {
    qDebug() << "teste...";
    // 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::clicked(QString message)
    {
    qDebug() << "CLICKED --> " << message;
    }
    bool WidgetSVG::eventFilter(QObject *obj, QEvent *event)
    {
    qDebug() << "inside event filter...";
    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();
    }
    @

    How to fix it?!



  • As I wrote earlier:
    @return QWidget::eventFilter(target, event); // this line is important!@
    (this is important, 'cause if have any other event, later won't be handled)

    Forget the return "true".
    Hope this helps...



  • [quote author="broadpeak" date="1346160246"]two code snippet:
    @
    void YourWidget::resizeEvent(QResizeEvent * /* event */)
    {
    yourWebView->setGeometry(9, 9, 50, 25);
    }
    @
    [/quote]

    I'm trying this, and I got this error:

    @../SistemaAssistenciaTecnica/widgetsvg.cpp:75:43: error: no 'void WidgetSVG::resizeEvent(QResizeEvent*)' member function declared in class 'WidgetSVG'
    make: *** [widgetsvg.o] Error 1@

    The code:

    @
    void WidgetSVG::resizeEvent(QResizeEvent *)
    {
    adjust();
    }
    @



  • [quote author="broadpeak" date="1346248753"]As I wrote earlier:
    @return QWidget::eventFilter(target, event); // this line is important!@
    (this is important, 'cause if have any other event, later won't be handled)

    Forget the return "true".
    Hope this helps...[/quote]

    .h
    @
    public slots:
    bool eventFilter(QObject *obj, QEvent *event);
    @

    .cpp
    @
    bool WidgetSVG::eventFilter(QObject *obj, QEvent *event)
    {
    qDebug() << "inside event filter...";
    if (event->type() == QEvent::Resize)
    {
    adjust();
    }
    return QWidget::eventFilter(target, event);
    }
    @

    Error:

    @
    ../SistemaAssistenciaTecnica/widgetsvg.cpp: In member function 'virtual bool WidgetSVG::eventFilter(QObject*, QEvent*)':
    ../SistemaAssistenciaTecnica/widgetsvg.cpp:73:33: error: 'target' was not declared in this scope
    ../SistemaAssistenciaTecnica/widgetsvg.cpp: At global scope:
    ../SistemaAssistenciaTecnica/widgetsvg.cpp:66:6: warning: unused parameter 'obj'
    ../SistemaAssistenciaTecnica/widgetsvg.cpp: In member function 'virtual bool WidgetSVG::eventFilter(QObject*, QEvent*)':
    ../SistemaAssistenciaTecnica/widgetsvg.cpp:74:1: warning: control reaches end of non-void function
    @



  • Sorry:

    @
    bool WidgetSVG::eventFilter(QObject *obj, QEvent *event)
    {
    qDebug() << "inside event filter...";
    if (event->type() == QEvent::Resize)
    {
    adjust();
    }
    return QWidget::eventFilter(obj, event);
    }
    @



  • @
    bool WidgetSVG::eventFilter(QObject *obj, QEvent *event)
    {
    qDebug() << "inside event filter...";
    if (event->type() == QEvent::Resize)
    {
    adjust();
    return true; // if you have handled the event (what you have filtered), HERE return "true"
    }
    // if you have NOT handled the event (other events), return with this:
    return QWidget::eventFilter(obj, event);
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.