'Can't use QFont type property values when extending QML properties?
here is my code
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;
}
when i runing ,i get output:
mytext family is = "asdasdad" fontSize = 20 font is = QFont(,-1,-1,5,50,0,0,0,0,0)
so,my question is how could i set the value for the property of 'font'? if no way,how qquicktext work for the element of 'Text'?
and one more question,if i change the type QFont to QFont*,is there any way to setting in qml file?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
