Qt Quick 2.0 App with Quick Controls 1.0 (can't set color property of Application Window)
-
I am really frustrated why it doesn't work. I just created Quick 2.0 app with quick controls in latest qt sdk for mac and modified qml code a bit (add one MouseArea and set clicked handler to set parent.color). When i click inside that app window, it outputs that parent does not have color property (??????? in doc ApplicationWindow inherits from Window and it has color property). If i am tried just consolo.log that color property, it gaves me undefined.
PLEASE what i am doing wrong? I need work, thx. -
Hello and welcome to qt-project.org,
could you please provide us your QML-code so i can see what's going on in there?
-
@import QtQuick 2.1
import QtQuick.Controls 1.0ApplicationWindow {
title: qsTr("Hello World")
width: 1400
height: 800
color: "black"menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("Exit") onTriggered: Qt.quit(); } } } Rectangle { width:200 height:200 color: "#414141" id: kik } MouseArea { anchors.fill: parent onClicked: {parent.color = "black"} }
}@
-
Theoretically it should be right. Could you please tell me which viewer you're using?
-
I am using just what was generated from newset Qt designer.
main.cpp
@#include "qtquick2controlsapplicationviewer.h"
int main(int argc, char *argv[])
{
Application app(argc, argv);QtQuick2ControlsApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/asfasf/main.qml")); viewer.show(); return app.exec();
}@
qtquick2controlsapplicationviewer.h
@#ifndef QTQUICK2APPLICATIONVIEWER_H
#define QTQUICK2APPLICATIONVIEWER_H#ifndef QT_NO_WIDGETS
#include <QApplication>
#else
#include <QGuiApplication>
#endifQT_BEGIN_NAMESPACE
#ifndef QT_NO_WIDGETS
#define Application QApplication
#else
#define Application QGuiApplication
#endifQT_END_NAMESPACE
class QtQuick2ControlsApplicationViewer
{
public:
explicit QtQuick2ControlsApplicationViewer();
virtual ~QtQuick2ControlsApplicationViewer();void setMainQmlFile(const QString &file); void addImportPath(const QString &path); void show();
private:
class QtQuick2ApplicationViewerPrivate *d;
};#endif // QTQUICK2APPLICATIONVIEWER_H
@qtquick2controlsapplicationviewer.cpp
@#include "qtquick2controlsapplicationviewer.h"
#include <QCoreApplication>
#include <QDir>
#include <QQmlComponent>
#include <QQmlEngine>
#include <QQuickView>class QtQuick2ApplicationViewerPrivate
{
QString mainQmlFile;
QQmlEngine engine;
QQuickWindow *window;QtQuick2ApplicationViewerPrivate() : window(0) {} ~QtQuick2ApplicationViewerPrivate() { delete window; } static QString adjustPath(const QString &path); friend class QtQuick2ControlsApplicationViewer;
};
QString QtQuick2ApplicationViewerPrivate::adjustPath(const QString &path)
{
#if defined(Q_OS_IOS)
if (!QDir::isAbsolutePath(path))
return QString::fromLatin1("%1/%2")
.arg(QCoreApplication::applicationDirPath(), path);
#elif defined(Q_OS_MAC)
if (!QDir::isAbsolutePath(path))
return QStringLiteral("%1/../Resources/%2")
.arg(QCoreApplication::applicationDirPath(), path);
#elif defined(Q_OS_BLACKBERRY)
if (!QDir::isAbsolutePath(path))
return QStringLiteral("app/native/%1").arg(path);
#elif !defined(Q_OS_ANDROID)
QString pathInInstallDir =
QStringLiteral("%1/../%2").arg(QCoreApplication::applicationDirPath(), path);
if (QFileInfo(pathInInstallDir).exists())
return pathInInstallDir;
pathInInstallDir =
QStringLiteral("%1/%2").arg(QCoreApplication::applicationDirPath(), path);
if (QFileInfo(pathInInstallDir).exists())
return pathInInstallDir;
#endif
return path;
}QtQuick2ControlsApplicationViewer::QtQuick2ControlsApplicationViewer()
: d(new QtQuick2ApplicationViewerPrivate())
{}
QtQuick2ControlsApplicationViewer::~QtQuick2ControlsApplicationViewer()
{
delete d;
}void QtQuick2ControlsApplicationViewer::setMainQmlFile(const QString &file)
{
d->mainQmlFile = QtQuick2ApplicationViewerPrivate::adjustPath(file);QQmlComponent component(&d->engine); QObject::connect(&d->engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
#ifdef Q_OS_ANDROID
component.loadUrl(QUrl(QStringLiteral("assets:/")+d->mainQmlFile));
#else
component.loadUrl(QUrl::fromLocalFile(d->mainQmlFile));
#endifif (!component.isReady()) qWarning("%s", qPrintable(component.errorString())); d->window = qobject_cast<QQuickWindow *>(component.create()); if (!d->window) qFatal("Error: Your root item has to be a Window."); d->engine.setIncubationController(d->window->incubationController());
}
void QtQuick2ControlsApplicationViewer::addImportPath(const QString &path)
{
d->engine.addImportPath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
}void QtQuick2ControlsApplicationViewer::show()
{
if (d->window)
d->window->show();
}
@ -
ok i got it
- i don't now why, but parent of mouse area isn't that application window, because if i specify on application window id, then parent != 'id value'
- i must call update after i changed the color (i thought it does automatically)
-
Well actually it should update automatically. You could try to use a QQuickView instead of the qtquick2applicationviewer. Maybe it'll work with it.