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


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

    code review!

    1.运行

    请添加图片描述

    2.文件结构

    在这里插入图片描述

    3.main.qml

    在这里插入图片描述

    代码

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("QML ComboBox C++ signal")
    
        property string selectedOption
    
        ComboBox {
            id: comboBox
            x: 12
            y: 58
            width: 147
            height: 25
            model: ["选项一", "选项二", "选项三"]
    
            Component.onCompleted: {
                selectedOption = "选项一"
            }
    
            onCurrentIndexChanged: {
                selectedOption = comboBox.currentText;
            }
        }
    
        Button {
            id: button
            x: 18
            y: 134
            text: qsTr("发送")
            onClicked: {
                myClass_rename.setValue(selectedOption);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    4.main.cc

    在这里插入图片描述

    代码

    #include "MyClass.h"
    #include 
    #include 
    #include 
    
    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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5.MyClass.h

    在这里插入图片描述

    代码

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    class MyClass : public QObject {
        Q_OBJECT
    
      public:
        explicit MyClass(QObject *parent = nullptr);
    
      public slots:
        void setValue(QString value);
    };
    
    #endif // MYCLASS_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.MyClass.cc

    在这里插入图片描述

    代码

    #include "MyClass.h"
    #include 
    
    MyClass::MyClass(QObject *parent) : QObject(parent) {}
    
    void MyClass::setValue(QString value) {
        qDebug() << "value= " << value;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    8.ComboBox.pro

    代码

    QT += quick qml
    CONFIG += c++11
    
    INCLUDEPATH += ./
    
    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.0
    
    SOURCES += \
            ./main.cc
    
    RESOURCES += ./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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    9.qml.qrc

    代码

    <RCC>
        <qresource prefix="/">
            <file>main.qml</file>
        </qresource>
    </RCC>
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    M语言-数据类型
    05-`Linux`的软件管理
    LeetCode50天刷题计划(Day 11—— 最接近的三数之和(8.40-10.00)
    机器学习:朴素贝叶斯算法(Python)
    鸡卵清白蛋白偶联维生素A(VA-OVA),Vitamin A-Ovalbumin Conjugate
    Android -- 每日一问:Activity的启动模式(launchMode)有哪些,有什么区别?
    PyCharm因安装了illuminated Cloud插件导致加载项目失败
    掌握高效创作的艺术:AI助你轻松生成高质量文章
    【Vue渲染】 条件渲染 | v-if | v-show | 列表渲染 | v-for
    Linux系统命令——通过端口确认进程及路径方法
  • 原文地址:https://blog.csdn.net/weixin_43297891/article/details/133873097