Possible Typo in Popular "Learn Qt 5" Book by Nicholas Sherrif?
-
Hello,
I'm new to Qt and my c++ skills are intermediate student-level.
I'm working through Nicholas Sherrif's book, Learn Qt 5, and I have come to a spot where even the example code does not seem to compile properly.
My full repository can be found at this link here.
At this point, I have copy/pasted everything from the example code, Chapter 4, as can be found at this link here.
The bug is with a complicated bit of code that is above my skill level.
It shows up in the file
cm/cm-lib/source/controllers/command-controller.cpp
.The compiler error is as follows:
..<directory-listings>/command-controller.cpp:39: error: no matching function for call to ‘QQmlListProperty<cm::framework::Command>::QQmlListProperty(cm::controllers::CommandController*, QList<cm::framework::Command*>&)’ ../../cm-lib/source/controllers/command-controller.cpp: In member function ‘QQmlListProperty<cm::framework::Command> cm::controllers::CommandController::ui_createClientViewContextCommands()’: ../../cm-lib/source/controllers/command-controller.cpp:39:91: error: no matching function for call to ‘QQmlListProperty<cm::framework::Command>::QQmlListProperty(cm::controllers::CommandController*, QList<cm::framework::Command*>&)’ 39 | return QQmlListProperty<Command>(this, implementation->createClientViewContextCommands); | ^
Here is the full file content for
command-controller.cpp
:#include "command-controller.h" #include <QList> #include <QDebug> using namespace cm::framework; namespace cm { namespace controllers { class CommandController::Implementation { public: Implementation(CommandController* _commandController) : commandController(_commandController) { Command* createClientSaveCommand = new Command( commandController, QChar( 0xf0c7 ), "Save" ); QObject::connect( createClientSaveCommand, &Command::executed, commandController, &CommandController::onCreateClientSaveExecuted ); createClientViewContextCommands.append( createClientSaveCommand ); } CommandController* commandController{nullptr}; QList<Command*> createClientViewContextCommands{}; }; CommandController::CommandController(QObject* parent) : QObject(parent) { implementation.reset(new Implementation(this)); } CommandController::~CommandController() { } QQmlListProperty<Command> CommandController::ui_createClientViewContextCommands() { return QQmlListProperty<Command>(this, implementation->createClientViewContextCommands); } void CommandController::onCreateClientSaveExecuted() { qDebug() << "You executed the Save command!"; } }}
Based on what I am reading in the error output, it would seem that the function in use in for
QQmlListProperty...
is not properly declared, or even mentioned, in the header file.The header file is beyond my skill level.
Here is a copy of the header
command-controller.h
:#ifndef COMMANDCONTROLLER_H #define COMMANDCONTROLLER_H #include <QObject> #include <QtQml/QQmlListProperty> #include <cm-lib_global.h> #include <framework/command.h> namespace cm { namespace controllers { class CMLIBSHARED_EXPORT CommandController : public QObject { Q_OBJECT Q_PROPERTY(QQmlListProperty<cm::framework::Command> ui_createClientViewContextCommands READ ui_createClientViewContextCommands CONSTANT) public: explicit CommandController(QObject* _parent = nullptr); ~CommandController(); QQmlListProperty<framework::Command> ui_createClientViewContextCommands(); public slots: void onCreateClientSaveExecuted(); private: class Implementation; QScopedPointer<Implementation> implementation; }; }} #endif
I have found another post here on the Qt forums that seems to be dealing with the exact same line of code from the exact same book.
However, this person seems to understand the code more thoroughly, and I also get the impression that their specific issue was just a little bit different.
Can anyone help with this, please?
Thank you.
-
@bluesanta I guess you are using Qt 6?
There are some changes between Qt 5 and Qt 6. For example the constructor of QQmlListProperty does not take a reference any more:https://doc.qt.io/qt-6/qml-changes-qt6.html#removed-api
Changing the code to
QQmlListProperty<Command> CommandController::ui_createClientViewContextCommands() { return QQmlListProperty<Command>(this, &implementation->createClientViewContextCommands); }
should work.