Skip to content

Commit 1e55ea3

Browse files
committed
Added message box for informative command line arguments for Windows
Added a message handler so any Qt message sent before the main window is visible will be shown to user in a message box. In principle, this is designed for the informative command line arguments: '-h' and '-v', but will improve also the error messages about not found arguments or any other warning or error shown before the messages can be seen in the Log dock. See issue #1069 # Conflicts: # src/main.cpp
1 parent 2447ed1 commit 1e55ea3

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎src/Application.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ Application::Application(int& argc, char** argv) :
154154
}
155155
}
156156

157+
if(m_dontShowMainWindow) {
158+
m_mainWindow = nullptr;
159+
return;
160+
}
161+
157162
// Show main window
158163
m_mainWindow = new MainWindow();
159164
m_mainWindow->show();
@@ -188,7 +193,8 @@ Application::Application(int& argc, char** argv) :
188193

189194
Application::~Application()
190195
{
191-
delete m_mainWindow;
196+
if(m_mainWindow)
197+
delete m_mainWindow;
192198
}
193199

194200
bool Application::event(QEvent* event)

‎src/main.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,60 @@
11
#include "Application.h"
2+
#include "sqlite.h"
3+
4+
#include <QMessageBox>
5+
6+
static QMessageBox* messageBox = nullptr;
7+
8+
void db4sMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
9+
{
10+
QByteArray localMsg = msg.toLocal8Bit();
11+
12+
// Emulate the default Qt treatment. This might not work in some OS, like Windows.
13+
fprintf(stderr, "%s\n", localMsg.constData());
14+
15+
const char *file = context.file ? context.file : "";
16+
const char *function = context.function ? context.function : "";
17+
localMsg = QString("%1 (%2, %3:%4)\n").arg(msg).arg(function).arg(file).arg(context.line).toLocal8Bit();
18+
19+
// Log using the SQLite log mechanism, so it gets the same treatment than messages
20+
// reported by SQLite itself. This will allow these messages to be seen in our Log window.
21+
// Error code will be the type
22+
sqlite3_log(static_cast<int>(type), localMsg.constData());
23+
}
24+
25+
void boxMessageOutput(QtMsgType, const QMessageLogContext &, const QString &msg)
26+
{
27+
if (!messageBox)
28+
messageBox = new QMessageBox();
29+
30+
messageBox->setText(messageBox->text() + msg + "\n");
31+
32+
if (!messageBox->isVisible())
33+
messageBox->show();
34+
}
235

336
int main( int argc, char ** argv )
437
{
38+
39+
#ifdef Q_OS_WIN
40+
// In Windows, there is no output to terminal for a graphical application, so we install
41+
// the output to message box, until the main window is shown or the application exits.
42+
qInstallMessageHandler(boxMessageOutput);
43+
#endif
44+
545
// Create application object. All the initialisation stuff happens in there
646
Application a(argc, argv);
747

48+
// If there has been output in the message box, let user see it.
49+
if(messageBox && messageBox->isVisible())
50+
messageBox->exec();
51+
852
// Quit application now if user doesn't want to see the UI
953
if(a.dontShowMainWindow())
1054
return 0;
1155

56+
qInstallMessageHandler(db4sMessageOutput);
57+
1258
// Run application
1359
return a.exec();
1460
}

0 commit comments

Comments
 (0)