1.使用静态变量,更新窗口关闭状态,.h中声明
static bool m_bClosed;
2.静态变量初始化,需要放在类外(注意)
//标记当前状态
bool Widget::m_bClosed = false;
3.构造函数中,每次更新窗口打开标识
//标记窗口打开
m_bClosed = false;
//窗口销毁时更新窗口关闭标记
connect(this, &QWidget::destroyed, [=]()
{
m_bClosed = true;
qDebug() << "destroyed";
});
4.//窗口销毁时更新窗口关闭标记
//窗口销毁时更新窗口关闭标记
connect(this, &QWidget::destroyed, [=]()
{
m_bClosed = true;
qDebug() << "destroyed";
});
//点击关闭时窗口销毁
this->setAttribute(Qt::WA_DeleteOnClose);
5.定义函数
//判断窗口是否打开
bool CCodeCheckSetor::IsOpen()
{
if(Widget::m_bClosed)
{
m_pWidget = nullptr;
}
return (m_pWidget != nullptr) ? true : false;
}
//打开窗口
bool CCodeCheckSetor::Open()
{
//如果窗体已关闭,初始化(避免窗体在最小化时,用户非正常关闭
if(Widget::m_bClosed)
{
m_pWidget = nullptr;
}
if(m_pWidget == nullptr)
{
m_pWidget = new Widget();
}
if(m_pWidget != nullptr && !Widget::m_bClosed)
{
m_pWidget->show();
}
return true;
}
6.引用
//防止过快点击和多次启动
if (g_pCodeSeter == nullptr && !g_bCodeStarting)
{
g_bCodeStarting = true; //避免多次启动
HMODULE mod = LoadLibrary(_T("DHCodeCheck.dll"));
DWORD err = GetLastError();
if (mod) {
//获取函数地址
g_GetSeter = (GetSeter)GetProcAddress(mod, "GetSeter");
if (g_GetSeter)
{
//实例化
g_pCodeSeter = g_GetSeter();
if (g_pCodeSeter->IsOpen())
{
g_pCodeSeter->Show();
}
else
{
//启动窗口
//StartCodeCheckDlg startDlg = nullptr;
g_pStartCodeCheckDlg = (StartCodeCheckDlg)GetProcAddress(mod, "StartCodeCheckDlg");
g_pStartCodeCheckDlg(g_pCodeSeter);
}
}
}
g_bCodeStarting = false;
}
else if (g_pCodeSeter && !g_bCodeStarting)
{
if (g_pCodeSeter->IsOpen())
{
g_pCodeSeter->Show();
}
else //
{
//启动窗口
g_pStartCodeCheckDlg(g_pCodeSeter);
}
}