Public Functions qt++ to qml



  • I 'm trying to get values ​​of public functions
    for example. I want to know the number of screens connected with "screenCount ()"

    if (1 == screenCount()){celular.show();};
    

    or if I want the position of the mouse in QML
    viewer.rootContext()->setContextProperty("anchoSalida", QMouseEvent->globalX());

    all these examples fail. as it should really be written ?


  • Moderators

    @Dante-Leoncini

    you mean something like the following?

    class MyFunctionsProvider : QObject
    {
        Q_OBJECT
    
    public:
         MyFunctionsProvider ( QObject* parent = 0 ) : QObject(parent)
         {
         }
    
         Q_INVOKABLE int GetScreenCount() const
         {
                 QList<QScreen *> screens = QGuiApplication::screens();
                return screen.count();
         }
    
         Q_INVOKABLE QPoint GlobalCursorPos() const
         {
                return QCursor::pos();
         }
    };
    
    ...
    
    viewer.rootContext()->setContextProperty("MyFunctions", new MyFunctionsProvider(viewer.rootContext()) );
    

    In QML you can do then:

    MyFunctions.GetScreenCount()
    MyFunctions.GlobalCursorPos()
    


  • @raven-worx

    thanks for the reply. For the width and height of the screen to solve it so:

    viewer.rootContext()->setContextProperty("altoSalida", WidgetEscritorio->screenGeometry(1).height());
        viewer.rootContext()->setContextProperty("anchoSalida", WidgetEscritorio->screenGeometry(1).width());
    

    but when I wanted to do the same with the position of the mouse did not work .
    Try to use your code to get the coordinates :

    class MyFunctionsProvider : QObject
    {
        Q_OBJECT
    
    public:
         MyFunctionsProvider ( QObject* parent = 0 ) : QObject(parent)
         {
         }
    
         Q_INVOKABLE QPoint GlobalCursorPos() const
         {
                return QCursor::pos();
         }
    };
    
    viewer.rootContext()->setContextProperty("MyFunctions", new MyFunctionsProvider(viewer.rootContext()));
    

    But when I try to compile get the error: 'QObject' is an inaccessible base of 'MyFunctionsProvider'

    I 'm looking for because it is. I lack even practiced in qt c ++
    (translated with google)

    edit:
    missing the word "public" in class MyFunctionsProvider : public QObject
    but now comes another error: undefined reference to `vtable for MyFunctionsProvider'


  • Moderators

    @Dante-Leoncini said in Public Functions qt++ to qml:

    missing the word "public" in class MyFunctionsProvider : public QObject

    yes, i missed hat.

    but now comes another error: undefined reference to `vtable for MyFunctionsProvider'

    strange. Is the code you've posted really all you are using? Or did you stip something out?
    Try a clean rebuilt maybe.



  • @Dante-Leoncini said in Public Functions qt++ to qml:

    but now comes another error: undefined reference to `vtable for MyFunctionsProvider'

    Hi,
    you need to run qmake.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.