Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt Quick 2.0 App with Quick Controls 1.0 (can't set color property of Application Window)
Qt 6.11 is out! See what's new in the release blog

Qt Quick 2.0 App with Quick Controls 1.0 (can't set color property of Application Window)

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KrabQT
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KrabQT
        wrote on last edited by
        #3

        @import QtQuick 2.1
        import QtQuick.Controls 1.0

        ApplicationWindow {
        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"}
        }
        

        }@

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          Theoretically it should be right. Could you please tell me which viewer you're using?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KrabQT
            wrote on last edited by
            #5

            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>
            #endif

            QT_BEGIN_NAMESPACE

            #ifndef QT_NO_WIDGETS
            #define Application QApplication
            #else
            #define Application QGuiApplication
            #endif

            QT_END_NAMESPACE

            class QtQuick2ControlsApplicationViewer
            {
            public:
            explicit QtQuick2ControlsApplicationViewer();
            virtual ~QtQuick2ControlsApplicationViewer();

            void setMainQmlFile&#40;const QString &file&#41;;
            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));
            #endif

            if (!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();
            }
            @

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KrabQT
              wrote on last edited by
              #6

              ok i got it

              1. 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'
              2. i must call update after i changed the color (i thought it does automatically)
              1 Reply Last reply
              0
              • O Offline
                O Offline
                onek24
                wrote on last edited by
                #7

                Well actually it should update automatically. You could try to use a QQuickView instead of the qtquick2applicationviewer. Maybe it'll work with it.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved