So I'm sending a QDateTime to QML in order to display it and do some checkings (ideally I would like to be able to use all javascript date functions like getTime(),getMonth()...), and I realized if I send:
QDateTime(2019-10-30 11:15:00.000 CET Qt::TimeZone Europe/Amsterdam )
And then I read the date on QML, I get the time on the local timezone instead of the one set on the QDateTime...
Wed Oct 30 06:15:00 2019 GMT-0400 (NY timezone)
Is there any way I can keep the timezone in the QML side?
Thanks!!
The javascript Date class is quite limited. It holds an internal UTC time/date and doesn't allow to change the timezone. It also displays by default converting to the local timezone, which is what you have observed.
There is a javascript Moment library with timezone support, to alleviate some of the Date shortcomings, but it is not fully compatible with QML, IIRC.
Anyway, the best route seems to be avoiding Date objects in QML as much as you can, and use something else instead. Either a javascript alternative, or even better, your own C++ class encapsulating QDateTime, exposed as a context property to QML, with whatever methods you need in the QML side. Something like this example ApplicationData from the official Qt documentation.
Following the #Pedro's advice (I don't know how to tag you properly...), I used moment.js as well as moment-timezone.js so I could reference any date to the timezone I want. To anyone interested, that's how I did it:
import QtQuick 2.9
import "./moment.js" as Moment
import "./moment-timezone-with-data.js" as MomentTimezone
Item {
anchors.fill: parent
property date customDate: new Date(); // local timezone is "America/New_York"
function getDateWithTimeZone(date, timeZone) {
var momentDate = moment(new Date(date));
var momentDateTz = momentDate.tz(timeZone);
return momentDateTz;
}
Text {
anchors.centerIn: parent
text: customDate.toLocaleString() + "\n"
+ getDateWithTimeZone(customDate, "Europe/Paris").toString()
}
}
which gives the following output:
Related
Looking at the documentation of Qt5 it seems possible to change the pixel format of the camera. I need to create a QCameraViewfinderSettings set the new pixel format and set the new settings to the camera... like in this example
QCameraViewfinderSettings viewfinderSettings;
viewfinderSettings.setPixelFormat(QVideoFrame::Format_RGB32);
camera->setViewfinderSettings(viewfinderSettings);
But I cannot find anything similar in QML... Is there any way to do the something in QML? I'd like something like:
Camera {
id: camera
viewfinder.pixelFormat = VideoFrame.Format_RGB32
}
If QML doesn't allow me to set the pixel format what alternatives have I?
No, you can't change the pixel format in QML per the maintainer of that component, but you can pass the QML object to C++ and do it from there per bug report 42909.
As Yoann Lopes wrote in that bug's comments, you can access the QCamera of the QML object with:
QCamera *cam = qvariant_cast<QCamera*>(obj->property("mediaObject"))
What is best practice to deal with DateTime objects in MeteorJS with Redux?
Lately implemented the usage of Redux in my React Native + Meteor app for offline functionality following the blog post of Spencer Carli. But I have some problems with the way both systems store DateTime objects. I must admit that I have no detailed understanding of how date objects are handled in JavaScript.
Meteor
My Meteor collection model contains a datetype object (I use simpl-schema):
const Objects= new Mongo.Collection('objects');
Objects.schema = new SimpleSchema({
startedAt: Date,
});
Date presentation
In the react-native app I have to parse the date type to a string:
<Text>Started at: {object.startedAt.toUTCString()}</Text>
However, in disconnected mode the date is of type string ā2017-02-11T09:00:00.000Zā so this parse fails
Insert items with a Date object
The insert of items in react-native:
Meteor.collection('objects').insert({
startedAt: new Date(),
}
);
This is accepted in disconnected mode, but when connection with the server is restored, insertion of items in the MongoDB is rejected.
Though not a great answer my primary suggestion, whenever using dates in Javascript, is to use the momentjs. It will save you a ton of time.
Otherwise, and I'm not sure if this is the "right" approach, I would do
<Text>Started at: {new Date(object.startedAt).toUTCString()}</Text>
that way the startedAt value will always be converted to a Date object, regardless of it it's already one or if it's a string.
Hope that helps!
I have got a QQuickView which has loaded a qml file like the following.
Rectangle { width: 100; height: 100 }
Then I am retrieving the root object via QObject *root = view->rootObject().
Now I want to get the class name from this object.
The following code results into "QQuickRectangle"
root->metaObject()->className()
But what I want is "Rectangle" just like the typename in the qml file.
Any idea?
Edit: I want to build a treeview with the object hirarchie of a qml file like QtCreator.
There is a pattern there, for qml types implemented in C++ the name would be QQuickSomething, for qml types implemented in qml the name would be Something_QMLTYPE_X_MAYBEMORESTUFF(objAddress).
So you can do some basic string editing depending on the result you get to isolate the actual type name:
QString name = QString(root->metaObject()->className());
if (name.contains("QQuick")) name.remove("QQuick");
else if (name.contains("QMLTYPE")) name.remove(QRegExp("_QMLTYPE_[0-9]*.*"));
// else it might be just a QObject or your on custom type you should handle
Edit: I want to build a treeview with the object hirarchie of a qml
file like QtCreator.
Unless you are willing to dig into and use private APIs, it would likely be easier and also more useful to have your own custom model to drive both a view and the actual object tree. Also, QML is quite easy to parse, I'd personally buckle down and write a parses faster than it would take me to get into the existing one, especially if all that is needed is an object tree outline, but YMMV.
There is "better" information kept on this (QQmlType & QQmlMetaType), but it is not accessible through any public API that I can think of.
Can you explain what you would like to do with it? Maybe there's an alternative.
QtQuick doesn't provide some special metadata for QML items. It looks that QtQuick uses item types internally only while parsing the source.
The known workaround is objectName:
Rectangle {
objectName: "Rectangle"
}
and so:
QString className = item->objectName();
I have a C++/CX app that is processing some data from a file. It has a string in there representing the culture that was used to save the dates, and it has some dates. I need to convert them from strings to Platform::DateTime. I have heard that Windows::Globalization::DateTimeFormatting is the class to use, but I don't see how to use it for that. Does anyone have an example?
The C++/CX projection of WinRT differs from the Managed (C#/VB) projection in a number of ways, and one of the most major is in the projection of fundamental types (such as Point, Size, String, and DateTime).
The managed projection projects these types as .NET types (with all the underlying support of the BCL) while the C++ projection generally minimally projects these types for interop, expecting the user to rely on C++ library support for more advanced functionality.
So, where in .NET a signed 32-bit integer becomes a System.Int32 (with its relevant .Parse functionality) in C++ you get a regular C++ int and are expected to use CRT functionality (_wtoi) to accomplish a similar task. This difference often results in a 'feature gap' between the different projections, one of the more painful of which is in dealing with the DateTime structure (which has very rich support in the BCL).
The solution I've managed to get was to start with the COleDateTime class (found by including ATLComTime.h) and going COleDateTime->SYSTEMTIME->FILETIME->_ULARGE_INTEGER->Windows::Foundation::DateTime. It's serious gymnastics, but the COleDateTime class has the language-specific parsing capability that you require.
LCID lcid = LocaleNameToLCID(L"es-es", LOCALE_ALLOW_NEUTRAL_NAMES); //parse language tag to get locale ID
COleDateTime dt;
dt.ParseDateTime(L"12 enero, 2012 10:00:01", 0, lcid); //parse date string based on language
//get system time struct
SYSTEMTIME st;
dt.GetAsSystemTime(st);
//COleDateTime is in local timezone, DateTime is in UTC, so we need to convert
SYSTEMTIME st_utc;
TzSpecificLocalTimeToSystemTime(nullptr, &st, &st_utc);
//get filetime struct to get a time format compatible with DateTime
FILETIME ft;
SystemTimeToFileTime(&st_utc, &ft);
//use _ULARGE_INTEGER to get a uint64 to set the DateTime struct to
_ULARGE_INTEGER ulint = {ft.dwLowDateTime, ft.dwHighDateTime};
Windows::Foundation::DateTime wfdt;
wfdt.UniversalTime = ulint.QuadPart;
I've asked around about the DateTimeFormatter class, and the documentation is incorrect; it does not support parsing and is not intended to (only formatting).
Would std::get_time do the trick? The example at the bottom of the page suggests you can parse a date string using a locale into a tm struct. I imagine you could then convert the tm struct into the correct WinRT DateTime.
I use this code:
auto cal = ref new Windows::Globalization::Calendar();
cal->AddSeconds(additionalSeconds);
return cal->GetDateTime();
With Windows::Globalization::Calendar, you have all the nice time functions you need: https://msdn.microsoft.com/library/windows/apps/br206724
Does anybody know of a Windows tool to report fake dates/times to a process?
Apparently there are Linux programs that can be used to test how the software will react in the future / in a different timezone or to trigger scheduled tasks without actually modifying the system clock. Are there such programs for Windows?
RunAsDate can do this.
Starting about 2 years ago, I always abstract the call to DateTime.Now (C#) through a Utility class. This way I can always fake the date/time if I want to, or just pass it directly through to DateTime.Now.
Sounds similiar to what BillH suggested.
public class MyUtil
{
public static DateTime GetDateTime()
{
return DateTime.Now.AddHours(5);
//return DateTime.Now;
}
}
Wrapper your calls to the 'getCurrentDateTime()' system calls so you can introduce offsets or multipliers into the times your code reads during testing?