gpt4 book ai didi

c++ - 为什么延迟加载的 QWidget 会显示出来,而存储的 QWidget 却不会?

转载 作者:行者123 更新时间:2023-11-30 05:34:21 30 4
gpt4 key购买 nike

我有一组出现在不同地方的小弹出窗口小部件,但每次只出现一个。对于简单的功能,new-to-show 和 delete-to-hide 没问题,并且按预期工作,但当它们开始处理自己的数据时,我可以看到内存泄漏。

因为我只需要每种类型中的一个,所以我想我应该在父构造函数中预先创建所有这些,然后根据需要显示和隐藏它们。据我所知,这应该有效,但 popup->show() 没有显示。此示例所基于的复杂应用显示弹出窗口确实存在于正确的位置并且可以与用户交互...除了它是不可见的。

这是显示的惰性版本:

#ifndef MAIN_H
#define MAIN_H

#include <QtWidgets>

class Popup : public QLabel
{
Q_OBJECT
public:
explicit Popup(int x, int y, int width, int height, QWidget* parent = 0);
};

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
~MainWindow() {}
void mousePressEvent(QMouseEvent* ev);
private:
Popup* popup;
};

#endif // MAIN_H

/***************
*** main.cpp ***
***************/
#include "main.h"
#include <QApplication>

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}



MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
popup = 0;

QWidget* cWidget = new QWidget(this);
cWidget->setStyleSheet("background-color: lightgray");
setCentralWidget(cWidget);

showMaximized();
}

void MainWindow::mousePressEvent(QMouseEvent* ev)
{
if(popup != 0)
{
if(!popup->geometry().contains(ev->x(), ev->y()))
{
delete popup;
popup = 0;
}
}
else
{
popup = new Popup(ev->x(), ev->y(), 100, 100, this);
popup->show();
}
ev->accept();
}



Popup::Popup(int x, int y, int width, int height, QWidget* parent) :
QLabel(parent)
{
setStyleSheet("background-color: black");
setGeometry(
x - (width / 2), // Left
y - (height / 2), // Top
width , // Width
height // Height
);
}

这是未显示的预创建版本:

#ifndef MAIN_H
#define MAIN_H

#include <QtWidgets>

class Popup : public QLabel
{
Q_OBJECT
public:
explicit Popup(QWidget* parent = 0);
void setup(int x, int y, int width, int height);
};

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
~MainWindow() {}
void mousePressEvent(QMouseEvent* ev);
private:
Popup* popup;
};

#endif // MAIN_H

/***************
*** main.cpp ***
***************/
#include "main.h"
#include <QApplication>

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}



MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
popup = new Popup(this);

QWidget* cWidget = new QWidget(this);
cWidget->setStyleSheet("background-color: lightgray");
setCentralWidget(cWidget);

showMaximized();
}

void MainWindow::mousePressEvent(QMouseEvent* ev)
{
if(popup->isVisible())
{
if(!popup->geometry().contains(ev->x(), ev->y()))
{
popup->hide();
}
}
else
{
popup->setup(ev->x(), ev->y(), 100, 100);
popup->show();
}
ev->accept();
}



Popup::Popup(QWidget* parent) :
QLabel(parent)
{
setStyleSheet("background-color: black");
}

void Popup::setup(int x, int y, int width, int height)
{
setGeometry(
x - (width / 2), // Left
y - (height / 2), // Top
width , // Width
height // Height
);
}

我错过了什么?

最佳答案

您永远不会为您的popup 小部件设置窗口标志(例如Qt::Popup)。
它实际上是 MainWindoww 的子部件,应该显示在某处。

在 Qt 中,没有布局的 QWidget 只是简单地堆叠在一起。出现的 z 顺序取决于实例化的顺序。这就是为什么惰性 QLabel 可见而另一个不可见的原因。

实用上在 cWidget 之后实例化弹出窗口可能就足够了:

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
// popup = new Popup(this); // not here

QWidget* cWidget = new QWidget(this);
cWidget->setStyleSheet("background-color: lightgray");
setCentralWidget(cWidget);

popup = new Popup(this); // but here (untested, but should work)

showMaximized();
}

但是要以 Qt 方式执行此操作,您应该避免堆叠小部件,而是为您的 popup 提供真正的弹出式外观​​:

popup->setWindowFlags(Qt::Popup); // this lets popup have its own window

关于c++ - 为什么延迟加载的 QWidget 会显示出来,而存储的 QWidget 却不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280497/

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