Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correctly creating a QProperty?
Qt 6.11 is out! See what's new in the release blog

Correctly creating a QProperty?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EStudley
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      The @READ username@ needs to be a function returning a QString. You only defined a member variable.

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        [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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EStudley
          wrote on last edited by
          #4

          Okay I had tried that before but I didn't know why it wasn't working. Two reasons:

          1. 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.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            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 ;)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • E Offline
              E Offline
              EStudley
              wrote on last edited by
              #6

              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)
              }
              }
              @

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  EStudley
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    EStudley
                    wrote on last edited by
                    #9

                    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..

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      QtQuick2ApplicationViewer derives from QQuickView. QQuickView provides a engine() method. There you set your object on.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved