質問

QT開発者!下の写真のように、私の中庭の背景に画像を追加する方法はありますか?

enter image description here

私はこのようなものを使うことができることを知っています

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

しかし、背景に自分の画像を繰り返す必要はありません。

ありがとうございました!

役に立ちましたか?

解決

上記の私のコメントで言ったように、あなたはサブクラスすることができます 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;
};

そして最後に、メインウィンドウでカスタム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();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top