Hi guys
I’m playing with QML/C++ integration and now I’m trying to set context property as described here http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-contextproperties.html
So my code looks like this
@
#include <QtGui/QGuiApplication>
#include «qtquick2applicationviewer.h»
#include <QDateTime>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.rootContext()->setContextProperty(QStringLiteral("somedata"), QDateTime::currentDateTime());
viewer.setMainQmlFile(QStringLiteral("qml/TestQML/main.qml"));
viewer.showExpanded();
return app.exec();
}@
I’ve just added line 10 to the project generated code
@viewer.rootContext()->setContextProperty(QStringLiteral(«somedata»), QDateTime::currentDateTime());@
But unfortunately I’ve got this error
../TestQML/main.cpp:10:25: error: invalid use of incomplete type ‘class QQmlContext’
In file included from ../Qt5.1.0/5.1.0/gcc/include/QtQuick/QQuickView:1:0,
from ../TestQML/qtquick2applicationviewer/qtquick2applicationviewer.h:14,
from ../TestQML/main.cpp:2:
../Qt5.1.0/5.1.0/gcc/include/QtQuick/qquickview.h:52:7: error: forward declaration of ‘class QQmlContext’
Maybe someone came across with that before?
EDIT I was basically able to get this to work, thanks in large part to /u/doctorfill456. However, in looking up how to do the next thing, I actually found a much easier way. It’s long enough that I’m going to make a separate post, which is here.
Original post follows:
This is driving me absoultely up the wall. I’ve been trying to piece together different parts of the documentation on this (because it never actually shows you the full thing, and keeps changing other parts when it shows you the next step).
So I’m trying to have a C++ class that can return a value that QML can then load.
Here’s the relevant bits of my C++ class:
class TextProcessor : public QObject {
Q_OBJECT
Q_PROPERTY(QString literalText READ literalText WRITE writeLiteralText NOTIFY literalTextChanged)
public:
/* snip */
QString literalText() const {
return tempString;
}
private:
QString tempString = "blah";
}
Then in main.cpp:
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
TextProcessor textProcessor;
QQuickView view;
view.engine()->rootContext()->setContextProperty("textProcessor", &textProcessor); // THIS LINE BREAKS
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
The documentation says this method should allow calling various methods of the class from within QML. However, the line I mentioned above breaks on trying to envoke setContextProperty(), with this error: member access into incomplete type 'QQmlContext'. This approach is word-for-word out of Qt’s documentation, and yet.
I had originally not been using a QQuickView at all. Instead doing an instance of QQmlApplicationEngine for loading the QML file, and using qmlRegisterType. This compiles, but nothing from the C++ class is accessible from the QML file (Unable to assign [undefined] to QString).
I assume I’m missing something obvious, but I am completely at a loss for what.
Вопрос:
Я пытаюсь интегрировать iAd в проект cocos2d-x, как описано в:
http://becomingindiedev.blogspot.com.es/2015/02/integrating-iad-in-cocos2d-x-v3x.html
AdBanner.h
#import <Foundation/Foundation.h>
#import <iAd/iAd.h>
@class RootViewController;
@interface AdBanner : NSObject<ADBannerViewDelegate>
{
UIWindow* window;
RootViewController* rootViewController;
ADBannerView* adBannerView;
bool adBannerViewIsVisible;
}
AdBanner.mm
@implementation AdBanner
-(id)init
{
if(self=[super init])
{
adBannerViewIsVisible = YES;
rootViewController =
(RootViewController*) [[[UIApplication sharedApplication] keyWindow] rootViewController];
window = [[UIApplication sharedApplication] keyWindow];
[self createAdBannerView];
}
return self;
}
-(void)layoutAnimated:(BOOL)animated
{
CGRect bannerFrame = adBannerView.frame;
//Has the banner an advestiment?
if ( adBannerView.bannerLoaded && adBannerViewIsVisible )
{
NSLog(@"Banner has advertisement");
bannerFrame.origin.y = window.bounds.size.height - bannerFrame.size.height;
} else
{
NSLog( @"Banner has NO advertisement" );
//if no advertisement loaded, move it offscreen
bannerFrame.origin.y = window.bounds.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
[rootViewController.view layoutIfNeeded]; //Member access into incomplete type "RootViewController"
adBannerView.frame = bannerFrame;
}];
}
@end
Линия внизу в AdBanner.mm дает ошибку:
[rootViewController.view layoutIfNeeded]; //Member access into incomplete type "RootViewController"
Как это разрешить?
Лучший ответ:
Вы объявили RootViewController как объявление прямого класса в файле .h с помощью директивы @Class, но вы не импортировали RootViewController.h в файл ADBanner.mm.
Это означает, что компилятор знает, что существует некоторый класс RootViewController, но больше не знает об этом – его суперкласс, методы или свойства. Таким образом, он не может подтвердить, что на самом деле имеет метод layoutIfNeeded.
Добавление #import "RootViewController.h" в начало ADBanner.mm предоставит компилятору необходимую ему информацию и устранит ошибку.