为什么 Qt 中的 close 方法不能正常工作?
Why the close method in Qt does not work correctly?
我正在用 Qt 编写代码。我与赛普拉斯 FX2LP 进行了 USB 通信。当设备未通过 USB 电缆连接时,我应该显示一个带有 OK-Abort 选项的对话框。当用户单击确定时,我重新检查 USB 连接。它运作良好。但是当用户点击中止时,我应该完全关闭程序。中止选项不起作用并重新显示 OK-中止对话框。我的代码有什么问题?
这是我的代码的一部分(Main.cpp/Mainwindow/My_Receive_Data_Thread(我的 USB 通信线程)):
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QTimer>
#include <QIcon>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true);
a.processEvents();
MainWindow w;
w.setAttribute(Qt::WA_QuitOnClose);
w.show();
return a.exec();
}
My_Receive_Data_Thread:
My_Receive_Data_Thread::~My_drawing_object(){
}
void My_Receive_Data_Thread::Send_command_packet(){
CCyUSBDevice USBDevice;// CCyUSBDevice recognize only Cypress Devices
short int numDevices = USBDevice.DeviceCount();
if(numDevices==0)
emit show_message("warning","USB Device not connected!");
}
主窗口:
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow){
ui->setupUi(this);
My_Receive_Data_Thread_1= new My_Receive_Data_Thread(this);
connect(My_Receive_Data_Thread_1,SIGNAL(show_message(QString,QString)),this,SLOT(show_message_box(QString,QString)),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
connect(this,SIGNAL(Send_command_packet()),My_Receive_Data_Thread_1,SLOT(Send_command_packet()),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::show_message_box(QString title,QString text){
QMessageBox::StandardButton reply;
reply=QMessageBox::question(this,title,text,QMessageBox::Abort|QMessageBox::Ok);
if(reply==QMessageBox::Abort)
close();
}
Send_command_packet()
可能被调用了多次,所以它发出了很多 show_message()
都在排队。
他们中的第一个将呼叫排队到 close()
,但还有其他 show_message()
需要处理。
在MainWindow
中添加一个bool m_closed
,关闭时设置为true
,并在show_show_message_box()
中勾选。
void MainWindow::show_message_box(QString title,QString text){
if (!m_closing){
QMessageBox::StandardButton reply;
reply=QMessageBox::question(this,title,text,QMessageBox::Abort|QMessageBox::Ok);
if(reply==QMessageBox::Abort){
m_closing = true;
close();
}
}
}
我正在用 Qt 编写代码。我与赛普拉斯 FX2LP 进行了 USB 通信。当设备未通过 USB 电缆连接时,我应该显示一个带有 OK-Abort 选项的对话框。当用户单击确定时,我重新检查 USB 连接。它运作良好。但是当用户点击中止时,我应该完全关闭程序。中止选项不起作用并重新显示 OK-中止对话框。我的代码有什么问题? 这是我的代码的一部分(Main.cpp/Mainwindow/My_Receive_Data_Thread(我的 USB 通信线程)):
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QTimer>
#include <QIcon>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true);
a.processEvents();
MainWindow w;
w.setAttribute(Qt::WA_QuitOnClose);
w.show();
return a.exec();
}
My_Receive_Data_Thread:
My_Receive_Data_Thread::~My_drawing_object(){
}
void My_Receive_Data_Thread::Send_command_packet(){
CCyUSBDevice USBDevice;// CCyUSBDevice recognize only Cypress Devices
short int numDevices = USBDevice.DeviceCount();
if(numDevices==0)
emit show_message("warning","USB Device not connected!");
}
主窗口:
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow){
ui->setupUi(this);
My_Receive_Data_Thread_1= new My_Receive_Data_Thread(this);
connect(My_Receive_Data_Thread_1,SIGNAL(show_message(QString,QString)),this,SLOT(show_message_box(QString,QString)),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
connect(this,SIGNAL(Send_command_packet()),My_Receive_Data_Thread_1,SLOT(Send_command_packet()),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::show_message_box(QString title,QString text){
QMessageBox::StandardButton reply;
reply=QMessageBox::question(this,title,text,QMessageBox::Abort|QMessageBox::Ok);
if(reply==QMessageBox::Abort)
close();
}
Send_command_packet()
可能被调用了多次,所以它发出了很多 show_message()
都在排队。
他们中的第一个将呼叫排队到 close()
,但还有其他 show_message()
需要处理。
在MainWindow
中添加一个bool m_closed
,关闭时设置为true
,并在show_show_message_box()
中勾选。
void MainWindow::show_message_box(QString title,QString text){
if (!m_closing){
QMessageBox::StandardButton reply;
reply=QMessageBox::question(this,title,text,QMessageBox::Abort|QMessageBox::Ok);
if(reply==QMessageBox::Abort){
m_closing = true;
close();
}
}
}