gpt4 book ai didi

C++ Qt QCloseEvent 不起作用

转载 作者:太空狗 更新时间:2023-10-29 21:12:53 25 4
gpt4 key购买 nike

我想创建一条消息,当用户按下退出按钮时,该消息会出现在新窗口中。为此,我创建了一个 QCloseEvent,但 MainWindow 实际上根本没有关闭。我究竟做错了什么?

主窗口.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
//Constructor
explicit MainWindow(QWidget *parent = 0);

//Destructor
~MainWindow();

public slots:
void closeMainWindow();

private:
QCloseEvent *event;

主窗口.cpp

void MainWindow::closeMainWindow(){

event = new QCloseEvent();
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close the Window"),
tr("Do you want to close the window?"),
QMessageBox::Yes | QMessageBox::No);

if(answer == QMessageBox::Yes){
event->accept();
}
else
event->ignore();
}

最佳答案

您必须实现 closeEvent主窗口的功能!

例如

void MyMainWindow::closeEvent(QCloseEvent *event)
{
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close the Window"),
tr("Do you want to close the window?"),
QMessageBox::Yes | QMessageBox::No);

if(answer == QMessageBox::Yes){
event->accept();
}
else
event->ignore();
}

关于C++ Qt QCloseEvent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45933864/

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