Qt学习记录

目录

1. setText()和setPlainText()的区别

2. QML(QtDS工程)导入Qt Quick Application

3.  QML与Qt C++交互

4. QML的TextEdit获取文本内容

5.  QML的TextEdit设置滚动条

6. Qt Quick Application工程打包exe文件无法打开

7. QML的ComboBox增删下拉选项

8. 使用QML的界面如何调用widget的窗口


1. setText()和setPlainText()的区别

// setText识别html
ui->textEdit->setText(code);
// setPlainText不识别html 纯文本
ui->textEdit->setPlainText(code);

2. QML(QtDS工程)导入Qt Quick Application

2.1 将QML工程整体复制到Qt工程目下,并把QML工程目录下.conf文件剪切(复制也可)至Qt工程目录下。将QML工程整体导入进资源文件,并把.conf文件导入进资源文件

2.2 仅仅使用以下代码可实现QML导入Qt Quick Application,QML主导界面

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>

int main(int argc, char *argv[])
{
    // 可能出现警告 不影响
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    // QtDS设计的QML工程会有一个imports文件夹,需要添加路径
    engine.addImportPath("qrc:/AS_TOOL_UI/imports");
    // 载入QML
    engine.load(QUrl(QStringLiteral("qrc:/AS_TOOL_UI/content/App.qml")));


    return app.exec();
}

2.3 可能出现部分组件未找到的错误,注释掉未找到的组件即可

3.  QML与Qt C++交互

>>>>>>>>>>>QML调用C++函数方法<<<<<<<<<<<

3.1 创建一个用户类,继承QObject,函数方法设置为public并加Q_INVOKABLE前缀修饰 

#ifndef USERFUNC_H
#define USERFUNC_H

#include <QObject>
#include <QString>

class UserFunc : public QObject
{
    Q_OBJECT
public:
    explicit UserFunc(QObject *parent = nullptr);
    // 加Q_INVOKABLE前缀修饰  一般只以函数形式
    Q_INVOKABLE QString get_str(QString str);
};

#endif // USERFUNC_H
#include "userfunc.h"
#include <QString>

UserFunc::UserFunc(QObject *parent)
    : QObject{parent}
{
}

QString UserFunc::get_str(QString str)
{
    return str;
}

3.2 在main函数里将用户类添加进QML中,QML即可直接使用myClass.get_str()

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QtQml> // 导入此头文件方可使用setContextProperty方法
#include "userfunc.h" // 导入用户类头文件

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    // 实例化用户类
    UserFunc myClass;

    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/AS_TOOL_UI/imports");
    // 将用户类添加到QML中
    engine.rootContext()->setContextProperty("myClass", &myClass);
    engine.load(QUrl(QStringLiteral("qrc:/AS_TOOL_UI/content/App.qml")));


    return app.exec();
}

>>>>>>>>>>>C++获取设置QML元素属性<<<<<<<<<<< 

3.3 通过获取window根节点,查找对象名称来获取到QML的UI元素,然后获取或设置属性,C++数据格式要通过QVariant()转化为QML数据格式

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QtQml>
#include <QQmlProperty> // 需添加
#include <QQuickItem> // 需添加
#include <QVariant> // 需添加

void Test(QList<QObject *> obj)
{
    auto window = qobject_cast<QObject*>(obj.front()); // 获取window根节点
    auto target= window->findChild<QObject*>("text"); // 查找objectName是text的元素
    target->setProperty("text",QVariant("hello word")); // 设置text属性为success 不切断属性绑定
    //QQmlProperty::write(target,"text",QVariant("hello word")); // 切断属性绑定
    QString text = target->property("text").toString(); // 获取text属性的属性值,返回QVariant类型需转换数据类型
}
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/UntitledProject/imports");
    engine.load(QStringLiteral("qrc:/UntitledProject/content/App.qml"));
    Test(engine.rootObjects()); // 传入QML对象参数 必须在engine.load()后

    return app.exec();
}
Text {
        objectName: "text"
        width: 343
        height: 218
        text: qsTr("Hello Word")
        font.pixelSize: 56
    }

 >>>>>>>>>>>QML连接C++信号<<<<<<<<<<<

