首页 > 解决方案 > 在 QtQuick 中显示一个没有任何顶层窗口的弹出窗口

问题描述

我想制作一个游戏叠加层来显示我的自定义十字准线。我希望它一直存在,没有关闭政策。

Popup在 an 中使用了一个项目,ApplicationWindow我将它的不透明度设置为0,并将弹出窗口的不透明度设置为,1但它什么也没显示。我也尝试使用没有任何顶层窗口的弹出项目,但又什么也没有。

问题是“是否可以在没有任何可见的顶级窗口的情况下显示始终在顶部的弹出窗口?”

您的建议将不胜感激。

标签: qtqmlqt5qtquick2popupwindow

解决方案


如果没有弹出窗口,我们可以尝试使用 QQuickItem 的 'z' 属性。始终将十字准线堆叠在其他物品之上。如果你想在屏幕上移动十字准线,你可以使用它们的 'x,y' 属性。

我已经尝试了一个简单的示例。在滚动视图顶部使用图像项目作为十字准线。它按预期工作。我尝试了我的方法。我们将看看是否有其他想法出现。

示例代码在这里:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480

    // used here only to indicate image loading is going on
    BusyIndicator {
        id: busyindicatorId
        visible: backgroundImgId.status === Image.Loading ||
                 crossImgId.status === Image.Loading
        anchors.centerIn: parent
    }

    // background item
    ScrollView {
        anchors.fill: parent
        anchors.margins: 10
        clip: true
        visible: !busyindicatorId.visible
        Image {
            id: backgroundImgId
            source: "https://i.ibb.co/ZBNLvzb/andriod.jpg"
        }
    }

    // crosshair item
    Image {
        id:  crossImgId
        z: 1
        width: 100
        height: width
        visible: !busyindicatorId.visible
        source: "https://i.ibb.co/SJFTLwN/cross.png"
        anchors.centerIn: parent
    }
}

更新 实例化的两个窗口可用于十字准线,并且必须设置一些窗口属性(透明,alwaysontop)以始终显示在顶部。让我们看一下这个基本代码。示例视频

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QScreen>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    QScreen *screen = QGuiApplication::primaryScreen();
    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:/crosshair.qml")));
    view.setX(screen->geometry().width()/2 - view.width()/2);
    view.setY(screen->geometry().height()/2 - view.height()/2);
    view.setColor("transparent");
    view.setFlags(Qt::SubWindow | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    view.show();

    return app.exec();
}

//////////////// main.qml ////////////////
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true
    visibility: "FullScreen"
    objectName: "mainWindow"

    // used here only to indicate image loading is going on
    BusyIndicator {
        id: busyindicatorId
        visible: backgroundImgId.status === Image.Loading
        anchors.centerIn: parent
    }

    // background item
    ScrollView {
        anchors.fill: parent
        anchors.margins: 10
        clip: true
        visible: !busyindicatorId.visible
        Image {
            id: backgroundImgId
            source: "https://i.ibb.co/ZBNLvzb/andriod.jpg"
        }
    }
}

//////////////// crosshair.qml ////////////////
import QtQuick 2.9
import QtQuick.Controls 2.2

Item {
    width: 100
    height: 100
    // crosshair item
    Image {
        width: parent.width
        height: parent.height
        source: "https://i.ibb.co/SJFTLwN/cross.png"
    }
}

推荐阅读