5

I've just started to learn about Qt Mobile programming with Qt Quick 2.0 and I've been on google the whole day and its driving me crazy so here goes. I have got just your standard qt quick mobile app that qt makes for you. Here is all the files.

(I'm brand new to this so these might be noob qu's, sorry) Ok so here is my list of I Don't Knows:

  1. how on earth do I connect this class method to the button click in the qml file.
  2. Would it be possible to connect the same click and pass params through the click from the qml to the method and if so how would I do it?

  3. and also I was wondering if I could link the class to the qt quick like a regular QWidget class for instance:

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    SomeClass *sc = new SomeClass();
    
    QtQuick2ApplicationViewer viewer;
    
    // this I guess would be more like the Widget method but this i really dont know 
    // about, just a question I'm throwing out there
    viewer. (Link qt quick qml to object "sc"(Containing all my processes))
    
    viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml"));
    viewer.showExpanded();
    
    return app.exec();
    }
    

The main(.)cpp file

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

This is just the button element from my main.qml

Button {
        id: btn
        y: 443
        height: 39
        text: "Click Me"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 8
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.left: parent.left
        anchors.leftMargin: 15
    }

This is just some random class header :

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include <QObject>
#include <QDebug>>

class SomeClass : public QObject
{
    Q_OBJECT
public:
    explicit SomeClass(QObject *parent = 0);

signals:

public slots:
    void buttonClicked();
    void buttonClicked(QString &in);
};

#endif // SOMECLASS_H

Of course the cpp file :

#include "someclass.h"

SomeClass::SomeClass(QObject *parent) :
    QObject(parent)
{
}

void SomeClass::buttonClicked()
{
    // do something
}

void SomeClass::buttonClicked(QString &in)
{
    qDebug() << "Your string was : " << in;
}

I really appreciate all the help. Thank you.

TheMan68
  • 1,429
  • 6
  • 26
  • 48

1 Answers1

2

First, you need to export the SomeClass object to QtQuick (is that your third question?):

SomeClass sc;
viewer.rootContext()->setContextProperty(QStringLiteral("_someObject"), &sc);

This makes the sc object available in QtQuick, under the name "_someObject".

Then, in the QML use it like this:

Button {
    ....
    onClicked: {
        _someObject.buttonClicked(); //_someObject is the name used in setContextProperty.
    }
}

This assumes the Button has a signal clicked() that's emitted on click/touch. Not knowing which Button component you use, I can't check that.

To pass an argument, just do

onClicked: {
    _someObject.buttonClicked("Something");
}
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • i did it up to the viewer.rootContext() but after that i tried putting the -> to do the rest of the process and it wouldn't give me any options so im not sure what im doing wrong – TheMan68 Feb 01 '14 at 12:58
  • What do you mean with "didn't work at all"? Didn't compile, or didn't work at runtime? What do you mean with "options"? – Frank Osterfeld Feb 01 '14 at 14:30
  • well when you press . you get -> then list of methods/signals/propperties but i didn't get anything. I think it must be down to an include or something. – TheMan68 Feb 02 '14 at 09:36
  • That'd probably be QQmlContext. The compiler would have told you, if you just typed it instead of sitting and waiting for completion to work ;) – Frank Osterfeld Feb 02 '14 at 10:22
  • I did try ur method. For quite some time and I didn't get any helpful feed back from the compiler. I'm a beginner at Qt Quick but pretty well established in qt c++ and I've worked very hard to be able to do that. I've put in well over 3 to 40 hours of research over the past few days trying to figure out qt quick for mobile so I have to say I don't appreciate the sarcasm. The last thing I would do is sit on my ass and wait for my program to program it's self. I'm always very thankful of the help that you guys give and am very quick to thank anyone so please just friendly criticism . Thanks – TheMan68 Feb 03 '14 at 06:35
  • Sorry then - it just sounded like "Auto-completion doesn't work, I'm stuck", thus the snarky remark. If you provide more detail on where you're stuck ("didn't work at all" isn't very informative), people on SO might be able to help you. – Frank Osterfeld Feb 03 '14 at 08:43
  • thank you for your help. after including "QT += declarative" in .pro file and then QQmlContext with QtDeclerative in my main.cpp then everything worked – TheMan68 Mar 14 '14 at 06:24
  • sorry i was wrong. you only need to include QQmlContext and then everything works ok – TheMan68 Mar 14 '14 at 06:58