QPixmap and SVG - qt

How would you suggest to handle svg with QPixmap?
The construct QPixmap(":/myfile.svg"); then call of scaled() does not work. The QPixmap gets pixelised.
Thx.

Now there is much simpler way without needing of SVG module
QIcon("filepath.svg").pixmap(QSize())
So simple and works fine. Atleast in my case it worked.

You should use SVGRenderer to render it onto a QImage. From there you can convert to a QPixmap with QPixmap::convertFromImage.

Something like that:
QSvgRenderer renderer(svg_file_name);
QPixmap pm(width, height);
pm.fill(fill_color);
QPainter painter(&pm);
renderer.render(&painter, pm.rect());

On Qt5.12 i managed to display SVG icons without pixellisation in a QLabel:
QPixmap logoPixmap(":my-logo.svg"); // set your logo here
auto logoLabel = new QLabel(this);
logoLabel->setPixmap(logoPixmap);
logoLabel->setScaledContents(true);
logoLabel->setFixedSize(176, 61); // set your size here

Related

Tiled image in QGraphicsView foreground

I'm working on a Qt application made with a main QGraphicsView.
This view can show and switch between differents QgraphicsScenes. This application needs to always have an overlay in front of each scenes, so the best way to do this overlay is by setForegroundBrush() method of QGraphicsView.
But my overlay is a tiled-image, where I could edit the opacity and the scale of the source image.
Here's the code written in my QGraphicsView class constructor :
QString imgPath("path/to/image.png");
QPixmap map(imgPath);
QPainter painter(this);
QRectF zone(0,0,map.width(),map.height());
painter.drawPixmap(zone,map,zone);
QBrush brush = painter.brush();
brush.setStyle(Qt::TexturePattern);
setForegroundBrush(brush);
But doesn't work, nothing is shown.
I tested a simple QBrush with a QPixmap and works fine, but I need to use QPainter to be able to edit the opacity of my image.
Finally I think the easiest way to have a tiled image in QGraphicsView foreground is by reimplmenting the drawForeground(QPainter *painter, const QRectF &rect).
void Frontend::drawForeground(QPainter *painter, const QRectF &rect){
float alpha = 0.15;
float scale = 2;
QString imgPath("path/to/image.png");
QPixmap img(imgPath);
painter->scale(scale,scale);
painter->setOpacity(alpha);
painter->drawTiledPixmap(rect,img);
}
You cannot paint on the widget outside of its paintEvent method. Perhaps you wanted to have the painter work on the pixmap (painter(&map)) instead of the widget (painter(this))?
You could also add an overlay by:
Painting it in the reimplemented paintEvent of your derived scene, making sure that you paint not on the scene, but on its viewport(). There are convenience methods that are called by the view's paintEvent, such as drawBackground and drawForeground.
Painting it in a generic QWidget overlay.
I have several answers that demonstrate how to get overlays over widgets in generaly, and also on scene views.

Rotate QPixmap on a QGraphicsScene around a point

My problem is the following:
I have a QGraphicsScene, there is a QPixmap on it. I would like to rotate that pixmap around a center point, would work like a clock actually.
I've tried these:í
QPixmap pointer_pixmap("/home/peter/desktop/myimg2.png");
QTransform transform;
QGraphicsPixmapItem *pointer = new QGraphicsPixmapItem(pointer_pixmap);
pointer->setOffset(174,190);
pointer->setTransformOriginPoint(QPoint(174-pointer_pixmap.width(), 190-pointer_pixmap.height()));
transform.translate((174-pointer_pixmap.width())/2,(190-pointer_pixmap.height())/2);
transform.rotate(60);
transform.translate(-((174-pointer_pixmap.width())/2),-((190-pointer_pixmap.height())/2));
pointer_pixmap = pointer_pixmap.transformed(transform);
item->addItem(pointer);
pointer->setPixmap(pointer_pixmap);
It looks like the translation doesn't have any effect on my pixmap. Why's that?
You should rotate the QGraphicsPixmapItem, not the pixmap itself.
Use QGraphicsItem::setTransformOriginPoint to define the transform origin point and QGraphicsItem::setRotation to rotate the item.

QT - Resize QToolbar

i have some concatenate toolbar. For every toolbar i have call:
toolbar->setGeometry(x,y,width,height)
but i have no resize.
I try to call
toolbar->updateGeometry();
but nothing.
My goal is to expand every toolbar with my size definition
There is a good chance you are using this for repositioning your toolbars on init and saving at closing.
Here is a solid way to do that:
What you really need is to use the QMainWindow saveGeometry() and restoreGeometry() functions and save and load the byte array through the QSettings interface.
writeSettings
QSettings s;
s.beginGroup("MainWindow");
this->restoreGeometry(s.value("geometry").toByteArray());
this->restoreState(s.value("windowState").toByteArray());
s.endGroup();
readSettings
QSettings s;
s.beginGroup("MainWindow");
s.setValue("geometry", saveGeometry());
s.setValue("windowState", saveState());
s.endGroup();
Hope that helps.
You can try QWidget::resize( int w, int h ) to resize the toolbar.
toolbar-> resize( 200, 20 );
The toolbar's geometry is either managed by a layout or by the main window.
You'd need to show how is the toolbar used/displayed.

Render existing Qt Widget into SVG

I have an exisiting Graph displayed as QWidget which I can already save to a bitmap using grab():
QPixmap image = grab();
Is there a Possibility to save that widget also to .svg?
Thanks!
QSvgGenerator generator;
generator.setFileName(path);
generator.setSize(widget->size());
generator.setViewBox(widget->rect());
generator.setTitle(tr("Your title"));
generator.setDescription(tr("some desscription"));
widget->render(&generator);

Display window full screen on secondary monitor using Qt

Seems to be possible with native controls (see here and here) so now I'm looking for some Qt code to do it.
I use this code for the second display in full screen successfully on both Windows & Linux
QRect screenres = QApplication::desktop()->screenGeometry(1/*screenNumber*/);
SecondDisplay secondDisplay = new SecondDisplay(); // Use your QWidget
secondDisplay->move(QPoint(screenres.x(), screenres.y()));
secondDisplay->resize(screenres.width(), screenres.height());
secondDisplay->showFullScreen();
One way of doing it in Qt5 is to use QWindow::setScreen to set the screen on which the window should be shown. QWidget has a windowHandle() that returns the pointer to the QWindow.
Here is how to show your widget on second screen in full-screen mode :
QWidget * widget = new QWidget();
widget->show();
widget->windowHandle()->setScreen(qApp->screens()[1]);
widget->showFullScreen();
My take on this:
auto const desktop(QApplication::desktop());
setGeometry(desktop->screenGeometry(1));
#ifndef Q_OS_WIN
setWindowState(Qt::WindowState(Qt::WindowFullScreen | windowState()));
#endif // Q_OS_WIN
showFullScreen first, then setGeometry.
Qt5 tested OK
This problem got solved while using window->showFullScreen() instead of window->show().

Resources