Can't display custom c++ based Item
-
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.0Item {
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.0Rectangle {
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
-
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.
-
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+
-
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.
-
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.
-
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.