[Solved] SetContextProperty();
-
@ #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"...
-
Can you say what error do you have?
-
@ #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'" -
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.0Item
{
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 }
}
@ -
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. -
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...
-
solved with simple code:
@ import QtQuick 1.0Rectangle {
color: "orange"
id: containerRectangle { 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(QLatin1String("qml/LebendigeLieder/main.qml")); viewer.showExpanded(); return app.exec();
}
@ -
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());
}
@
@
Rectangle {
color: "green"
}
@