Images in svg file doesn't show up in Qt - qt

I'm trying to display a svg image with Qt and it works well except that any image (.gif, .bmp, ...) in the svg image doesn't show up (lines, circles, texts works OK).
If I open the svg in firefox the image displays correctly.
I have put qgif4.dll in a folder imageformats and QtSvg4.dll besides the executable but to no avail.
I'm displaying the svg with the help of svgview.h/.cpp and I'm running on a WinXP machine (Qt version 4.7.2).
Any help greatly appreciated!

If you are loading it via a QIcon, it should work without assistance.
If you are trying to load it using a QPixmap or QImage - you can't. You have to use QSvgRenderer (or the related classes) in the svg module to rasterise the data.

Related

Pixelated gif in QT

I'm trying to show a gif file in QT app, using the approach provided in the link: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/widgets/movie?h=5.15
Approach makes use of QMovie object set in a QLabel.
The example works well and fine.
But if I enable High DPI scaling for the app, the gif becomes all pixelated. Please see the screenshots below.
This is the line that I add to enable High DPI scaling.
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
Any ideas to get this fixed ?
I have tried the following fixes already:
setScaledSize for the QMovie object
setScaledContents(true) for the QLabel
QT version I'm using is 5.15.2 and platform is Windows.
A GIF picture cannot have more than 256 unique colours. When you load such an image into Qt, it is internally represented in that exact format with the palette (of 256 colours) from the GIF representation, even if your hardware might be able to display many more colours.
This also means that when you scale such an image, Qt is not allowed to extend the colour space to render in-between colours - This means that scaled GIF pictures generally have to look much worse than scaled high-colour images.
The solution to this is either to transform the QImage you created from a GIF picture into a format with a larger colour space before scaling it (with QImage::convertToFormat) or, better still, don't use GIF images at all. After all, GIF is a format developed 30 years ago and has never really been updated to adapt to modern hardware, and using it, you artificially limit your programs to the capabilities of that format.
It seems at this moment there is no way to render sharp gifs when scaled using QMovie and QLabel. I have filed a bug for the same in QT bug tracker.
Meanwhile I have found a workaround that works fine. It is done making use of QML in the QWidget system, using QQuickWidget.
Let me add the full steps here, so that it is helpful to anyone else who run into this problem:
First we need to add support for Qml and QuickWidgets. I use CMake and Visual Studio. Hence I add the below lines in the CMakeLists.txt file. Equivalent changes needs to be made in the .pro file, if QT Creator is used instead.
# I'm only adding the relevant lines for brevity
find_package(Qt5 COMPONENTS Qml QuickWidgets)
target_link_libraries(${APP_TARGET_NAME}
Qt5::Qml
Qt5::QuickWidgets)
# Note the --qmldir switch
add_custom_command (TARGET ${APP_TARGET_NAME} POST_BUILD
COMMAND ${QTDIR}/bin/windeployqt
--qmldir ${CMAKE_CURRENT_SOURCE_DIR}/qml
${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${APP_TARGET_NAME}.exe)
Then create a spinner.qml file inside a folder named qml:
import QtQuick 2.15
Rectangle {
width: 12
height: 12
AnimatedImage {
y: 5
width: 12
height: 12
id: spinner
source: "img/spinner.gif"
speed: 1.0
}
}
And then load the qml file using QQuickWidget and add the QQuickWidget instance to the QWidget layout.
QHBoxLayout *main_layout = new QHBoxLayout();
auto *spinner_gif = new QQuickWidget(QUrl::fromLocalFile(":/spinner.qml"));
main_layout->addWidget(spinner_gif);
this->setLayout(main_layout);
The gif that shows up won't be pixelated even when scaled and we can see the GPU in use, in the Task Manager, as expected for a QQuickWidget.

How to show an svg image?

How to show an svg image in xamarin.forms? I have an cross platform project where png works great instead of svg. In my solution I have UWP,iOS,Android and I add macOs as well.

QPixmap with .svg "couldn't create image from"?

i try to get an .svg to an QPixmap and i get the Error "couldn't create Image from".
Code:
QString test = "/home/ftp/DM262-WIF-269-0.svg";
QPixmap design(test);
ui->scan_bild->setPixmap(design);
ui->scan_bild->setScaledContents(true);
is there a Resolution for my Problem?
Thx
Felix
The SVG loader of Qt supports only a subset of SVG. It seems that embedding of raster graphics images in SVGs is not supported. (If I remember right embedding of SVGs in other SVGs doesn't work as well. – Too sad.)
To prove that I don't tell something wrong, I tested an icon of our software.
The following image shows how the Explorer Preview plug-in (based on Qt) shows the image:
The same image loaded into Firefox:
When I import this image in GIMP, the embedded image is considered as well.
So, if you don't want to re-design your SVG icon I see only one solution: render the SVGs into raster graphics images (e.g. PNG or JPEG) which are better supported in Qt – may be, with multi-resolution icons (QIcon – High DPI icons) for a better result.

css issue while downloading png image from svg in nvd3

I have one line chart using nvd3.js . I want to download this chart in .png format.
I am using canvas and sending the canvas.toDataURL("image/png") to server and downloading the image, but the image is not in proper shape .it seems the css(nvd3.css) is not getting applied.
Any help will be really heplfull.
Thanks,
chandan
i have the same requirement and "example of how to export a png directly from an svg" solved my problem (internally it adopted svg-crowbar code. basically, you need to "explicitly set css style" for all svg element.

Vertical scrollbar scrollup-scrolldown images look jagged on Qtopia machine

I am trying to use a vertical scrollbar in my Qt project.
The issue is when use the Cleanlooks widget style for the vertical scrollbar,it looks ok on the Linux Ubuntu machine,where Qt-4.3.3 is running.
But,when i run the same project on a Qtopia-4.3.3 Linux(ARM) machine,the scrolldown and scrollup images look totally jagged. They,look very bad. I tried using stylesheets,but without luck. Any suggestions regarding using stylesheets are most welcome.
Is there any way,to overcome this problem?
In my relatively small experience with styles on embedded platforms, some of the styles are designed so they use an image for certain things, and scale it as necessary. This produces very jagged graphics if the source is small and the target it large. One possibility is to inherit your own QStyle from the Cleanlooks style, and override the drawing of the arrow images with your own drawing code. It shouldn't be too hard to draw a proper arrow in code, rather than using an image.

Resources