3.4  使用Connections进行连接,可多个,target属性代表连接信号所属类,类对象必须先传入QML才可使用(参见3.2所述如何将用户类添加到QML)

/*******************main.cpp*******************/
engine.rootContext()->setContextProperty("mySerial", myClass.serial_main); // 将C++类添加进QML使用
/*******************serial.cpp******************/
signals:
    void readSignal(QString xxx);
/***********************QML*********************/
// 连接C++的信号 可多个 target代表连接信号所属类  类对象必须先传入QML才可使用
Connections {
    target: mySerial
    //on + 信号名(首字母大写)
    onReadSignal: function(xxx){
        console.log(xxx)
    }
}

4. QML的TextEdit获取文本内容

// 使用属性获取 可能会出现textEdit的XML源代码也被获取的情况
textEdit.text
// 使用方法获取 只会获取文本
textEdit.getText(0, textEdit.length)

5.  QML的TextEdit设置滚动条

Rectangle {
            id: serial_recDataPage
            width: 854
            height: 473
            // 添加滚动条
            ScrollBar {
                /***********必要的1************/
                id: serial_vbar1
                hoverEnabled: true
                active: hovered || pressed
                orientation: Qt.Vertical
                // 这句是ScrollBar滚动文本的关键
                size: serial_recDataPage.height / serial_recDataEdit.height
                width: 10
                anchors.top: parent.top
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                /*****************************/
            }
            // 添加鼠标区域
            MouseArea {
                /***********必要的2***********/
                id: serial_recDataMouse
                anchors.fill: parent
                anchors.rightMargin: 10
                clip: true
                /*****************************/
                // 添加TextEdit
                TextEdit {
                    /***********必要的3***********/
                    id: serial_recDataEdit
                    // 这句是ScrollBar滚动文本的关键
                    y: -serial_vbar1.position * serial_recDataEdit.height
                    height: contentHeight
                    width: parent.width
                    /****************************/
                    font.pixelSize: 16
                    wrapMode: Text.Wrap
                    selectByKeyboard: true
                    selectByMouse: true
                }
            }
        }

6. Qt Quick Application工程打包exe文件无法打开

6.1 使用以下命令打包,其中最后的路径为构建环境的QML文件依赖路径

windeployqt projectName.exe --qmldir D:\Qt\Qt6.2\6.3.0\mingw_64\qml

6.2 可能还无法打开,请尝试以下方法:

1. 将我们生成的目录中的和QML相关的文件替换为Qt安装目录下的文件,可能我们生成的这些文件有缺失

2. 复制exe文件到构建环境的目录(Qt\Qt6.2\6.3.0\mingw_64\bin\)里面,如果能打开则证明缺少部分依赖文件,可复制出构建环境根目录(Qt\Qt6.2\6.3.0\mingw_64\)下的bin,plugins,qml文件,进行手动删减排除问题

7. QML的ComboBox增删下拉选项

ComboBox {
        id: myComboBox
        x: 236
        y: 742
        width: 111
        height: 41
        // 使用ListModel给ComboBox添加下拉内容
        model: ListModel {
            id: testList
            ListElement {text: "111"}
            ListElement {text: "222"}
            ListElement {text: "333"}
        }
    }

// 使用ListModel的id来操作myComboBox的下拉选项
testList.clear() // 清空选项
testlist.remove(1) // 移除索引为1的选项
testList.append({text: "444"}) // 追加选项

8. 使用QML的界面如何调用widget的窗口

此类情况一般是出现在使用QML作为UI界面,但需要创建例如QMessageBox或QFileDialog等widget窗口的需求下

//#include <QGuiApplication> // 需修改
#include <QApplication> // 需添加
#include <QQmlApplicationEngine>
#include <QQuickView>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    //QGuiApplication app(argc, argv); // 使用此创建app不可使用widget
    QApplication app(argc, argv); // 使用此创建app即可使用widget

    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/AS_TOOL_UI/imports"); // 添加imports文件路径
    engine.load(QUrl(QStringLiteral("qrc:/AS_TOOL_UI/content/App.qml"))); // 加载QML

    return app.exec();
}

END

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿衰0110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值