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

QtSvg on Linux doesn't work



  • Hi, I'm developing an application on Windows and Linux, but when I try to build it on Linux, I have a problem. I'm using svg files, which work fine on Windows, but when I try to run it on Linux, it doesn't work. I checked the files and all seems to be good. I don't know why it doesn't work.



  • @Sucharek
    I use QtSvg under Ubuntu 20.04 and it works fine.

    Nobody can help further from your statements like

    but when I try to run it on Linux, it doesn't work



  • @Sucharek said in QtSvg on Linux doesn't work:

    I checked the files and all seems to be good. I don't know why it doesn't work.

    Very hard to help you with this description!
    Work's for me (™)

    One tip: did you check filename case... Linux file system is case-sensitive



  • Hi, I forgot to add an important detail. I'm on Arch. I installed the qt5 package on Arch.
    EDIT: Some more info: 5.10.79-1-MANJARO



  • Hi, so I fixed it. I initialized the svg file paths when declaring the variable, and that works on Windows, but for some reason not on Linux. I had:

    QGraphicsSvgItem *svg = new QGraphicsSvgItem("path/to/svg.svg");
    

    on the top, but that doesn't work. I only had to add:

    QGraphicsSvgItem *svg;
    

    at the top, and declare it in the void that has "ui->setupUi(this);" in it (don't know what's the name of that part):

    ui->setupUi(this);
    svg = new QGraphicsSvgItem("path/to/svg.svg");
    


  • @Sucharek said in QtSvg on Linux doesn't work:

    on the top

    Assuming that means "at the top of a source file, outside of any method" you should never create a new QGraphicsSvgItem there (nor more or less any Qt UI object). Your statement will execute before you have created the QApplication in your main(), and until then it is undefined behaviour to create any such UI object. You are "lucky" it worked in Windows at all.

    Also, for the record, does your "path/to/svg.svg" imply this is a relative path? Paths to such files should always be absolute (or in Qt resources).



  • Hi @JonB, thats for telling me. I never knew, and used it in almost every of my project. I'll keep an eye on that.
    And yes, I used resources for the svg image.



  • @Sucharek as @JonB pointed out, the placement "at the top fo source file" is not a good idea.

    Given that you have these statements:

    ui->setupUi(this);
    svg = new QGraphicsSvgItem("path/to/svg.svg");

    which implies some kind of constructor for a widget, you may want to add svg as a member of that widget class (in the class header file):

    ...
    private:
    ...
    QGraphicsSvgItem *svg;
    ...
    

    then in the constructor you could do:

    ui->setupUi(this);
    svg = new QGraphicsSvgItem("path/to/svg.svg", this);
    

    to take advantage of Qt's memory management feature
    (in your example, you must delete the svg object yourself...)


Log in to reply