博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qt下qml和c++交互信号槽机制测试
阅读量:3947 次
发布时间:2019-05-24

本文共 4268 字,大约阅读时间需要 14 分钟。

               

qt下qml和c++交互信号槽机制测试

本文博客链接:,作者:jdh,转载请注明.

环境:

主机:WIN7

开发环境:Qt5.2

说明:

写一个测试程序测试qml和c++利用信号槽机制传输信息.

测试程序功能:16进制和10进制互相转换.

源代码:

main.cpp

#include 
#include "qtquick2applicationviewer.h"#include
#include
#include
#include "myclass.h"int main(int argc, char *argv[]){    QGuiApplication app(argc, argv);    MyClass my;    QtQuick2ApplicationViewer viewer;    viewer.setMainQmlFile(QStringLiteral("qml/Hex2/main.qml"));    QQuickItem *rootObject = viewer.rootObject();    rootObject->setProperty("fileName", "jdh");    QObject::connect(rootObject,SIGNAL(btn_2dec_click(QString)),\                     &my,SLOT(slot_hex2dec(QString)));    QObject::connect(rootObject,SIGNAL(btn_2hex_click(QString)),\                     &my,SLOT(slot_dec2hex(QString)));    QObject::connect(&my,SIGNAL(sig_disp(QVariant)),\                     rootObject,SLOT(disp(QVariant)));    //viewer.showExpanded();    viewer.showFullScreen();    rootObject->setProperty("str_out","0x");    return app.exec();}
myclass.h

#ifndef MYCLASS_H#define MYCLASS_H#include 
#include
class MyClass : public QObject{    Q_OBJECTpublic:    explicit MyClass(QObject *parent = 0);signals:    void sig_disp(QVariant str_num);public slots:    void slot_hex2dec(QString str_num);    void slot_dec2hex(QString str_num);};#endif // MYCLASS_H
myclass.cpp

#include "myclass.h"#include 
#include "qtquick2applicationviewer.h"#include
#include
#include
MyClass::MyClass(QObject *parent) :    QObject(parent){}//十六进制转十进制void MyClass::slot_hex2dec(QString str_num){    qDebug() << "11" << str_num;    bool ok;    emit sig_disp("dec:" + QString::number(str_num.toInt(&ok,16),10));}//十进制转十六进制void MyClass::slot_dec2hex(QString str_num){    qDebug() << "22" << str_num;    bool ok;    emit sig_disp("hex:0x" + QString::number(str_num.toInt(&ok,10),16));}
main.qml

import QtQuick 2.0import QtQuick.Controls 1.1import QtQuick.Layouts 1.0Rectangle {    id : rect    //按键按下发射    signal btn_2dec_click(string str_num)    signal btn_2hex_click(string str_num)    width: 360    height: 360    ColumnLayout {        id: columnLayout1        x: 8        y: 15        width: 310        height: 324        Label {            id: label2            x: 83            y: -37            width: 178            height: 12            text: "十六进制和十进制转换器 by jdh"        }        RowLayout {            id: rowLayout1            x: 0            y: -216            width: 310            height: 50            spacing: 1            Label {                id: label1                x: 0                y: -199                width: 57                height: 39                text: "输入:"            }            TextField {                id: txt_in                x: 71                y: -204                width: 254                height: 39                placeholderText: qsTr("Text Field")            }        }        RowLayout {            id: rowLayout2            x: 0            y: -153            width: 310            height: 50            Label {                id: label3                x: 13                y: -151                width: 57                height: 39                text: "输出:"            }            TextField {                id: txt_out                x: 76                y: -151                width: 254                height: 39                placeholderText: qsTr("Text Field")            }        }        RowLayout {            id: rowLayout3            x: 0            y: -80            width: 310            height: 50            Button {                id: btn_2dec                x: 13                y: -75                width: 108                height: 44                text: "转十进制"                onClicked: btn_2dec_click(txt_in.text)            }            Button {                id: btn_2hex                x: 222                y: -75                width: 108                height: 44                text: "转十六进制"                onClicked: btn_2hex_click(txt_in.text)            }        }    }    function disp(str_num)    {        txt_out.text = str_num    }}
运行效果:

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!

你可能感兴趣的文章
Implementing Ancestral Navigation 实现原始导航
查看>>
Implementing Temporal Navigation 实现时间导航
查看>>
Responding to Touch Events 响应触摸事件
查看>>
Defining and Launching the Query 定义和启动查询
查看>>
Handling the Results 处理结果
查看>>
如何内置iperf到手机中
查看>>
如何adb shell进入ctia模式
查看>>
Contacts Provider 联系人存储
查看>>
android 图库播放幻灯片时灭屏再亮屏显示keyguard
查看>>
android 图库语言更新
查看>>
android camera拍照/录像后查看图片/视频并删除所有内容后自动回到camera预览界面
查看>>
android 图库中对非mp4格式的视频去掉"修剪"功能选项
查看>>
how to disable watchdog
查看>>
android SDIO error导致wifi无法打开或者连接热点异常的问题
查看>>
android USB如何修改Serial Number or SN?
查看>>
android 用svn管理的版本编译出来有问题
查看>>
android 如何用jar包代替java代码编译
查看>>
android 数据连接关闭的情况下如何让彩信发不出去
查看>>
android 编辑彩信,加入几页铃声,预览暂停界面,铃声名字不见了
查看>>
android 在新建短信时,加入名称为","(英文逗号)的联系人时,应用崩溃的修改
查看>>