dose anyone know how to convert Qt::WidgetAttribute to Qt::WindowFlags???
and by convert i mean is there any WidgetAttribute like WA_NoBackground or WA_TranslucentBackground as a flag for WindowFlags....???
i've been trying to transparent my ApplicationWindow Item in QML but i couldn't.
Main.Qml
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
flags: Qt.SubWindow | Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint
opacity: 0.5
Image {
anchors.fill: parent
source: "blah.png" // and this picture in transparent
}
}
and know im looking for the flag that works like WA_NoBackground and WA_TranslucentBackground
Main.cpp
int main(int argc, char *argv[])
{
Application app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl("blah.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
window->show();
window->setColor(Qt::transparent);
return app.exec();
}
Related
I had a question about how to trimming corners around the edges in ApplicationWindow, i tried to use clip, opacity and many standard methods of the qml language My Code:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.13
ApplicationWindow {
id: loginPage
visible: true
opacity: 0.8
x: 732; y:245
width: 456; height: 529
color: "#3C096C"
flags: Qt.FramelessWindowHint
}
Here is the result that I want to get:
The answer is to not try to make the ApplicationWindow itself have a radius, but to make it transparent and simply draw a rounded Rectangle as the first child.
ApplicationWindow
{
id: loginPage
visible: true
x: 732; y:245
width: 456; height: 529
color: "transparent"
flags: Qt.FramelessWindowHint
Rectangle {
color: "#3C096C"
opacity: 0.8
radius: 20
anchors.fill: parent
}
}
Another alternative is to use the method setMask() of QWindow:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QWindow>
class Utils: public QObject{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void applyRadius(QWindow *window, int radius){
QRect r(QPoint(), window->geometry().size());
QRect rb(0, 0, 2 * radius, 2 * radius);
QRegion region(rb, QRegion::Ellipse);
rb.moveRight(r.right());
region += QRegion(rb, QRegion::Ellipse);
rb.moveBottom(r.bottom());
region += QRegion(rb, QRegion::Ellipse);
rb.moveLeft(r.left());
region += QRegion(rb, QRegion::Ellipse);
region += QRegion(r.adjusted(radius, 0, -radius, 0), QRegion::Rectangle);
region += QRegion(r.adjusted(0, radius, 0, -radius), QRegion::Rectangle);
window->setMask(region);
}
};
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
Utils utils;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("Utils", &utils);
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();
}
#include "main.moc"
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.13
ApplicationWindow {
id: loginPage
visible: true
opacity: 0.8
x: 732; y:245
width: 456; height: 529
color: "#3C096C"
flags: Qt.FramelessWindowHint
Component.onCompleted: ()=> Utils.applyRadius(loginPage, 20);
}
I'm having a problem when I try to use Item::grabToImage() qml method.
No matter which item I point to, it always says the following error:
grabToImage: item's window is not visible
I tried using the root/toplevel Item named rect too, but it didnt work.
My goal: I want to capture a rectangle sized image with the map tile and polygon draw on it
Below there's a minimal reproducible example
import QtQml 2.2
import QtLocation 5.9
import QtPositioning 5.9
import QtQuick 2.0
import QtQuick.Controls 2.4
Item {
id: rect
width: 1024
height: 768
visible: true
Plugin {
id: mapPlugin
name: "osm"
}
Map {
id: map
enabled: true
visible: true
parent: rect
gesture.enabled: true
anchors.fill: parent
plugin: mapPlugin
zoomLevel: 14
activeMapType: supportedMapTypes[3]
}
Item {
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.margins: 10
height: 40
Button {
id: saveToDisk
text: qsTr("Pick")
onClicked: {
map.grabToImage(function (result) {
console.log('saving to disk..')
result.saveToFile("pick.png")
})
}
}
}
}
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuickWidgets/QQuickWidget>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQuickWidget *q = new QQuickWidget;
q->setResizeMode(QQuickWidget::SizeRootObjectToView);
q->setSource(QUrl("main.qml"));
q->show();
return app.exec();
}
The strategy of QQuickWidget for painting is to create an off-screen QQuickWindow that renders the QML from where a screenshot is taken and drawn onto the widget. The above limits the use of grabToImage() since this method requires that the QQuickWindow of the items be visible.
The solution is to use QQuickView + QWidget::createWindowContainer():
#include <QApplication>
#include <QWidget>
#include <QQuickView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQuickView *q = new QQuickView;
q->setResizeMode(QQuickView::SizeRootObjectToView);
q->setSource(QUrl("main.qml"));
QWidget * container = QWidget::createWindowContainer(q);
container->show();
return app.exec();
}
I'm working through the book "Learn Qt 5" from Nicholas Sherriff.
Now I have some problems getting the QtStackView component to work properly.
I've written following code:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/views/MasterView.qml")));
return app.exec();
}
MasterView.qml
import QtQuick 2.5
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
Window {
visible: true
width: 1024
height: 768
title: qsTr("Client Management")
StackView {
id: contentFrame
initialItem: Qt.resolvedUrl("qrc:/views/SplashView.qml")
}
}
SplashView.qml
import QtQuick 2.5
Rectangle {
anchors.fill: parent
color: "#f04c42"
}
But when executing, the rectangle from SplashView.qml does not appear.
I do not get any errors. If I put the rectangle block inside the window block it works. But even if i put the rectangle with an id in the StackView block it doesn't work.
I'm using an older version of QtQuick than the book recommends, becouse im using not the newest debian distribution.
Am I missing something?
Thanks
The problem is simple, the Rectangle in the SplashView takes the size of the parent:
Rectangle {
anchors.fill: parent //<---
color: "#f04c42"
}
And the parent is the StackView, but the StackView has no size, so the solution is to set a size, for this we can use the anchors:
StackView {
id: contentFrame
anchors.fill: parent // <--- possible solution
initialItem: Qt.resolvedUrl("qrc:/views/SplashView.qml")
}
I create a project on QtQuick in QT Creator 4.3.1 without using the ui form.
Here's the code main.qml:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea {
anchors.fill: parent
onClicked: {
console.log(qsTr('Clicked on background. Text: "' + textEdit.text + '"'))
}
}
TextEdit {
id: textEdit
text: qsTr("Enter some text...")
verticalAlignment: Text.AlignVCenter
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 20
Rectangle {
anchors.fill: parent
anchors.margins: -10
color: "transparent"
border.width: 1
}
}
}
The program runs and works.
Now I want to get rid of Window and replace it with Rectangle:
import QtQuick 2.6
Rectangle {
id: root
width: 200; height: 200;
color: "#ffffff"
}
But when the program starts, nothing happens, the form does not open
What am I doing wrong?
main.cpp code. code in the same in both cases:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
In a Qt application you must have at least one window that is the top-level, that is, the window where other components are placed.
When you create your project, you should get the default main.cpp:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
So let's analyze the elements and use the documentation for it:
QQmlApplicationEngine:
QQmlApplicationEngine provides a convenient way to load an application from a single QML file.
[...]
Unlike QQuickView, QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window.
That is, QQmlApplicationEngine does not create a top-level, so if we want the window to be shown you must use another element, and as recommended, an option is to use Window{}
In your second test you are using an item, e.g. Rectangle, and this is only a component and is not able to create a top-level, so for that it is advisable to use QQuickView:
The QQuickView class provides a window for displaying a Qt Quick user interface.
[...]
So if you want to show the Rectangle you should use the following in your main.cpp:
main.cpp
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.show();
return app.exec();
}
main.qml
import QtQuick 2.6
Rectangle {
id: root
width: 200; height: 200;
color: "#ffffff"
}
I'm trying to list the items from a QStringList to QML, but I keep getting an undefined error for the bindings.
Here is the C++ code:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QStringList lst;
QString m("item 1");
lst.append(m);
QQmlComponent comp(&engine);
QQmlContext *ctx = engine.rootContext();
ctx->setContextProperty("pLst", QVariant::fromValue(lst));
return app.exec();
}
Here is the QML code:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
id: root; objectName: "root"
title: qsTr("Doesn't Matter")
width: 640
height: 480
visible: true
ListView{
id: lst
model: pLst
}
}
The error says pLst is not defined.
This is because you call load() before you set context property, so pLst does not yet exists at the moment ListView is constructing.
You should call load() after you set the context properties used for initialization of QML objects.