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. Can't display custom c++ based Item
Forum Updated to NodeBB v4.3 + New Features

Can't display custom c++ based Item

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.8k 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.
  • F Offline
    F Offline
    Florian77
    wrote on last edited by
    #1

    Hello, I can't display my custom c++ based Item. My code is the following:
    main.cpp:
    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "tile.h"
    #include <QtQml>
    #include <QQmlEngine>

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    qmlRegisterType <Tile>("TileDataLibrary", 1, 0, "Tile");
    QtQuick2ApplicationViewer viewer;
    // viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QStringLiteral("qml/testqml/main.qml"));
    viewer.showExpanded();

    return app.exec();
    

    }
    @
    tile.h:
    @#ifndef TILE_H
    #define TILE_H

    #include <QtCore>
    #include <QObject>
    #include <QQuickItem>

    class Tile : public QQuickItem
    {
    Q_OBJECT
    public :

     Q_PROPERTY(double tileOpacity READ tileOpacity WRITE setTileOpacity NOTIFY tileOpacityChanged)
     double tileOpacity()const{return m_tileOpacity;}
     void setTileOpacity(double newOpacity){if(newOpacity==m_tileOpacity)return;m_tileOpacity=newOpacity;emit tileOpacityChanged(); }
    
     Q_PROPERTY(QUrl tileImage READ tileImage WRITE setTileImage NOTIFY tileImageChanged)
     QUrl tileImage() const{return m_tileImage;}
     void setTileImage(const QUrl &newUrl){if(newUrl==m_tileImage)return;m_tileImage=newUrl;emit tileImageChanged();}
    
     Tile(){m_tileOpacity=0;m_tileImage="img/z.png";}
    

    signals:
    void imageChanged();
    void tileOpacityChanged();
    void tileImageChanged();

    private:
    double m_tileOpacity;
    QUrl m_tileImage;
    };
    #endif // TILE_H
    @
    Tile.qml:
    @import QtQuick 2.0
    import TileDataLibrary 1.0

    Item {
    id:tile
    width: 50
    height: 50

    // Instantiate Tile
    Tile {}
    
    Image {
        source: "img/z.png"
        width:50
        height:50
        opacity: 0.9
    }
    

    }
    @
    main.qml:
    @import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    Tile{z:2}
    Text {
    text: qsTr("Hello World")
    anchors.centerIn: parent
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    Qt.quit();
    }
    }
    }
    @
    My output is: a window with written hello world in the center. My png image isn't displayed (May I add that the path is correct: I checked)

    Thanks for your help

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You have not specified any painting for your item, so no wonder it does not draw.

      Also, you instantiate the tile, but you don't anchor it or set it's size.

      (Z(:^

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Florian77
        wrote on last edited by
        #3

        Ok I have anchored and set the size of my Tile, how do I specify a painting for my Item? I am confused, because this code worked with Qt Quick1, altough I never understood the code of Tile.qml. Why do I have to instanciate a Tile inside my Tile .qml file? It doesn't make any sense for me, because I thought my Tile Item wasn't fully defined only after the Tile.qml file is completed. I hope I am being clear on this second part, if not, a way to paint my Item would still be appreciated. thanks+

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You need to "reimplement this method":http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html#updatePaintNode to do custom painting. Or use QQuickPaintedItem.

          But in your case, I don't see any reason why you even have your Tile class: it does not do anything, Image element handles all display stuff... there is no need for your Tile.

          (Z(:^

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Florian77
            wrote on last edited by
            #5

            There is no need for it because what I posted is a simplified version of the code, which was written for you convenience. Okay thanks I will look into that.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You can take a look at my CCF project. It is rather large, but I do a lot of tricky QML stuff, including custom painting. It should work out of the box in Qt Creator. "Link to custom-painted class":https://github.com/sierdzio/closecombatfree/blob/master/src/qmlBase/ccfqmlbasemap.h.

              (Z(:^

              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