gpt4 book ai didi

c++ - QT 中继承自 QWindow 的另一个类的 Resize 事件

转载 作者:行者123 更新时间:2023-11-30 03:39:36 28 4
gpt4 key购买 nike

我正在编写一个简单的程序(C++QTQML),我想在其中实现resizeEvent 然后使用某种模式调整窗口的高度和宽度。我的问题是,当我调整窗口大小时,resizeEvent 没有被调用。我认为我做错了什么,但我不确定是什么。任何想法将不胜感激。

主要.cpp

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QObject *root = engine.rootObjects().first();
class CMaze maze(root,&engine);
return app.exec();
}

CMaze.h

class CMaze: public QWindow
{
public:
CMaze(QObject *root, QQmlApplicationEngine *engine);
private:
QObject *root;
QQmlApplicationEngine *engine;
/*+ Some other variables*/
void resizeEvent(QResizeEvent *event);
};

CMaze.cpp

CMaze::CMaze(QObject *root,QQmlApplicationEngine *engine)
{
this->root = root;
this->engine = engine;
/* + Some other functionality*/
}
void CMaze::resizeEvent(QResizeEvent *event)
{
qDebug() << "resize event entered"; // NEVER WRITTEN to CONSOLE WHEN RESIZING
}

编辑:

主要.qml:

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("The Maze")

Rectangle{
id: background
objectName: "background"
anchors.fill: parent
color: "#ffffcc"
}
}

最佳答案

您声明了 2 个窗口:C++ 代码中的 QWindow(您称为“错误”窗口的窗口)与您的 QML 完全无关,以及 QML 中没有调整大小处理程序的 ApplicationWindow。您应该合并这两个窗口。我建议您基于类 QQuickView 进行以下重构这是一个带有集成 QML 引擎的窗口:

CMaze.h:

class CMaze: public QQuickView
{
public:
/* QQuickView already has a QML engine and a root object */
CMaze(/*QObject *root, QQmlApplicationEngine *engine*/);
private:
// QObject *root;
// QQmlApplicationEngine *engine;

/*+ Some other variables*/

protected: /* respect inherited scope */
/* use override to prevent misdeclaration*/
void resizeEvent(QResizeEvent *event) override;

};

CMaze.cpp:

CMaze::CMaze(/*QObject *root,QQmlApplicationEngine *engine*/)
{
//this->root = root; /* replaced by this->rootObject() */
//this->engine = engine; /* replaced by this->engine() */

/* + Some other functionality*/
}
void CMaze::resizeEvent(QResizeEvent *event)
{
qDebug() << "resize event entered";
}

主要.cpp:

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
// QQmlApplicationEngine engine;
// engine.load(QUrl(QLatin1String("qrc:/main.qml")));
// QObject *root = engine.rootObjects().first();
CMaze maze; //(root,&engine);
/* set QML source on maze */
maze.setSource(QUrl(QLatin1String("qrc:/main.qml")));
/* show the view */
maze.show();
return app.exec();
}

主.qml:

// you already have the window: just keep the rectangle
Rectangle{
id: background
objectName: "background"
anchors.fill: parent
color: "#ffffcc"
}

关于c++ - QT 中继承自 QWindow 的另一个类的 Resize 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693448/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com