Change Camera pixel format from qml - qt

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"))

Related

How to get the typename of a QObject instance parsing QML?

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();

how to get image file format from QPixmap?

In my program, user choice and load an image into QPixmap in some class and after some works on loaded QPixmap the QPixmap passed into an other class, in new class I want to save the passed QPixmap as file, but I don't know what's the QPixmap format!
How we can get image file format from QPixmap?
A pixmap is conceptually system-specific, has no format per se, and may well lose data from the image that you've loaded. Also note that the image format and file format are two different things.
To preserve the image format, you must use the QImage class.
To preserve the file format, you must explicitly use QImageReader to read the image. The file format is available through the reader's format() method. It needs to be stored separately from the image, and used when saving the image later.
Finally, you might wish to preserve the file's name.
As a matter of implementation detail, with the default Qt's raster backend, a QPixmap is a thin wrapper around QImage. But your intent is that of an image, not a pixmap, thus you should use the image class.
QImage img = pixmap.toImage();
img.save("/media/mmcblk0p1/xx.jpeg");

How to get RGB value from the returned value of the method com.sun.glass.ui.Robot.getPixelColor()?

I am trying to get the pixel colour below the mouse cursor in JavaFX. Since AWT Robot is having issue with JavaFX in Mac OS, I am planning to use com.sun.glass.ui.Robot.getPixelColor(). This method com.sun.glass.ui.Robot.getPixelColor() is found to return an integer value. So how can I derive the RGB value of the colour?
Also could anyone let me know if the AWTRobot class issue is going to be resolved in JavaFX 8?
I think there's a way to do this without resorting to private APIs. If you snapshot the Node to an Image, you can retrieve a PixelReader and use it to get the pixel color at the given coordinates.

How to set QCamera on label

I've decided no to use OpenCV. I will use the QCamera class. Everything is working perfect to this moment. I can capture and save images wherever I want, but the problem is how I can set the camera to a label or graphics view?
I mean, to see what is happening at the moment. When I make infinite loop everything crashes. Write any information you know, cause there are no examples how to do that, or I just can't see. If you can please write some source code.
Use QCameraVievFinder or QVideoWidget widgets ( docs - here) for that purpose, here is example for you:
#include <QCameraViewfinder>
// .......
QCamera *camera=new QCamera(this);
QCameraViewfinder *viewfinder = new QCameraViewfinder(this);
viewfinder->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
camera->setViewfinder(viewfinder);
setCentralWidget(viewfinder);
//viewfinder->show();
camera->start(); // to start the viewfinder
Note: you need to add to your *,pro file this config to use them: QT += multimedia multimediawidgets
If you want a bit more low level widget (to process every frame the way you like (face detection etc), subclass QAbstractVideoSurface, docs - here
or try to connect to QVideoProbe class (docs - here), though i could not do it myself, this class is a bit experimental i guess, didn't worked

Is there a simple way to restore widget sizes in Qt?

I'm looking for a way to preserve the size of windows in a Qt app.
I've seen that there is the possibility of using the following method for every widget:
saveGeometry()
But really, I don't find this a satisfactioning method. Is there something like setAutosaveGeometry(True)?
I'm especially looking for a way to store the widths of table columns.
The QHeaderView class also has two methods for saving and restoring it's state to and from a QByteArray: saveState()and restoreState()
A table view's headers are accessible via the horizontalHeader() and verticalHeader() methods.
saveGeometry returns a QByteArray value, you need to store it somewhere.
Example:
void MainWindow::closeEvent(QCloseEvent *event){
QSettings settings;
settings.setValue("geometry", saveGeometry());
}
For reading the geometry call the restoreGeometry function:
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent) {
[...]
QSettings settings;
restoreGeometry(settings.value("geometry").toByteArray());
[...]
}
To learn more about window geometry please read the documentation
See the Qt documentation on Restoring a Window's Geometry.

Resources