我有一个带有启动画面的应用程序,我使用线程睡眠方法保持了 2 秒。在我的 MianWindow 中,我有一个对象,它侦听 sslErrors 信号,然后调用一个显示 ssl 错误的插槽。在初始屏幕关闭之前,我不希望显示错误的对话框。此应用程序还必须能够从网络文件夹运行。
在我的本地机器上,它每次都按照我想要的方式运行,但在网络驱动器上,有时会在显示初始屏幕时打开 SSL 错误对话框,因此当我单击对话框上的“否”按钮(用于忽略错误)时,我的应用程序崩溃:S
主文件
#include "mainwindow.h"
#include "single_application.h"
#include <QtCore>
#include <QApplication>
#include <QSplashScreen>
#include <QStringList>
#include <QMessageBox>
const QString CSM_VERSION = QString("v1.0i");
class MyInitThread : public QThread
{
protected:
void run(void)
{
this->sleep(2);
}
};
int main(int argc, char *argv[]) try
{
bool my_global_bool = true;
SingleApplication a(argc, argv, "CSM_CYTO_VIEWER_RUN_ONCE");
a.setProperty("my_global_bool", my_global_bool);
if (a.isRunning())
{
QMessageBox::information(0, "Cyto Viewer " + CSM_VERSION, "Cyto Viewer is already running.");
return 0;
}
//QApplication a(argc, argv);
QApplication::setWindowIcon(QIcon(":/logo/csm.ico"));
QApplication::setApplicationName(QString("Cyto Viewer"));
QApplication::setApplicationVersion(QString(CSM_VERSION));
//Check if system has a system tray
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray "
"on this system."));
return 1;
}
Q_ASSERT( QSslSocket::supportsSsl() );
MainWindow w;
QPixmap pixmap(":/splash/splash.png");
QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
w.setWindowOpacity(0);
w.setWindowTitle("Cyto Viewer " + CSM_VERSION);
splash.show();
QEventLoop loop;
MyInitThread *thread = new MyInitThread();
QObject::connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(thread, SIGNAL(finished()), &splash, SLOT(close()));
QObject::connect(thread, SIGNAL(finished()), &w, SLOT(slInit()));
thread->start();
loop.exec();
w.show();
return a.exec();
}
主窗口.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(view->page()->networkAccessManager(), SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )),
this, SLOT(onSslErrors(QNetworkReply*, const QList<QSslError> & )));
}
SSL 错误槽
void MainWindow::onSslErrors(QNetworkReply* reply, const QList<QSslError> &errors)
{
if(SSL_ONCE != true){
QString errorString;
bool firstError = false;
//TODO: Get this displaying cirtificate errors
foreach (const QSslError e, errors)
{
if(firstError != true) {
if (!errorString.isEmpty())
errorString += "";
if(e.error() != e.NoError){
errorString += "<ul style=\"background: url(minus-small.png);\"><li style=\"background: url(minus-small.png);\">" + e.errorString() + "</li></ul>";
}
}
}
//Delay MessageBox until the splash screen closes (2 seconds)
//delay(2.5);
QEventLoop loop;
MainWinThread *thread = new MainWinThread();
connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
//Detect any SSL errors from the network reply
thread->start();
loop.exec(); //Do event processing until the thread has finished!
QMessageBox sslQuestion;
//Fix for QMessageBox width
QSpacerItem horizontalSpacer(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
sslQuestion.setText("Cyto Viewer " + CSM_VERSION);
QGridLayout* layout = (QGridLayout*)sslQuestion.layout();
sslQuestion.setInformativeText("The following SSL error(s) occured: " + errorString + "Do you wish to continue?");
sslQuestion.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
sslQuestion.setIconPixmap(QPixmap(":/logo/lock-ssl.png"));
sslQuestion.setDefaultButton(QMessageBox::No);
sslQuestion.setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
sslQuestion.setMinimumWidth(800);
layout->addItem(&horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
int SSLChoice = sslQuestion.exec();
if (SSLChoice == QMessageBox::No) {
reply->abort();
delete systray;
exit(EXIT_FAILURE);
//qApp->quit();
//return;
} else {
reply->ignoreSslErrors(/*errors*/);
SSL_ONCE = true;
}
} else {
reply->ignoreSslErrors();
return;
}
}
我的 slinit 插槽(用于设置系统托盘/隐藏主窗口)
void MainWindow::slInit()
{
setWindowOpacity(1);
//Set min size, this can be changed through registry
this->setMinimumSize(800, 640);
//System tray
aboutAction = new QAction("About", this);
quitAction = new QAction("Exit", this);
connect (aboutAction, SIGNAL(triggered()), this, SLOT(about()));
connect (quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(aboutAction);
//trayIconMenu->addAction(printAction);
trayIconMenu->addAction(quitAction);
systray = new QSystemTrayIcon(this);
connect(systray, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
QIcon icon(":/logo/csm.ico");
systray->setIcon(icon);
systray->setContextMenu (trayIconMenu);
systray->show();
if(systray->isVisible() /*&& !csmURL.isEmpty()*/){;
QString cdate = QDate::currentDate().toString("yyyy");
trayMsg = "<qt>Cyto Viewer "+CSM_VERSION+" is a browser produced by Perceptive Instruments© " + cdate + " specifically for Cyto Study Manager</b>.</qt>";
}
}