Witam,
Napotkałem na problem podczas korzystania z QQuickWidget. Chce stworzyć widget w którym jest QPushButton oraz widget zaimplementowany w QML dodany przez QQuickWidget. Lecz w wyniku mojego programu otrzymuję widok jak w załączniku. Wygląda tak jakby implementacja w pliku QML nie została dodana do QQuickWidget. Nie mogę dotrzeć co jest tego powodem?
Mój wyświetlany widget:
#include "widget.h"
#include "ui_widget.h"
#include <QQmlContext>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->quickWidget->rootContext()->setContextProperty("mainWidget", this);
ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
}
Widget::~Widget()
{
delete ui;
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget mainWidget;
mainWidget.show();
return a.exec();
}
Implementacja w QML
import QtQuick 2.0
item {
width: 600
height: 600
component.onCompleted: {
myListView.model.append({ myColor: 'red', myText: 'raz' });
myListView.model.append({ myColor: 'blue', myText: 'dwa' });
myListView.model.append({ myColor: 'green', myText: 'trzy' });
myListView.model.append({ myColor: 'orange', myText: 'cztery' });
myListView.model.append({ myColor: 'brown', myText: 'piec' });
myListView.model.append({ myColor: 'black', myText: 'szesc' });
}
ListView {
id: myListView
anchors.fill: parent
model: ListModel {}
delegate: Rectangle{
width: parent.width
height: 50
color: myColor
Text {
text: MyText
}
}
}
}