Last active
November 11, 2015 13:41
-
-
Save kafeg/5fb3cbb2378fc577a9bb to your computer and use it in GitHub Desktop.
Qt Quick QML AdMob Android iOS Realisation: http://forsk.ru (based on https://github.com/yevgeniy-logachev/QtAdMob)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "admobctl.h" | |
#include "QtAdMob/QtAdMobBanner.h" | |
#include "QtAdMob/QtAdMobInterstitial.h" | |
#include <QtQml> | |
#include <QDebug> | |
#include <QTimer> | |
AdMobCtl::AdMobCtl(QObject *parent) : QObject(parent) | |
{ | |
m_Banner = CreateQtAdMobBanner(); | |
m_Banner->Initialize(); | |
m_Banner->SetSize(IQtAdMobBanner::Banner); | |
m_Interstitial = CreateQtAdMobInterstitial(); | |
showedTimer = new QTimer(this); | |
connect(showedTimer, SIGNAL(timeout()), this, SLOT(showedTimerSlot())); | |
showedTimer->start(500); | |
} | |
AdMobCtl::~AdMobCtl() | |
{ | |
delete m_Banner; | |
delete m_Interstitial; | |
} | |
void AdMobCtl::showedTimerSlot() | |
{ | |
//qDebug() << "showedTimerSlot" << m_Banner->IsShow(); | |
if (cachedBannerHeight != bannerHeight()) { | |
cachedBannerHeight = bannerHeight(); | |
emit bannerHeightChanged(bannerHeight()); | |
} | |
if (cachedBannerWidth != bannerWidth()) { | |
cachedBannerWidth = bannerWidth(); | |
emit bannerWidthChanged(bannerWidth()); | |
} | |
if (m_Banner->IsShow() && (showedTimer->interval() == 500)) { | |
emit showed(); | |
showedTimer->stop(); | |
showedTimer->start(2000); | |
} | |
} | |
void AdMobCtl::setBannerVisible(const bool visible) | |
{ | |
//qDebug() << "setBannerVisible" << visible; | |
m_bannerVisible = visible; | |
if (m_bannerVisible) { | |
m_Banner->Show(); | |
} else { | |
m_Banner->Hide(); | |
} | |
} | |
bool AdMobCtl::bannerVisible() const | |
{ | |
return m_bannerVisible; | |
} | |
void AdMobCtl::setBannerPosition(const QPoint position) | |
{ | |
//qDebug() << "new position" << position; | |
m_position = position; | |
//for example: QPoint position((width() - m_Banner->GetSizeInPixels().width()) * 0.5f, 0.0f); | |
m_Banner->SetPosition(m_position); | |
} | |
QPoint AdMobCtl::bannerPosition() const | |
{ | |
return m_position; | |
} | |
int AdMobCtl::bannerWidth() const | |
{ | |
return m_Banner->GetSizeInPixels().width(); | |
} | |
int AdMobCtl::bannerHeight() const | |
{ | |
return m_Banner->GetSizeInPixels().height(); | |
} | |
void AdMobCtl::setUnitId(const QString &unitId) | |
{ | |
//qDebug() << "set unitId" << unitId << m_bannerVisible << m_interstitialEnabled; | |
if (!m_initialized) { | |
m_unitId = unitId; | |
m_Banner->SetUnitId(m_unitId); | |
m_Banner->SetPosition(m_position); | |
//m_Banner->AddTestDevice("514ED2E95AD8EECE454CC5565326160A"); | |
//if (m_bannerVisible) { | |
m_Banner->Show(); | |
//} | |
m_Interstitial->LoadWithUnitId(m_unitId); | |
if (m_interstitialEnabled) { | |
//m_Interstitial->AddTestDevice("514ED2E95AD8EECE454CC5565326160A"); | |
m_Interstitial->Show(); | |
} | |
m_initialized = true; | |
} else { | |
qDebug() << "AdMob alredy initialized!"; | |
} | |
} | |
bool AdMobCtl::interstitialEnabled() const | |
{ | |
return m_interstitialEnabled; | |
} | |
void AdMobCtl::setInterstitialEnabled(bool interstitialEnabled) | |
{ | |
//qDebug() << "set m_interstitialEnabled" << interstitialEnabled; | |
m_interstitialEnabled = interstitialEnabled; | |
if (m_interstitialEnabled) { | |
m_Interstitial->Show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ADMOBBANNERCTL_H | |
#define ADMOBBANNERCTL_H | |
#include <QObject> | |
#include <QPoint> | |
class IQtAdMobBanner; | |
class IQtAdMobInterstitial; | |
class QTimer; | |
class AdMobCtl : public QObject | |
{ | |
Q_OBJECT | |
Q_PROPERTY(bool bannerVisible READ bannerVisible WRITE setBannerVisible) | |
Q_PROPERTY(bool interstitialEnabled READ interstitialEnabled WRITE setInterstitialEnabled) | |
Q_PROPERTY(QPoint bannerPosition READ bannerPosition WRITE setBannerPosition) | |
Q_PROPERTY(int bannerHeight READ bannerHeight NOTIFY bannerHeightChanged) | |
Q_PROPERTY(int bannerWidth READ bannerWidth NOTIFY bannerHeightChanged) | |
Q_PROPERTY(QString unitId WRITE setUnitId) | |
public: | |
explicit AdMobCtl(QObject *parent = 0); | |
~AdMobCtl(); | |
signals: | |
void showed(); | |
void bannerHeightChanged(int height); | |
void bannerWidthChanged(int width); | |
public slots: | |
void showedTimerSlot(); | |
void setBannerVisible(const bool bannerVisible); | |
bool bannerVisible() const; | |
bool interstitialEnabled() const; | |
void setInterstitialEnabled(bool interstitialEnabled); | |
void setBannerPosition(const QPoint bannerPosition); | |
QPoint bannerPosition() const; | |
int bannerHeight() const; | |
int bannerWidth() const; | |
void setUnitId(const QString &unitId); | |
private: | |
IQtAdMobBanner *m_Banner; | |
IQtAdMobInterstitial *m_Interstitial; | |
bool m_bannerVisible, m_interstitialEnabled; | |
QPoint m_position; | |
QString m_unitId; | |
bool m_initialized = false; | |
QTimer *showedTimer; | |
int cachedBannerHeight, cachedBannerWidth; | |
}; | |
#endif // ADMOBBANNERCTL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QQmlApplicationEngine> | |
#include <admobctl.h> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
//AdMob http://forsk.ru | |
qmlRegisterType<AdMobCtl>("ru.forsk.admobctl", 1, 0, "AdMobCtl"); | |
QQmlApplicationEngine engine; | |
QString productType = QSysInfo::productType(); | |
if (productType == "windows") { | |
engine.load(QUrl(QStringLiteral("../darkstories/qml/main.qml"))); | |
} else { | |
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); | |
} | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.4 | |
import QtQuick.Window 2.2 | |
import ru.forsk.admobctl 1.0 | |
ApplicationWindow { | |
id: appWindow | |
width: 480 | |
height: 800 | |
visible: true | |
visibility: (Qt.platform.os == "android" || Qt.platform.os == "ios") ? Window.FullScreen : Window.Windowed | |
AdMobCtl { | |
id: adMobCtl | |
interstitialEnabled: true | |
bannerVisible: true | |
unitId: "ca-app-pub-7212327600726803/6079324485" | |
bannerPosition: Qt.point((appWindow.width - adMobCtl.bannerWidth) * 0.5, 0) | |
onShowed: { | |
console.log("change banner position!"); | |
bannerPosition = Qt.point((appWindow.width - adMobCtl.bannerWidth) * 0.5, | |
(appWindow.height - adMobCtl.bannerHeight * 1.5)) | |
} | |
} | |
Rectangle { | |
id: root | |
anchors.fill: parent | |
anchors.bottomMargin: adMobCtl.bannerHeight | |
color: "white" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment