diff --git a/CMakeLists.txt b/CMakeLists.txt index 07461f2..04a087a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,8 @@ set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) -find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) +find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Quick QuickWidgets) +find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Quick QuickWidgets) set(PROJECT_SOURCES main.cpp @@ -44,7 +44,7 @@ else() endif() endif() -target_link_libraries(QtMd PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) +target_link_libraries(QtMd PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::QuickWidgets) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an diff --git a/main.qml b/main.qml index 68c2108..b27f78f 100644 --- a/main.qml +++ b/main.qml @@ -1,5 +1,43 @@ import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Controls.Material 2.15 -Item { +Rectangle { + anchors.fill: parent + border.color: Material.accent + border.width: 1 + Row { + anchors.centerIn: parent + spacing: 10 + + Button { + id: clickBtn + width: 100 + + text: "点击我" + flat: false + anchors.verticalCenter: parent.verticalCenter + + onClicked: { + countText.text = (parseInt(countText.text) + 1).toString(); + console.log("按钮被点击了, text:" + countText.text) + } + } + + Rectangle { + width: 100 + height: 32 + anchors.verticalCenter: parent.verticalCenter + border.width: 1 + border.color: Material.accent + + Text { + id: countText + anchors.verticalCenter: parent.verticalCenter + anchors.horizontalCenter: parent.horizontalCenter + text: qsTr("0") + } + } + } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 6395a77..dd64f81 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,11 +1,23 @@ #include "mainwindow.h" #include "./ui_mainwindow.h" +#include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + + + QQuickWidget *quickWidget = new QQuickWidget; + quickWidget->setSource(QUrl::fromLocalFile(":/main.qml")); + quickWidget->setGeometry(ui->centralwidget->geometry()); + quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView); + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(quickWidget); + ui->centralwidget->setLayout(layout); } MainWindow::~MainWindow() diff --git a/mainwindow.h b/mainwindow.h index f7a3da3..7d3a763 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,6 +3,7 @@ #include + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; diff --git a/mainwindow.ui b/mainwindow.ui index 6e80a62..c3e8aeb 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -14,17 +14,6 @@ MainWindow - - - - 0 - 0 - 800 - 19 - - - -