关于Qt QMdiArea背景的图片

Dan*_*man 2 qt image qt4 qmdiarea

Qt开发人员!有没有办法在我的midArea的背景上添加图像,如下图所示?

在此输入图像描述

我知道我可以使用这样的东西

QImage img("logo.jpg");
mdiArea->setBackground(img);
Run Code Online (Sandbox Code Playgroud)

但我不需要在背景上重复我的图像.

谢谢!

vah*_*cho 6

正如我在上面的评论中所说,您可以对其进行子类化QMdiArea,覆盖其paintEvent()功能并自行绘制徽标图像(在右下角).以下是实现上述想法的示例代码:

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;
};
Run Code Online (Sandbox Code Playgroud)

最后在主窗口中使用自定义mdi区域:

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();
}
Run Code Online (Sandbox Code Playgroud)