在QML语言中怎么定义signal并怎么正确使用它

在QML语言中怎么定义signal并怎么正确使用它,第1张

使用Ubuntu SDK来打开我们已经创建好的应用。然后再打开文件“MyLightqml”。在文件的开始部分加入如下的语句:

[cpp] view plain copy

Item {

<strong>id: root</strong>

width: unitsgu(100)

height: unitsgu(75)

signal redLightOn

signal greenLightOn

signal yellowLightOn

Rectangle {

id: background

anchorsfill: parent

color: "black"

property int size: width07

我们可以看到我们已经定义了三个信号。它们分别是"redLightOn", "greenLightOn"及“yellowLightOn”。我们定义这三个信号的目的是为了当红色,**及绿色的灯亮的时候,我们用这些信号来发送一个通知。这样只要有兴趣的代码可以对它进行截获,并处理。这里必须注意的是信号的第一个字母为小写!

接下来,我们在我们的JS代码中来发送这些信号。代码如下:

[cpp] view plain copy

Timer {

interval: 1000

running: true

repeat: true

property int count: 0

onTriggered: {

if (parentstate == "red_on" && count >= 5)

{

parentstate = "red_yellow_on"

<strong> rootredLightOn();

rootyellowLightOn();</strong>

count = 0

}

else if ( parentstate == "red_yellow_on" )

{

parentstate = "green_on"

<strong>rootgreenLightOn();</strong>

count++

}

else if ( parentstate == "green_on" && count >= 5 )

{

parentstate = "yellow_on"

<strong> rootyellowLightOn();</strong>

count ++

}

else if ( parentstate == "yellow_on" ) {

parentstate = "red_on"

redLightOn();

count = 0

}

else {

count++

}

}

}

发送信号其实非常简单。直接发送,就像调用一个方法一样。

为了在我们的代码部分截取这个应用,我们可以使用如下的方法。在“TrafficLightqml”中,修改代码为:

[cpp] view plain copy

import QtQuick 20

import UbuntuComponents 01

import "ui"

MainView {

// objectName for functional testing purposes (autopilot-qt5)

objectName: "mainView"

// Note! applicationName needs to match the "name" field of the click manifest

applicationName: "comubuntudeveloperliu-xiao-guoTrafficLight"

/

This property enables the application to change orientation

when the device is rotated The default is false

/

//automaticOrientation: true

width: unitsgu(120)

height: unitsgu(80)

Page {

id:main

anchorsfill: parent

Row {

id: myrow

anchorshorizontalCenter: parenthorizontalCenter

anchorsverticalCenter: parentverticalCenter

spacing: unitsgu(5)

MyLight {

id:light

width: mainwidth/5

height: width3

onRedLightOn: {

consolelog("red light is on")

}

}

Connections {

target: light

onYellowLightOn: {

consolelog("yellow light is on")

}

}

}

function greenLightOn() {

consolelog("green light is on")

}

ComponentonCompleted: {

lightgreenLightOnconnect(greenLightOn);

}

}

}

为了说明清楚,我写了三种方法。一种是直接把信号的第一个字母变为大写, 并同时在前面加上"on“。第二种方法使用”Connections"来实现槽的连接。第三种方法,我们可以直接 把信号连接到一个JS的函数上。运行程序,我们可以在应用的输出窗口看到如下的输出:

[cpp] view plain copy

green light is on

yellow light is on

red light is on

red light is on

yellow light is on

green light is on

yellow light is on

red light is on

red light is on

yellow light is on

事实上所有的控件的property都可以发出一个信号。让我们来看一下我们先前完成的“color” property。

[cpp] view plain copy

void TrafficLight::setColor(const QColor &color)

{

if ( color == m_color )

return;

else {

m_color = color;

update(); // repaint with the new color

emit colorChanged();

}

}

从这里可以看出,每当property的值发生改变时,就会发生一个叫做“colorChanged”的信号。我们可以在我们的QML中截获这个信号。比如在我们的代码中,我们可以使用如下的代码:

[cpp] view plain copy

TrafficLight{

id: redlight

width: backgroundsize

height: backgroundsize

color:"red"

onColorChanged: {

consolelog("Color is changed to " + color);

}

}

当我们运行时,我们可以看到好多的颜色变化的事件。这是由于颜色在transition时发生很多的颜色的变化。同样我们也可以对任何一个property进行事件的捕获。比如:

[cpp] view plain copy

TrafficLight{

id: redlight

width: backgroundsize

height: backgroundsize

color:"red"

onColorChanged: {

consolelog("Color is changed to " + color);

}

onWidthChanged: {

consolelog("width is changed to " + width);

}

}

这段代码可以对"width"的变化进行捕获!

你说的profile name - data source - user id--password

是数据源里的参数

acc<->odbc<->pb

有这三种

方式一:

SQLCADBMS = "ODBC"

SQLCAAutoCommit = False

SQLCADBParm = "ConnectString='driver=Microsoft Access Driver (mdb);DBQ=c:\foodmartmdb'"

CONNECT;

IF SQLCASQLCode <> 0 THEN

MessageBox("数据库连接失败","请与管理员联系。错误号:" + String(SQLCASQLCode) + "~r~n错误原因:" + SQLCASQLErrText)

RETURN

else

MessageBox("数据库连接成功","现在进入系统 ")

end if

方式二:

SQLCADBMS = "OLE DB"

SQLCAAutoCommit = False

SQLCADBParm = "PROVIDER='MicrosoftJetOLEDB40',DATASOURCE='C:\foodmartmdb'"

CONNECT;

方式三:

建立DSN,通过ODBC连接。这个就不用多说了。

例如:

// Profile my_ass

SQLCADBMS = "ODBC"

SQLCAAutoCommit = False

SQLCADBParm = "ConnectString='DSN=my_ass'"

今天终于自己静态编译过了QT531, 成功用在项目上了, 记录下configure指令

configure -confirm-license -opensource -platform win32-msvc2013 -mp -debug-and-release -static -prefix "E:\Qt\531-static-vs2013" -qt-sql-sqlite -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -no-qml-debug -no-angle -nomake tests -nomake examples -skip qtwebkit

其中 -mp 是启用多核编译的开关

以上就是关于在QML语言中怎么定义signal并怎么正确使用它全部的内容,包括:在QML语言中怎么定义signal并怎么正确使用它、pb 如何连接ACCESS、QT5静态编译无法加载数据库插件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10177238.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存