当前位置: 首页 > news >正文

qt笔记之qml下拉标签组合框增加发送按钮发送标签内容

qt笔记之qml下拉标签组合框增加发送按钮发送标签内容

code review!

文章目录

  • qt笔记之qml下拉标签组合框增加发送按钮发送标签内容
    • 1.运行
    • 2.文件结构
    • 3.main.qml
    • 4.main.cc
    • 5.MyClass.h
    • 6.MyClass.cc
    • 7.CMakeLists.txt
    • 8.ComboBox.pro
    • 9.qml.qrc

1.运行

请添加图片描述

2.文件结构

在这里插入图片描述

3.main.qml

在这里插入图片描述

代码

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2Window {visible: truewidth: 640height: 480title: qsTr("QML ComboBox C++ signal")property string selectedOptionComboBox {id: comboBoxx: 12y: 58width: 147height: 25model: ["选项一", "选项二", "选项三"]Component.onCompleted: {selectedOption = "选项一"}onCurrentIndexChanged: {selectedOption = comboBox.currentText;}}Button {id: buttonx: 18y: 134text: qsTr("发送")onClicked: {myClass_rename.setValue(selectedOption);}}
}

4.main.cc

在这里插入图片描述

代码

#include "MyClass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>Q_DECLARE_METATYPE(MyClass *)int main(int argc, char *argv[]) {QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QGuiApplication app(argc, argv);QQmlApplicationEngine engine;MyClass myClass;engine.rootContext()->setContextProperty("myClass_rename", &myClass);engine.load(QUrl(QStringLiteral("qrc:/main.qml")));if (engine.rootObjects().isEmpty())return -1;return app.exec();
}

5.MyClass.h

在这里插入图片描述

代码

#ifndef MYCLASS_H
#define MYCLASS_H#include <QObject>
#include <QString>
#include <QVector>
#include <iomanip>
#include <iostream>class MyClass : public QObject {Q_OBJECTpublic:explicit MyClass(QObject *parent = nullptr);public slots:void setValue(QString value);
};#endif // MYCLASS_H

6.MyClass.cc

在这里插入图片描述

代码

#include "MyClass.h"
#include <QDebug>MyClass::MyClass(QObject *parent) : QObject(parent) {}void MyClass::setValue(QString value) {qDebug() << "value= " << value;
}

7.CMakeLists.txt

在这里插入图片描述

代码

cmake_minimum_required(VERSION 2.8.12)project(ComboBox LANGUAGES CXX)
set(Qt5_DIR "/home/user/Qt/5.15.2/gcc_64/lib/cmake/Qt5")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)find_package(Qt5 COMPONENTS Core Quick REQUIRED)include_directories(
#   "src""*.h"
)file (GLOB SRCS
#   "src/cc/*.cc""*.cc"
)file (GLOB INCS
#   "src/inc/*.h""*.h"
)add_executable(${PROJECT_NAME} ${SRCS} ${INCS} "main.cc" "qml.qrc")target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick -pthread)

8.ComboBox.pro

代码

QT += quick qml
CONFIG += c++11INCLUDEPATH += ./LIBS += -L/usr/lib/x86_64-linux-gnu -lz
LIBS += /usr/lib/x86_64-linux-gnu/libboost_*
#LIBS += /usr/lib/aarch64-linux-gnu/libboost_*# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \./main.ccRESOURCES += ./qml.qrc \
# image.qrc# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH # Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

9.qml.qrc

代码

<RCC><qresource prefix="/"><file>main.qml</file></qresource>
</RCC>
http://www.lryc.cn/news/195288.html

相关文章:

  • linux上构建任意版本的rocketmq多架构x86 arm镜像——筑梦之路
  • Java8 新特性之Stream(五)-- Stream的3种创建方法
  • Vue实现模糊查询搜索功能
  • (C++ STL) 详解vector模拟实现
  • c语言从入门到实战——C语言数据类型和变量
  • [论文精读]Semi-Supervised Classification with Graph Convolutional Networks
  • CICD:使用docker+ jenkins + gitlab搭建cicd服务
  • 新能源电池试验中准确模拟高空环境大气压力的解决方案
  • Python 中的模糊字符串匹配
  • 记录一个奇怪bug
  • SpringBoot面试题7:SpringBoot支持什么前端模板?
  • leetcode做题笔记172. 阶乘后的零
  • linux之shell脚本练习
  • CSS阶详细解析一
  • osWorkflow-1——osWorkflow官方例子部署启动运行(版本:OSWorkflow-2.8.0)
  • Stm32_标准库_13_串口蓝牙模块_手机与蓝牙模块通信
  • Unity中用序列化和反序列化来保存游戏进度
  • Junit 单元测试之错误和异常处理
  • LockSupport-park和unpark编码实战
  • js深拷贝与浅拷贝
  • Docker-harbor私有仓库部署与管理
  • ArcGIS笔记8_测量得到的距离单位不是米?一经度一纬度换算为多少米?
  • SpringBoot入门详解
  • 数据分析案例-基于snownlp模型的MatePad11产品用户评论情感分析(文末送书)
  • Leetcode刷题解析——904. 水果成篮
  • Spring Boot RESTful API
  • k8s day04
  • ESP32-IPS彩屏ST7789-Arduino-简单驱动
  • 高效工具类软件使用
  • 批处理文件(.bat)中,dir与tree命令的效果