首页 > 解决方案 > 在图层上方添加元素

问题描述

我在 Widows 10、QtCreator 和 QtDesigner 上使用 Qt (5.12.4) 设计了一个 GUI。在这个中,我添加了很多层来做一些根据窗口大小调整大小的事情。

但我要求在中间的这个 GUI 之上添加一个图像。如果不重做我的所有图层,这将无法完成,那么是否可以在 GUI 的某处添加一个元素(此处为图像)而不将其包含在我的一个图层中?

预先感谢您的帮助。最好的祝福

标签: qtuser-interfacelayer

解决方案


我终于按照@Farshid616 的链接,创建了我自己的类:

class Overlay : public QWidget {
public:
    explicit Overlay(QWidget *parent = nullptr) : QWidget(parent) {
        setAttribute(Qt::WA_NoSystemBackground);
        setAttribute(Qt::WA_TransparentForMouseEvents);
        QPixmap pic("://Ressources/img/tonneauGravesMarques.jpg");
        int width = 117;
        int height = 74;
        QPixmap scaled=pic.scaled ( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );

        QLabel *label = new QLabel(this);
        label->setPixmap(scaled);

        this->resize(width, height);
    }
};

并在我的主要功能中添加一个像这样的项目:

MainWindow::MainWindow(QWidget *parent) : AbstractMainWindow(parent)
{
    qDebug() << "TarMainWindow::TarMainWindow";
    ui->setupUi(this);

    // Create widget for displaying image above all layers
    m_overlay = new Overlay;

    // Add this widget to main window
    this->layout()->addWidget(m_overlay);
}

它正在工作!


推荐阅读