Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] SetContextProperty();
Forum Updated to NodeBB v4.3 + New Features

[Solved] SetContextProperty();

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 8.4k 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.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #1

    @ #include "funzioni.h"
    #include <QDeclarativeContext>
    #include <QUrl>
    #include <QObject>
    #include <QGraphicsObject>
    #include <QDirectPainter>

    Funzioni::Funzioni()
    {
    view.setWindowFlags(Qt::FramelessWindowHint);
    view.rootContext()->setContextProperty("funzioni", this);
    view.setSource(QUrl::fromLocalFile("qml/LebendigeLieder/main.qml"));

    #if defined(Q_WS_SIMULATOR) || defined(Q_OS_SYMBIAN)
    view.setAttribute(Qt::WA_LockPortraitOrientation);
    view.showFullScreen();
    #else
    view.show();
    #endif
    }

    int Funzioni::getHeight()
    {
    return QDirectPainter::screenHeight();
    }

    int Funzioni::getWidth()
    {
    return QDirectPainter::screenWidth();
    }
    @

    what must i use instead of this "view.rootContext()->setContextProperty("funzioni", this);"? instead of "this"...

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Does Funzioni derive from QObject? If not, it has to.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on last edited by
        #3

        Can you say what error do you have?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          spode
          wrote on last edited by
          #4

          @ #include "funzioni.h"
          #include <QDirectPainter>
          #include <QDeclarativeContext>
          #include <QGraphicsObject>

          Funzioni::Funzioni(QObject *parent) :
          QObject(parent)
          {
          QDeclarativeView view;
          view.setWindowFlags(Qt::FramelessWindowHint);
          view.rootContext()->setContextProperty("funzioni", this);
          view.setSource(QUrl::fromLocalFile("qml/LebendigeLieder/main.qml"));

          #if defined(Q_WS_SIMULATOR) || defined(Q_OS_SYMBIAN)
          view.setAttribute(Qt::WA_LockPortraitOrientation);
          view.showFullScreen();
          #else
          view.show();
          #endif
          }

          int Funzioni::getHeight()
          {
          return QDirectPainter::screenHeight();
          }

          int Funzioni::getWidth()
          {
          return QDirectPainter::screenWidth();
          }
          @
          0k, now errors are:
          "error: undefined reference to _imp___ZN14QDirectPainter12screenHeightEv'" "error: undefined reference to _imp___ZN14QDirectPainter11screenWidthEv'"

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            Do you develop for embedded linux? Otherwise the QDirectPainter class won't be available to you (and the linker complains about that). If yes, re-run qmake.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              spode
              wrote on last edited by
              #6

              i am using windows 7. i used @ int Funzioni::getHeight()
              {
              QDesktopWidget *v = QApplication::desktop();
              return v->height();
              }

              int Funzioni::getWidth()
              {
              QDesktopWidget *c = QApplication::desktop();
              return c->width();
              }
              @
              but errors are: "TypeError: Result of expression 'funzioni.getHeight' [undefined] is not a function." if i call methods by qml:
              @
              import QtQuick 1.0

              Item
              {
              id: contenitore
              width: funzioni.getWidth();
              height: funzioni.getHeight();

              Image
              {
                  source: "../LebendigeLieder/sfondo.jpg"
                  anchors.top: contenitore.top
                  anchors.left: contenitore.left
                  id: sfondo
                  z: -1
              }
              
              Rectangle
              {
                  color:"red"
                  width: 130; height: 30
                  anchors.top: contenitore.top; anchors.right: contenitore.right
                  radius: 10
              }
              

              }
              @

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Methods called from QML have to be either declared as slots or as "Q_INVOKABLE":http://doc.qt.nokia.com/4.7/qobject.html#Q_INVOKABLE.

                What do you want to achieve? Display your application fullscreen?
                Just use "QDeclarativeView::setResizeMode(Qt::SizeRootObjectToView)":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeview.html#resizeMode-prop.

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  ZapB
                  wrote on last edited by
                  #8

                  Are those functions slots or marked as Q_INVOKABLE?

                  Nokia Certified Qt Specialist
                  Interested in hearing about Qt related work

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    spode
                    wrote on last edited by
                    #9

                    basically, my issue is that i would a fullscreen application, but the root conteiner is so small that it cannot contain the rettangle. in fact, it is viewed as white rettangle which fills the width and the total height of the screen... i noted the general container is too small seing the QML designer...

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      spode
                      wrote on last edited by
                      #10

                      solved with simple code:
                      @ import QtQuick 1.0

                      Rectangle {
                      color: "orange"
                      id: container

                      Rectangle
                      {
                          id: indicatore
                          anchors.right: container.right
                          width: 130; height: 60
                      
                      }
                      

                      }
                      @
                      and
                      @
                      #include <QtGui/QApplication>
                      #include "qmlapplicationviewer.h"

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

                      QmlApplicationViewer viewer;
                      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);
                      viewer.setWindowFlags(Qt::FramelessWindowHint);
                      viewer.setMainQmlFile&#40;QLatin1String("qml/LebendigeLieder/main.qml"&#41;);
                      viewer.showExpanded();
                      
                      return app.exec();
                      

                      }
                      @

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        Be aware that if you are using QDeclarativeView::showFullscreen() you will need to set the geometry manually, otherwise your view won't show up. In addition, the order is important:

                        • set the source
                        • set the resize mode
                        • set the geometry
                        • show

                        @
                        int main(int argc, char* argv[])
                        {
                        QApplication application(argc, argv);
                        QDeclarativeView view(QUrl("main.qml"));

                        view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
                        view.setGeometry(application.desktop()->screenGeometry());
                        view.showFullScreen();
                        
                        return(application.exec(&#41;&#41;;
                        

                        }
                        @
                        @
                        Rectangle {
                        color: "green"
                        }
                        @

                        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