Skip to content

Commit 69d9dfa

Browse files
committed
make code work in the 'lightly hacked' and 'project aware' qmlscene
1 parent 99105fb commit 69d9dfa

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

src/app/gui_tests.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ namespace
2323
constexpr char EXPECTED_FIRST_LOADED_FILE[] = "main.qml";
2424
} // namespace
2525

26-
GuiTests::GuiTests( const QQmlApplicationEngine& engine )
26+
GuiTests::GuiTests( QQmlEngine& engine )
2727
{
28-
connect( &engine, &QQmlApplicationEngine::objectCreated, [ = ]( QObject*, const QUrl& url ) {
28+
const QQmlApplicationEngine* appEngine
29+
= dynamic_cast<const QQmlApplicationEngine*>( &engine );
30+
FASSERT( appEngine, "not null. downcast must succeed." );
31+
32+
connect( appEngine, &QQmlApplicationEngine::objectCreated, [ = ]( QObject*, const QUrl& url ) {
2933
FASSERT( url.fileName() == QString( EXPECTED_FIRST_LOADED_FILE ), "something must have changed in loading behavior of QQmlApplicationEngine" );
3034

3135
// quit during next event-loop cycle

src/app/gui_tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GuiTests : public QObject
1717
{
1818
Q_OBJECT
1919
public:
20-
explicit GuiTests( const QQmlApplicationEngine& qmlapp );
20+
explicit GuiTests( QQmlEngine& qmlapp );
2121
~GuiTests();
2222

2323
GuiTests( const GuiTests& ) = delete;

src/app/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main( int argc, char* argv[] )
3939
// QQuickStyle::setStyle( "Fusion" ); // <-- call setStyle after creating app (if style is needed)
4040

4141
// ViewModels must OUTLIVE the qml engine, so create them first:
42-
project::ViewModelCollection vms( app );
42+
project::ViewModelCollection vms( app, false /*cliArgsOnlyParseThenSkipErrorHandling*/ );
4343

4444
// For antialiasing: https://stackoverflow.com/a/49576756/10278
4545
// QSurfaceFormat format;

src/app/view_model_collection.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ namespace project
2424
using str = std::string;
2525

2626
// clang-format off
27-
ViewModelCollection::ViewModelCollection( const QCoreApplication& app )
28-
: m_opts( std::make_unique<CliOptions>( app ) ),
27+
ViewModelCollection::ViewModelCollection( const QCoreApplication& app, bool cliArgsOnlyParseThenSkipErrorHandling )
28+
: m_opts( std::make_unique<CliOptions>( app, cliArgsOnlyParseThenSkipErrorHandling ) ),
2929
m_eventFilter( std::make_unique<EventFilter>() ),
3030
m_qmlLogger( std::make_unique<QmlMessageInterceptor>( !m_opts->MaximumQtLogging() ) ),
3131
m_logging( std::make_unique<LoggingTags>( *m_opts ) )
@@ -44,7 +44,7 @@ const CliOptions& ViewModelCollection::Options() const
4444
return *m_opts;
4545
}
4646

47-
void ViewModelCollection::ExportContextPropertiesToQml( QQmlApplicationEngine* engine )
47+
void ViewModelCollection::ExportContextPropertiesToQml( QQmlEngine* engine )
4848
{
4949
// Sort of a "silly" demo usage of project::Log<>
5050
Log( str( "rootContext" ), engine )->rootContext()->setContextProperty( "versionInfoBuildDateString", BUILD_ON_DATE );

src/app/view_model_collection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class QmlMessageInterceptor;
2525
class ViewModelCollection
2626
{
2727
public:
28-
explicit ViewModelCollection( const QCoreApplication& app );
28+
explicit ViewModelCollection( const QCoreApplication& app, bool cliArgsOnlyParseThenSkipErrorHandling );
2929
~ViewModelCollection();
3030

3131
ViewModelCollection( const ViewModelCollection& ) = delete;
3232
ViewModelCollection& operator=( const ViewModelCollection& ) = delete;
3333

34-
void ExportContextPropertiesToQml( QQmlApplicationEngine* engine );
34+
void ExportContextPropertiesToQml( QQmlEngine* engine );
3535

3636
void SetRootObject( QObject* object );
3737

src/lib_app/cli_options.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ constexpr char GUI_TEST_CLI_OPT[] = "guitest";
1515
constexpr char MAX_QT_LOGGING_CLI_OPT[] = "maxlogs";
1616
constexpr char COLOR_PALETTE_CLI_OPT[] = "colorpalette";
1717

18-
CliOptions::CliOptions( const QCoreApplication& app )
18+
CliOptions::CliOptions( const QCoreApplication& app, bool stopAtParseOnly )
1919
{
2020
QCommandLineParser parser;
2121
parser.addHelpOption();
@@ -27,7 +27,27 @@ CliOptions::CliOptions( const QCoreApplication& app )
2727
{ { "c", COLOR_PALETTE_CLI_OPT }, "show color palette to allow for on-the-fly color experiments" },
2828
} );
2929

30-
parser.process( app );
30+
if( !stopAtParseOnly )
31+
{
32+
// The NOT-stop case is the full desktop application.
33+
// This call to 'process' gives us parsing and type-checking and error
34+
// reporting. (In other words, everything you would want in a full app.)
35+
parser.process( app );
36+
}
37+
else
38+
{
39+
// The 'stopAtParseOnly' case is the case when this instance of
40+
// CliOptions is part of our 'lightly hacked' and 'project aware'
41+
// locally-built qmlscene.
42+
// We MUST NOT CALL QCommandLineParser::process IN THIS CASE, because
43+
// it will prevent us from using the arguments that qmlscene accepts in
44+
// its own 'main' function.
45+
// By calling 'parse' instead, we DO achieve the desired capture of
46+
// our app-specific options. As a trade-off, we forgo the built-in
47+
// type-checking and error reporting of QCommandLineParser::process
48+
const QStringList arguments = QCoreApplication::arguments();
49+
parser.parse( arguments );
50+
}
3151

3252
m_guiTests = parser.isSet( GUI_TEST_CLI_OPT );
3353
m_maximumQtLogs = parser.isSet( MAX_QT_LOGGING_CLI_OPT );

src/lib_app/cli_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace project
1515
class CliOptions
1616
{
1717
public:
18-
explicit CliOptions( const QCoreApplication& app );
18+
explicit CliOptions( const QCoreApplication& app, bool stopAtParseOnly );
1919
~CliOptions();
2020

2121
CliOptions( const CliOptions& ) = delete;

0 commit comments

Comments
 (0)