Search code examples
imageqtqt4qmdiarea

Image on Qt QMdiArea background


Qt developers! Is there are way to add image on the background of my midArea like on picture below?

enter image description here

I know I can use something like this

QImage img("logo.jpg");
mdiArea->setBackground(img);

But I don't need any repeat of my image on the background.

Thank you!


Solution

  • As I said in my comment above, you can sub-class the QMdiArea, override its paintEvent() function and draw your logo image yourself (in the bottom right corner). Here is the sample code that implements the mentioned idea:

    class MdiArea : public QMdiArea
    {
    public:
        MdiArea(QWidget *parent = 0)
            :
                QMdiArea(parent),
                m_pixmap("logo.jpg")
        {}
    protected:
        void paintEvent(QPaintEvent *event)
        {
            QMdiArea::paintEvent(event);
    
            QPainter painter(viewport());
    
            // Calculate the logo position - the bottom right corner of the mdi area.
            int x = width() - m_pixmap.width();
            int y = height() - m_pixmap.height();
            painter.drawPixmap(x, y, m_pixmap);
        }
    private:
        // Store the logo image.
        QPixmap m_pixmap;
    };
    

    And finally use the custom mdi area in the main window:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QMainWindow mainWindow;
        QMdiArea *mdiArea = new MdiArea(&mainWindow);
        mainWindow.setCentralWidget(mdiArea);
        mainWindow.show();
    
        return app.exec();
    }