June 27, 2011

spode spode
Ant Farmer
317 posts

[Solved] SetContextProperty();

 

  1.  #include "funzioni.h"
  2. #include <QDeclarativeContext>
  3. #include <QUrl>
  4. #include <QObject>
  5. #include <QGraphicsObject>
  6. #include <QDirectPainter>
  7.  
  8. Funzioni::Funzioni()
  9. {    
  10.     view.setWindowFlags(Qt::FramelessWindowHint);
  11.     view.rootContext()->setContextProperty("funzioni", this);
  12.     view.setSource(QUrl::fromLocalFile("qml/LebendigeLieder/main.qml"));
  13.  
  14. #if defined(Q_WS_SIMULATOR) || defined(Q_OS_SYMBIAN)
  15.     view.setAttribute(Qt::WA_LockPortraitOrientation);
  16.     view.showFullScreen();
  17. #else
  18.     view.show();
  19. #endif
  20. }
  21.  
  22. int Funzioni::getHeight()
  23. {
  24.     return QDirectPainter::screenHeight();
  25. }
  26.  
  27. int Funzioni::getWidth()
  28. {
  29.     return QDirectPainter::screenWidth();
  30. }

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

10 replies

June 27, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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

June 27, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Can you say what error do you have?

June 27, 2011

spode spode
Ant Farmer
317 posts

  1.  #include "funzioni.h"
  2. #include <QDirectPainter>
  3. #include <QDeclarativeContext>
  4. #include <QGraphicsObject>
  5.  
  6. Funzioni::Funzioni(QObject *parent) :
  7.     QObject(parent)
  8. {
  9.     QDeclarativeView view;
  10.     view.setWindowFlags(Qt::FramelessWindowHint);
  11.     view.rootContext()->setContextProperty("funzioni", this);
  12.     view.setSource(QUrl::fromLocalFile("qml/LebendigeLieder/main.qml"));
  13.  
  14. #if defined(Q_WS_SIMULATOR) || defined(Q_OS_SYMBIAN)
  15.     view.setAttribute(Qt::WA_LockPortraitOrientation);
  16.     view.showFullScreen();
  17. #else
  18.     view.show();
  19. #endif
  20. }
  21.  
  22. int Funzioni::getHeight()
  23. {
  24.     return QDirectPainter::screenHeight();
  25. }
  26.  
  27. int Funzioni::getWidth()
  28. {
  29.     return QDirectPainter::screenWidth();
  30. }

0k, now errors are:
“error: undefined reference to `_imp___ZN14QDirectPainter12screenHeightEv’”
“error: undefined reference to `_imp___ZN14QDirectPainter11screenWidthEv’”

June 27, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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.

June 27, 2011

spode spode
Ant Farmer
317 posts

i am using windows 7. i used

  1.  int Funzioni::getHeight()
  2. {
  3.     QDesktopWidget *v = QApplication::desktop();
  4.     return v->height();
  5. }
  6.  
  7. int Funzioni::getWidth()
  8. {
  9.     QDesktopWidget *c = QApplication::desktop();
  10.     return c->width();
  11. }

but errors are: “TypeError: Result of expression ‘funzioni.getHeight’ [undefined] is not a function.” if i call methods by qml:
  1. import QtQuick 1.0
  2.  
  3. Item
  4. {
  5.     id: contenitore
  6.     width: funzioni.getWidth();
  7.     height: funzioni.getHeight();
  8.  
  9.     Image
  10.     {
  11.         source: "../LebendigeLieder/sfondo.jpg"
  12.         anchors.top: contenitore.top
  13.         anchors.left: contenitore.left
  14.         id: sfondo
  15.         z: -1
  16.     }
  17.  
  18.     Rectangle
  19.     {
  20.         color:"red"
  21.         width: 130; height: 30
  22.         anchors.top: contenitore.top; anchors.right: contenitore.right
  23.         radius: 10
  24.     }
  25. }

June 27, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Methods called from QML have to be either declared as slots or as Q_INVOKABLE [doc.qt.nokia.com].

What do you want to achieve? Display your application fullscreen?
Just use QDeclarativeView::setResizeMode [doc.qt.nokia.com].

June 27, 2011

ZapB ZapB
Robot Herder
1356 posts

Are those functions slots or marked as Q_INVOKABLE?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

June 27, 2011

spode spode
Ant Farmer
317 posts

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…

June 27, 2011

spode spode
Ant Farmer
317 posts

solved with simple code:

  1.  import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     color: "orange"
  5.     id: container
  6.  
  7.     Rectangle
  8.     {
  9.         id: indicatore
  10.         anchors.right: container.right
  11.         width: 130; height: 60
  12.  
  13.     }
  14. }

and
  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication app(argc, argv);
  7.  
  8.     QmlApplicationViewer viewer;
  9.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);
  10.     viewer.setWindowFlags(Qt::FramelessWindowHint);
  11.     viewer.setMainQmlFile(QLatin1String("qml/LebendigeLieder/main.qml"));
  12.     viewer.showExpanded();
  13.  
  14.     return app.exec();
  15. }

June 27, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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

  1. int main(int argc, char* argv[])
  2. {
  3.     QApplication application(argc, argv);
  4.     QDeclarativeView view(QUrl("main.qml"));
  5.  
  6.     view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
  7.     view.setGeometry(application.desktop()->screenGeometry());
  8.     view.showFullScreen();
  9.  
  10.     return(application.exec());
  11. }

  1. Rectangle {
  2.     color: "green"
  3. }

 
  ‹‹ [Solved] Symbian debug: graphics error      [Solved] Particular radius ››

You must log in to post a reply. Not a member yet? Register here!