Qt 5.10 Book Examples
MyWidget.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MyWidget.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include <QQuickWidget>
13 #include <QQmlContext>
14 
15 #include "MyWidget.h"
16 
17 // ----------------------------------------------------------------------
18 MyWidget::MyWidget(QWidget* pwgt/*=0*/) : QWidget(pwgt)
19 {
20  QQuickWidget* pv = new QQuickWidget(QUrl("qrc:/main.qml"));
21 
22  QVBoxLayout* pvbx = new QVBoxLayout;
23  pvbx->addWidget(pv);
24  setLayout(pvbx);
25 
26  QQmlContext* pcon = pv->rootContext();
27  QStringList lst;
28  for (int i = 0; i < 100; ++i) {
29  lst << "Item" + QString::number(i);
30  }
31  QStringListModel* pmodel = new QStringListModel(this);
32  pmodel->setStringList(lst);
33 
34  pcon->setContextProperty("myModel", pmodel);
35  pcon->setContextProperty("myText", "It's my text!");
36  pcon->setContextProperty("myColor", QColor(Qt::yellow));
37  pcon->setContextProperty("myWidget", this);
38 }
39 
40 // ----------------------------------------------------------------------
42 {
43  QMessageBox::information(0, "Message", "It's my message");
44 }
void slotDisplayDialog()
Definition: MyWidget.cpp:41
MyWidget(QWidget *pwgt=0)
Definition: main.cpp:35