繁体   English   中英

QQuickView 在调用 show 时显示一个空的 window

[英]QQuickView shows an empty window when calling show

我仍在尝试使用 QML + c++ 来学习编码,所以这个代码片段的本质可能听起来有点不切实际。 我正在尝试编写一个继承自QQuickView并加载qml文件的 class 。 这是我的 header 文件:

#ifndef FORMLOGIN_H
#define FORMLOGIN_H

#include <QQuickView>


class FormLogin:public QQuickView
{
public:
    FormLogin();
public slots:
    void myHide();
};

这是它的实现:

#include "formlogin.h"
#include<QCoreApplication>
#include<QQmlEngine>
FormLogin::FormLogin()
{
    this->setSource(QUrl("qrc:/main.qml"));
    QObject::connect(this->engine(),&QQmlEngine::quit,&QCoreApplication::quit);

}
#include "iostream"
using namespace std;
void FormLogin::myHide(){
    cout<<"called"<<endl;
    if(this->isVisible()){
        this->hide();
    }else{
        this->show();
    }
}

这是我的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include"formlogin.h"
#include<iostream>
#include<QTimer>
using namespace std;
FormLogin *formLogin;
QTimer *myTimer;
int main(int argc, char *argv[])
{
    //QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    
    formLogin=new FormLogin();
    myTimer=new QTimer();
    myTimer->start(3000);
    QObject::connect(myTimer,&QTimer::timeout,formLogin,&FormLogin::myHide);
    
    return app.exec();
}

我的main.qml看起来像这样:

import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtGraphicalEffects 1.0
Window {
    Rectangle{
        anchors.fill: parent
        color: "lightblue"
    }   

    title: qsTr("Title")
    width: 450; height: 550
    visible: true
}

现在,您可以从代码中了解到它有一个连接到myHideQTimer ,它显示和隐藏我的登录表单。 以下是我运行应用程序时发生的情况:

  1. 它打开了我在 main.qml 中设计的main.qml
  2. 它会在 3 秒后打开一个空的 window;
  3. 它在 3 秒后隐藏了空的 window 并继续这种方式。

我个人的期望是它不应该首先显示我的 QML 表格,它应该等到定时器的超时信号发出。 然而,它首先显示正确的 QML window 然后它开始显示奇怪的空 window 每次调用this->show时。 现在是我提问的时候了:

  1. 为什么它显示我的 QML window 而没有“formLogin->show()”?
  2. 这个空的 window 是什么?
  3. 为什么我不能隐藏我的主 window?

解释

问题是因为 QQuickView 需要一个项目作为根,而不是 Window。

问题是因为 QQuickView 需要一个元素作为根,而不是 window。 您的程序执行以下操作:

  1. 当您启动 window 时,可见的是 QML
  2. 然后出现QQuickView window
  3. 在它隐藏 QQuickView 并通过第 2 步之后。

步骤 1 和 2 分别回答您的问题 1 和 2。 对于问题 3,答案是您只隐藏了第二个 window。

QQmlApplicationEngine 文档中指出了这一点:

与 QQuickView 不同,QQmlApplicationEngine 不会自动创建根 window。 如果您使用 Qt Quick 中的视觉项目,则需要将它们放置在 Window 内。

解决方案

所以有2个选项:

  • 或将 Window 替换为项目:

     import QtQuick 2.0 Rectangle{ anchors.fill: parent color: "lightblue" }
     #include "formlogin.h" #include<QCoreApplication> #include<QQmlEngine> #include <iostream> FormLogin::FormLogin() { setSource(QUrl("qrc:/main.qml")); connect(engine(), &QQmlEngine::quit, &QCoreApplication::quit); setTitle("Title"); resize(450, 550); } void FormLogin::myHide(){ std::cout << "called" << std::endl; setVisible(;isVisible()); }
     #include <QGuiApplication> #include <QTimer> #include "formlogin.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); FormLogin formLogin; QTimer myTimer; myTimer.start(3000); QObject::connect(&myTimer, &QTimer::timeout, &formLogin, &FormLogin::myHide); return app.exec(); }
  • 或者使用 QQmlApplicationEngine 代替 QQuickView

加:

如果您想从 QML 修改 window,那么您有 2 个选项:

  1. 或者在 C++ 插槽中实现逻辑并从 QML 调用它。

  2. 或使用 setContextProperty 将 window 导出到 QML

     FormLogin::FormLogin() { rootContext()->setContextProperty("window", this); setSource(QUrl("qrc:/main.qml")); connect(this->engine(),&QQmlEngine::quit,&QCoreApplication::quit); setTitle("Title"); resize(450, 550); }
     import QtQuick 2.0 Rectangle{ id: root anchors.fill: parent color: "lightblue" Component.onCompleted: { window.flags = (window.flags | Qt.CustomizeWindowHint | Qt.FramelessWindowHint) & ~Qt.WindowTitleHint // window.showMaximized() } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM