I'm trying to use the applyWidthRatio function from the cpp file to provide DelegateChoice width even though this function is working fine for other components but not for DelegateChoice.Can someone please help me with this?
-
List.qml
import QtQuick 2.15import QtQuick.Layouts 1.15import QtQuick 2.14import Qt.labs.qmlmodels 1.0import TableModel 0.1
Rectangle { id: _eventLogScr clip: true border.width: 6 width: scalable.applyWidthRatio(909) height: scalable.applyHeightRatio(470) TableView { id:tableView1 anchors.fill: parent columnSpacing: 1 rowSpacing: 1 clip: true model: TableModel {}delegate: DelegateChooser { id:delegateChooser DelegateChoice { column: 0; Rectangle{ color: "transparent" border.width: 5 implicitWidth: scalable.applyWidthRatio(72) implicitHeight: scalable.applyHeightRatio(50) Text {} } } DelegateChoice { column: 1; Rectangle{ color: "transparent" border.width: 5 implicitWidth: scalable.applyWidthRatio(149) implicitHeight: scalable.applyHeightRatio(50) Text { } } } DelegateChoice { column: 2; Rectangle{ color: "transparent" border.width: 5 implicitWidth: scalable.applyWidthRatio(170) implicitHeight: scalable.applyHeightRatio(50) Text { } } } } }}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "theme.h"
#include "tabelmodel.h"int main(int argc, char *argv[])
{
qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
static const char ENV_VAR_QT_DEVICE_PIXEL_RATIO[] = "QT_DEVICE_PIXEL_RATIO";
if (!qEnvironmentVariableIsSet(ENV_VAR_QT_DEVICE_PIXEL_RATIO)
&& !qEnvironmentVariableIsSet("QT_AUTO_SCREEN_SCALE_FACTOR")
&& !qEnvironmentVariableIsSet("QT_SCALE_FACTOR")
&& !qEnvironmentVariableIsSet("QT_SCREEN_SCALE_FACTORS")) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}
QGuiApplication app(argc, argv);QQmlApplicationEngine engine;
Theme *scalable = new Theme();
engine.rootContext()->setContextProperty("scalable", scalable);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);return app.exec();
}theme.h#ifndef THEME_H
#define THEME_H
#include <QQmlPropertyMap>
#include <QObject>
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>
#include <iomanip>class Theme :public QObject
{
Q_OBJECT
public:
Theme();
qreal refDpi;
qreal refHeight;
qreal refWidth;
QRect rect;
qreal height;
qreal width;
qreal dpi;qreal m_hratio;
qreal m_ratioFont;
qreal m_wratio;Q_INVOKABLE int applyFontRatio(const int value);
Q_INVOKABLE int applyHeightRatio(const int value);
Q_INVOKABLE int applyWidthRatio(const int value);
};#endif // THEME_H
theme.cpp
#include "theme.h"Theme::Theme()
{
refDpi = 96.;
refHeight = 600.;
refWidth = 1024.;
rect = QGuiApplication::primaryScreen()->geometry();
qDebug()<<rect;
height = rect.height();
width = rect.width();
dpi = QGuiApplication::primaryScreen()->logicalDotsPerInch();
qDebug()<<dpi;
m_hratio = height/refHeight;
m_wratio = width/refWidth;m_ratioFont = qMin(heightrefDpi/(dpirefHeight), widthrefDpi/(dpirefWidth));
}
int Theme::applyFontRatio(const int value)
{
return int(value * m_ratioFont);
}
int Theme::applyHeightRatio(const int value)
{
//qDebug()<<s<<" of height is: "<<(value * m_hratio)<< " Hratio: "<<m_hratio;
return qMax(2, int(value * m_hratio));
}
int Theme::applyWidthRatio(const int value)
{
//qDebug()<<"Value is "<<value<<s<<" of Width is: "<<(value * m_wratio)<< " Wratio: "<<m_hratio;
return qMax(2, int(value * m_wratio));
}