qml与c++的简单实例,Connections信号连接
2017-07-13 21:42
316 查看
头文件如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QQuickWidget>
#include <QQmlContext>
class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(int _width READ returnW NOTIFY rectChanged)
Q_PROPERTY(int _height READ returnH NOTIFY rectChanged)
public:
Widget(QWidget *parent = 0);
~Widget();
int returnW(){return _width = width();}
int returnH(){return _height = height();}
void resizeEvent(QResizeEvent *event);
signals:
void rectChanged();
private:
int _width, _height;
QQuickWidget *qmlWidget;
};
#endif // WIDGET_HWidget类源文件
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qmlWidget = new QQuickWidget(this);
qmlWidget->rootContext()->setContextProperty("View",this);
qmlWidget->setSource(QUrl(QStringLiteral("qrc:/Connections.qml")));
resize(640,450);
}
Widget::~Widget()
{
if(qmlWidget != NULL)
{
delete qmlWidget;
qmlWidget = NULL;
}
}
void Widget::resizeEvent(QResizeEvent *event)
{
emit rectChanged();
}qml文件,记得要把qml文件放到资源文件里面去哦
import QtQuick 2.0
import QtQuick.Controls 1.4
Rectangle {
width: View._width
height: View._height
color: "green"
Text {
id: text1
text: qsTr("text one")
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 30
color: "blue"
font.pixelSize: 18
}
Text {
id: text2
text: qsTr("text two")
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: text1.bottom
anchors.topMargin: 50;
color: "yellow"
font.pixelSize: 18
}
Button {
id: btn
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: text2.bottom
anchors.topMargin: 50
text: "Change"
}
Connections {
target: btn
onClicked: {
text1.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1);
text2.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1);
}
// //类似的如果想用Widget里面的信号如下:
// target: View
// onRectChanged: test
}
}
相关文章推荐
- 从qtcreator的实例来简单分析qml的信号与响应函数
- 一个C++连接sql的简单实例
- 如何在C++代码中连接QML代码中的信号
- C++多线程编程简单实例
- [置顶] C++连接CTP接口实现简单量化交易(行情、交易、k线、策略)
- Android JNI简单实例(android 调用C/C++代码)
- c++ 转换与类类型简单实例
- C++发邮件简单实例详解
- C++基于TCP/IP简单的客户端、服务器通信程序实例
- C++实现统计代码运行时间计时器的简单实例
- C++连接CTP接口实现简单量化交易(行情、交易、k线、策略)
- C++简单文件操作实例
- PHP字符串的连接的简单实例
- linux下c/c++实例之十一简单的计算器程序
- C++多线程编程简单实例
- 设计模式---单件(C++版) 一个简单实例(避免用户忘记delete单件实例)
- 最简单的0-1背包问题c++代码实例及运行结果
- 数据结构之栈的简单实例c++实现
- PHP连接局域网MYSQL数据库的简单实例
- Linux下C++调用Java简单实例(jni)