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

QGraphicsView with QPixMap Background



  • Hello,

    I want to set a QPixMap as background of a QGraphicsView. I can't find a way to do it via the doc.
    Anyone knows how to preceed?

    Thanks





  • If i follow you and the doc, I've got to overload this method.
    I would do that

    @void custom_view::drawBackground(QPainter *p, const QRectF &rect)
    {
    QpixMap Background("./toto.png");
    p->drawPixmap(QRect(0, 0, width(), height()), Background);
    }
    @
    And then call it into the constructor.

    Am I right?



  • No, don't call it in the constructor. Just pass the scene to the view.

    You might want to cache the image, as to prevent unnecessary disk access, but that's for later I guess. You should probably learn more about the "graphics/view framework":http://doc.trolltech.com/latest/graphicsview.html.



  • You can also use stylesheet (set the image of interest as border-image) and apply it on graphicsview. This can be the case if you are interested in applying the pixmap in QGraphicsPixmapItem rather than the item itself.



  • In fact, I thought about using stylesheets but I my case I don't know if it's possible.
    See, I don't use a file for the pixmap, but the method grabWidget which catches a widget appearance into a QPixMap. That way, I don't have a absolute path to put to the stylesheet.
    Is it possible to use the stylesheet tho?



  • Stylesheet is file agnostic. You can do
    @setStyleSheet("background-image:your_pixmap_path");@

    Else go with drawBackground.



  • When you say your_pixmap_path, you talk about a real path don't you.
    What I would like to do would be something like :

    (this is a simplied code of course)
    in the main window class :

    @QMdiArea mdi;
    setCentralWidget(mdi)
    QPixmap pix = QPixmap::grabWidget(mdi);
    this->setStyleSheet("background-image:" + pix);@

    This does not work but is there a similar way to do it?



  • Yes, it is real path. You can say pix.save("your_pixmap_path.png") and use it in styleSheet



  • Okay, I see what you mean.
    I was trying to avoid dealing with saving a pixmap i have in my prog to relaod it in the stylesheet of the same prog but I guess there's no other way by using stylesheets


Log in to reply