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.4import QtQuick.Controls 1.3import 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 #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();}

5.MyClass.h

代码

#ifndef MYCLASS_H#define MYCLASS_H#include #include #include #include #include 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 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 qmlCONFIG += c++11INCLUDEPATH += ./LIBS += -L/usr/lib/x86_64-linux-gnu -lzLIBS += /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 modelQML_IMPORT_PATH # Additional import path used to resolve QML modules just for Qt Quick DesignerQML_DESIGNER_IMPORT_PATH =# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += target

9.qml.qrc

代码

<RCC><qresource prefix="/"><file>main.qml</file></qresource></RCC>