扩展 QML 属性时不能使用 QFont 类型 属性 值?
Can't use QFont type property values when extending QML properties?
这是我的代码
mytext.cpp
#ifndef MYTEXT_H
#define MYTEXT_H
#include <QObject>
#include <QtQml/qqml.h>
#include <QFont>
class MyText:public QObject
{
Q_OBJECT
Q_PROPERTY(QFont font READ font WRITE setFont)
Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily)
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize)
QML_ELEMENT
public:
MyText(QObject* object = nullptr);
QString fontFamily()const{return family;}
void setFontFamily(const QString& fy){family = fy;}
int fontSize()const{return ftSz;}
void setFontSize(int sz){ftSz = sz;}
QFont font()const{return ft;}
void setFont(const QFont& f){ft = f;}
private:
QFont ft;
QString family = "default family";
int ftSz = 12;
};
main.qml
import QtQuick 2.15
import MyText 1.0
MyText{
fontFamily:"asdasdad"
fontSize:20
//how to set the value of 'font' property ??
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include "mytext.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
auto *pMytext = qobject_cast<MyText *>(component.create());
if (pMytext) {
qWarning() <<"mytext family is = "<< pMytext->fontFamily()
<<"fontSize = "<<pMytext->fontSize()
<<"font is = "<<pMytext->font();
return EXIT_SUCCESS;
}
qWarning() << component.errors();
return EXIT_FAILURE;
}
当我运行时,我得到输出:
mytext family is = "asdasdad" fontSize = 20 font is = QFont(,-1,-1,5,50,0,0,0,0,0)
所以,我的问题是如何设置 'font' 的 属性 的值?如果不行,qquicktext如何对'Text'的元素起作用?
还有一个问题,如果我将类型 QFont
更改为 QFont*
,有没有办法在 qml 文件中设置?
可以使用Qt Qml类型的字体方法:
font: Qt.font({family: "Arial", pixelSize: 20})
https://doc.qt.io/qt-5/qml-qtqml-qt.html#font-method
或单独设置字体的属性:
font.family: "Arial"
font.pixelSize: 30
请注意,这两种方法不能混合使用,因此如果您设置 font
属性,则不能覆盖 font.pixelSize
例如
已编辑:修复了字体方法示例中的拼写错误
这是我的代码
mytext.cpp
#ifndef MYTEXT_H
#define MYTEXT_H
#include <QObject>
#include <QtQml/qqml.h>
#include <QFont>
class MyText:public QObject
{
Q_OBJECT
Q_PROPERTY(QFont font READ font WRITE setFont)
Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily)
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize)
QML_ELEMENT
public:
MyText(QObject* object = nullptr);
QString fontFamily()const{return family;}
void setFontFamily(const QString& fy){family = fy;}
int fontSize()const{return ftSz;}
void setFontSize(int sz){ftSz = sz;}
QFont font()const{return ft;}
void setFont(const QFont& f){ft = f;}
private:
QFont ft;
QString family = "default family";
int ftSz = 12;
};
main.qml
import QtQuick 2.15
import MyText 1.0
MyText{
fontFamily:"asdasdad"
fontSize:20
//how to set the value of 'font' property ??
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include "mytext.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
auto *pMytext = qobject_cast<MyText *>(component.create());
if (pMytext) {
qWarning() <<"mytext family is = "<< pMytext->fontFamily()
<<"fontSize = "<<pMytext->fontSize()
<<"font is = "<<pMytext->font();
return EXIT_SUCCESS;
}
qWarning() << component.errors();
return EXIT_FAILURE;
}
当我运行时,我得到输出:
mytext family is = "asdasdad" fontSize = 20 font is = QFont(,-1,-1,5,50,0,0,0,0,0)
所以,我的问题是如何设置 'font' 的 属性 的值?如果不行,qquicktext如何对'Text'的元素起作用?
还有一个问题,如果我将类型 QFont
更改为 QFont*
,有没有办法在 qml 文件中设置?
可以使用Qt Qml类型的字体方法:
font: Qt.font({family: "Arial", pixelSize: 20})
https://doc.qt.io/qt-5/qml-qtqml-qt.html#font-method
或单独设置字体的属性:
font.family: "Arial"
font.pixelSize: 30
请注意,这两种方法不能混合使用,因此如果您设置 font
属性,则不能覆盖 font.pixelSize
例如
已编辑:修复了字体方法示例中的拼写错误