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

How to display SVG in a QGroupBox ?



  • Hi everyone !

    I'm trying to display some SVG files in my software. In some places, the SVG is displayed. In some others, not...

    The code to load the files is the same for the entire set of images and if I force to display the same SVG everywhere, the problem remains the same.

    SvgWebViewWidget::SvgWebViewWidget ( const QString pImagePath, QWidget* pParent )
         : QWebEngineView( pParent )
     {
             // Load image
             this->load( QUrl::fromLocalFile( pImagePath ) );
         }
     }
    
    QGroupBox* lChildContainer = new QGroupBox( Name );
    lChildContainer->setStyleSheet( "QGroupBox { font-weight: bold };" );
    lChildContainer->setLayout( new QVBoxLayout() );
    lChildContainer->layout()->setSpacing( 0 );
    
     //pContainer is a QWidget *
     pContainer->layout()->addWidget( lChildContainer );
    
     QWidget* svgimage = new SvgWebViewWidget( lImagePath, this );
    
    lChildContainer->layout()->addWidget( svgimage);
    

    If I add the Image directly to pContainer, the SVG is displayed (but I need the QGroupBox).

    Do you have any idea to solve this bug ? Thanks



  • Hi !

    I really want to thank everyone who helped me ! I found the bug somewhere deeply in the code (actually, I was doing something else than finding it).
    I have a filter which hides or shows what I want. And when I upgraded WebView to WebEngineView, I forget to change it in the filter.

    Thanks again.



  • Hi, there is a QSvgWidget class in the Qt SVG module


  • Lifetime Qt Champion

    @Hirisar

    QGroupBox* lChildContainer = new QGroupBox( Name );
    lChildContainer->setStyleSheet( "QGroupBox { font-weight: bold };" );
    lChildContainer->setLayout( new QVBoxLayout() );
    lChildContainer->layout()->setSpacing( 0 );
    
    QWidget* svgimage = new SvgWebViewWidget( lImagePath, this );
    lChildContainer->layout()->addWidget(svgimage);
    
    lChildContainer->show();
    

    As shown in https://doc.qt.io/Qt-5/qgroupbox.html



  • @Bonnie said in How to display SVG in a QGroupBox ?:

    Hi, there is a QSvgWidget class in the Qt SVG module

    It does not fix the problem :'(



  • @jsulm said in How to display SVG in a QGroupBox ?:

    @Hirisar

    QGroupBox* lChildContainer = new QGroupBox( Name );
    lChildContainer->setStyleSheet( "QGroupBox { font-weight: bold };" );
    lChildContainer->setLayout( new QVBoxLayout() );
    lChildContainer->layout()->setSpacing( 0 );
    
    QWidget* svgimage = new SvgWebViewWidget( lImagePath, this );
    lChildContainer->layout()->addWidget(svgimage);
    
    lChildContainer->show();
    

    As shown in https://doc.qt.io/Qt-5/qgroupbox.html

    Thanks for this solution but the problem stays the same. If I load a PNG file instead of a SVG file, it runs properly (but I need to use SVG).
    And if I load a SVG file and I add a QDoubleSpinBox to lChildContainer, it works (but I don't want the last one)...

    Weird :'(



  • @Hirisar Just think maybe there is no need to use a QWebEngineView to show SVG.
    That's too heavy ;)



  • Could it be that because of the layout your SVG widget is shrunk down to 0 size while with a PNG it is not? Try calling setMinimumSize on the SVG widget to figure out if this is the problem.



  • @SimonSchroeder I tried but it didn't solve my problem...


  • Lifetime Qt Champion

    Hi
    I have zero issues showing a SVG in Groupbox using QSvgWidget.

       QGroupBox* lChildContainer = new QGroupBox( "Name", centralWidget() );
        lChildContainer->setStyleSheet( "QGroupBox { font-weight: bold };" );
        lChildContainer->setLayout( new QVBoxLayout() );
        lChildContainer->layout()->setSpacing( 0 );
    
        QSvgWidget* svgimage = new QSvgWidget( ":/Panneau_attention.svg" );
        lChildContainer->layout()->addWidget(svgimage);
    
        lChildContainer->show();
    
    

    so either its related to SvgWebViewWidget or the actual code itself.

    alt text



  • Hi !

    I really want to thank everyone who helped me ! I found the bug somewhere deeply in the code (actually, I was doing something else than finding it).
    I have a filter which hides or shows what I want. And when I upgraded WebView to WebEngineView, I forget to change it in the filter.

    Thanks again.


Log in to reply