qt - 在 QtQuick 中显示一个没有任何顶层窗口的弹出窗口
问题描述
我想制作一个游戏叠加层来显示我的自定义十字准线。我希望它一直存在,没有关闭政策。
我Popup
在 an 中使用了一个项目,ApplicationWindow
我将它的不透明度设置为0
,并将弹出窗口的不透明度设置为,1
但它什么也没显示。我也尝试使用没有任何顶层窗口的弹出项目,但又什么也没有。
问题是“是否可以在没有任何可见的顶级窗口的情况下显示始终在顶部的弹出窗口?”
您的建议将不胜感激。
解决方案
如果没有弹出窗口,我们可以尝试使用 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"
}
}
推荐阅读
- gcc - __attribute__ ((warn_unused_result)) 不会为 shared_ptr 生成警告
- java - 将 weblogic 迁移到 tomcat:没有可用的 JTA UserTransaction
- clam - 套接字上的 ClamAV clamd INSTREAM 的间歇性问题
- swift - 图片资源和 NSImageView
- react-native - 当反应本机应用程序在后台时如何保持套接字打开?
- linux - 收集linux上所有连接的设备
- php - html表格中的按钮未对齐
- python - 如何删除有关使用 Word2vec gensim\matutils.py:737 的 gensim 警告
- macos - 将自定义文件添加到要使用 productbuild/pkgbuild 安装在特定路径上的软件包?
- macos - 分别使用 .fill 和 .blit 来解决这两个问题时,Python 会冻结和崩溃