qml与c++的简单实例,Connections信号连接
来源:互联网 发布:行知基地实践活动作文 编辑:程序博客网 时间:2024/06/04 01:16
头文件如下:
#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
}
}
阅读全文
0 0
- qml与c++的简单实例,Connections信号连接
- 从qtcreator的实例来简单分析qml的信号与响应函数
- Qt-QML-Connections,接受组件信号
- qml学习------------信号和槽的连接
- QT信号与槽的简单实例
- 简单的Qt连接信号与槽
- C建立socket连接的简单实例
- PyQt4的信号与插槽中简单实例源码讲解
- QML与C++中信号和槽的使用
- 信号与槽的连接
- Qml中信号的发送与接收信号槽函数的使用
- [QML] Connections元素介绍
- QML类型说明-Connections
- [QML] Connections元素介绍
- Connections QML Type
- 简单的一个信号槽的连接
- 简单的C语言socket实例(远程连接)
- 单例模式的简单使用(QML调用C++)
- Codeforces #803E: Roma and Pokers 题解
- Struts2中访问Servlet中的API(ActionContext,ServletActionContext)两种方式
- 线段树求和
- 《GPU高性能编程 CUDA实战》(CUDA By Example)读书笔记
- 友元函数
- qml与c++的简单实例,Connections信号连接
- python爬虫
- JVM内存泄露与内存溢出的区别
- c++字符串读入写入小结
- SQL 语句(三)--------------数据查询(连接查询)
- 猪和回文
- pandas常用函数
- C++奇技淫巧(一):取位数,交换,比较,求均值
- 什么是IOC