2

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!

DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69

1 Answers1

7

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();
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Oh, great, thank you! And is there way without editing original qt module code? Because I got free version, and as far as I know I am not suppose to edit such things – DanilGholtsman Nov 11 '13 at 18:35
  • @DanilGholtsman, actually there is no change in Qt source code. The sample code I posted is supposed to be your own code, and you can do this without any restriction. – vahancho Nov 11 '13 at 20:11
  • 1
    oh, I see! there is extending! first time I saw it i was a distracted little bit, sorry for really stupid question ._. – DanilGholtsman Nov 12 '13 at 19:25