Correctly creating a QProperty?
-
Okay so I'm trying to learn basic Qt. I created a C++ class "LoginScreen" to go into my QML game. I have only two Q_PROPERTY's in my class.
@
class LoginScreen : public QObject
{
Q_OBJECT
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)public:
explicit LoginScreen(QObject *parent = 0);
~LoginScreen();
QString username;
QString password;
void setUsername(QString &u);
void setPassword(QString &p);
bool passwordCheck();signals:
void usernameChanged();
void passwordChanged();private:
QString _username;
QString _password;
};
@@
LoginScreen::LoginScreen(QObject *parent) :
QObject(parent)
{
}LoginScreen::~LoginScreen()
{
}void LoginScreen::setUsername(QString &u)
{
_username=u;
emit usernameChanged();
}void LoginScreen::setPassword(QString &p)
{
if (p != _password) {
_password = p;
emit passwordChanged();
}
}
@I feel like this is correct, but the compiler is giving me this error:
C:\Users\User\Documents\loginForm\release\moc_loginscreen.cpp:142: error: C2064: term does not evaluate to a function taking 0 arguments.Here's the line it stops at (In moc_loginscreen):
@
switch (_id) {
case 0: reinterpret_cast< QString>(_v) = username(); break;
case 1: reinterpret_cast< QString>(_v) = password(); break;
}
@What am I doing wrong? Can I not have a public variable with the same name as the Q_Property I created? I thought that's how you access the variable... Any help?
Also, if anyone has any tips for basic code style/methods that are used in Qt that I'm not doing now.. all the examples I have are out of date and based on Qt 4.
-
The @READ username@ needs to be a function returning a QString. You only defined a member variable.
-
[quote author="EStudley" date="1385132569"]
What am I doing wrong? Can I not have a public variable with the same name as the Q_Property I created? I thought that's how you access the variable... Any help?[/quote]
Exactly you misunderstood this part. In the Q_PROPERTY macro you specify the property-name and it access methods. So instead of creating a public variable you should create a public getter method. Which returns the value of the variable you set in your setter method.[quote author="EStudley" date="1385132569"]
Also, if anyone has any tips for basic code style/methods that are used in Qt that I'm not doing now.. all the examples I have are out of date and based on Qt 4.[/quote]
Qt4 isn't out of date yet ;)
Actually the majority of it's parts/concepts are still applicable. -
Okay I had tried that before but I didn't know why it wasn't working. Two reasons:
-
I had forgotten to #include <QString>.
@
QString username;
QString password;
@
needed to be
@
QString username();
QString password();
@Thanks for the help! If anyone wants to give some basic advice or see anything I'm doing wrong, please still post.
-
-
it isn't simply working because the compiler expects to call username/password as methods, but actually you defined them as member-variables. This confuses the compiler and he quits with an error ;)
-
How can I now access this class from QML? I tried to do this in main.cpp:
@
qmlRegisterType<LoginScreen>("login", 1, 0, "LoginScreen");
@But it says incorrect identifier.. I want to be able to do something like this:
@
MouseArea{
anchors.fill: parent
onClicked{
loginScreen.setUsername(parent.text)
}
}
@ -
read the "docs":http://qt-project.org/doc/qt-5.0/qtqml/qqmlengine.html#qmlRegisterType....thats not it's purpose.
Read "this ":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.htmlfor what you want to achieve. -
I have, and I'm still having issues. Do I have to import from a library? What do I put for the URI if the class is sitting in the same directory? Something random?
I've follow that exactly and all I get is:
C:\Users\User\Documents\loginForm\main.cpp:11: error: C2065: 'qmlRegisterType' : undeclared identifier
C:\Users\User\Documents\loginForm\main.cpp:11: error: C2275: 'LoginScreen' : illegal use of this type as an expression
EDIT: After navigating to your second link I have more questions. I don't declare a QMLEngine... How am I supposed to declare a QML component in it?
-
This is the most basic main.cpp from creating a new QtQuick 2 project. I have yet to see any example of declaring a QML type with this.. all the example are seemingly out of date and use completely different way to initialize the QML application..
@
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"int main(int argc, char *argv[]){
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/loginForm/main.qml"));
viewer.showExpanded();
return app.exec();
}
@I know I have to #include "LoginScreen.h". I just have no idea how to set the rootContext of the QML engine from this..
-
QtQuick2ApplicationViewer derives from QQuickView. QQuickView provides a engine() method. There you set your object on.