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

news/2025/2/11 22:13:32/

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.ppmy.cn/news/1157895.html

相关文章

40.同时最大在线人数问题求解(炸裂map)

思路分析&#xff1a; &#xff08;1&#xff09;对登录、登出数据构建map结构map(1, login_ts, -1, logout_ts)&#xff0c;使用炸裂函数对该map做炸裂处理 &#xff08;2&#xff09;炸裂后成为了两列&#xff0c;一列存储1or-1表示登录登出&#xff08;k&#xff09;&#x…

P2678 [NOIP2015 提高组] 跳石头

来自 <[NOIP2015 提高组] 跳石头 - 洛谷> 题意&#xff1a; 题目就是说有在起点和终点里面有5个石头&#xff0c;现在让你移出两个石头&#xff0c;要移哪两个可以满足最后最短距离最长。假如现在要移11位置和14位置的两块石头&#xff0c;最后的最短距离是2&#xff0c;…

OpenCV11-图像的模版匹配

OpenCV11-图像的模版匹配 图像的模版匹配 图像的模版匹配 前面通过图像直方图反向投影的方式在图像中寻找模版图像&#xff0c;由于直方图不能直接反映图像的纹理&#xff0c;因此&#xff0c;如果两幅不同的模版图像具有相同的直方图分布特性&#xff0c;那么在同一幅图中对着…

[Leetcode学习-C语言]Two Sum

题目连接&#xff1a;LeetCode - The Worlds Leading Online Programming Learning Platform leetcode写C真的很多坑...... 用 hash 拉链法 构造hash函数&#xff0c;才能写这么题小题。 typedef struct Node { // 得自己做个hash 拉链法struct Node *next;int val;int sign…

信息解码(Message Decoding, ACM/ICPC World Finals 1991, UVa 213)rust解法

考虑下面的01串序列&#xff1a; 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1101, 1110, 00000, … 首先是长度为1的串&#xff0c;然后是长度为2的串&#xff0c;依此类推。如果看成二进制&#xff0c;相同长度的后一个串等于前一个串加1。注意上述…

解决Armbian插有线才能连接无线网

问题 Armbian插一下有线才能自动连接无线网。 RK3228电视盒子&#xff0c;刷了armbian&#xff0c;机器有无线网卡和有线网卡。 在插上有线的情况下开机&#xff0c;无线也可以访问。拔掉有线&#xff0c;无线正常。 在没插有线的情况下&#xff0c;无线不能自动连接&#xff…

高效工具类软件使用

高效工具类软件使用 目录概述需求&#xff1a; 设计思路实现思路分析1.Leanote2.Obsidian 的使用 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better result,wait for…

ArcGIS笔记8_测量得到的距离单位不是米?一经度一纬度换算为多少米?

本文目录 前言Step 1 遇到测量结果以度为单位的情况Step 2 简单的笨办法转换为以米为单位Step 3 拓展&#xff1a;一经度一纬度换算为多少米 前言 有时我们会遇到这种情况&#xff0c;想在ArcGIS中使用测量工具测量一下某一段距离&#xff0c;但显示的测量结果却是某某度&…