I am running Qt 5.11.0 under Windows (I understand that it is very old but I cannot upgrade that embedded system).
For some reason, QML Menu from QtQuickControls1 ignores its style if app is a QGuiApplication, but works if it is a QApplication (from QtWidgets).
I also noticed that QApplication uses Desktop style by default, whereas QGuiApplication uses Base style instead and cannot load Desktop style at all.
Is QtWidgets dependency required to load QtQuickControls1 styles correctly, or am I missing something?
Why does QApplication change QML styling at all?
main.cpp
#include <QtQml/qqmlapplicationengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QtWidgets/QApplication>
int main(int argc, char ** argv) {
//QGuiApplication* app = new QApplication(argc, argv); //works
QGuiApplication* app = new QGuiApplication(argc, argv); //does not work
QQmlApplicationEngine engine;
engine.load(QUrl("file:///C:/main.qml"));
app->exec();
delete app;
return 0;
}
main.qml
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Window {
visible: true
Column{
CheckBox { text: "checkbox"}
ComboBox { model: ["combobox"] }
Button { text: "button"}
Button {
text: "custom"
menu: Menu {
style: MenuStyle { frame: Rectangle { color: "yellow" } }
MenuItem{text: "menuitem"}
}
}
}
}
Expected results:
Menu should have yellow background.
Actual results:
I tried your code with 5.15.7 and was able to see the yellow rectangle whether I used QApplication or QGuiApplication. So it may be a bug in version 5.11.
Related
I have problems with my QML ScrollViews in a Qt application. I can not avoid the interactive mode, even setting the property to 'false'
For testing, I have the docu QML example loaded in a QQuickView:
main.cpp:
int main(int argc, char *argv[])
{
QtQuickControlsApplication a(argc, argv);
QQuickView* pView = new QQuickView();
pView->setSource(QUrl("qrc:/TestScroll.qml"));
pView->setColor("white");
pView->setHeight(400);
pView->setWidth(600);
pView->show();
return a.exec();
}
TestScroll.qml:
import QtQuick 2.9
import QtQuick.Controls 2.2
ScrollView {
width: 200
height: 200
ScrollBar.vertical.interactive: false // Not working
ListView {
model: 20
delegate: ItemDelegate {
text: "Item " + index
}
}
}
What am i doing wrong?
I think I did not explain my problem properly, sorry...
What I need is to avoid the 'bounce' at the end of the scroll view when scrolling using the wheel. When I use the scrollBar for scrolling, I get no bounce at the end of the scrolling screen
Thanks,
Diego
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 am building a desktop application using QML with QQuickWidget and QMainWindow, everything looks work fine except to the FileDialog. When I open a window using FileDialog it works flawlessly at first time but in the second time and so on the FileDialog window is rendered behind the main window, preventing the user to choose a file, it is just possible to choose a file after click on the "choose window" (that bring it forward). If the application is in fullscreen mode it is not possible to realize that the FileDialog window is opened.
It just happen when I use QQuickWidget and QMainWindow, if I create a project with QGuiApplication (without QQuickWidget and QMainWindow) I don't have any problems with FileDialog behavior. Some idea how can I make FileDialog work properly with QQuickWidget?
FileDialog code:
/*uncomment ApplicationWindow and comment the rectangle component to right behavior*/
/*
ApplicationWindow{
height: 600
width: 800
*/
Rectangle {
id: rect
visible: true
Button{
id:open
height: 40
onClicked: fileDialog.open()
text: "open dialog"
}
FileDialog {
id: fileDialog
modality: Qt.ApplicationModal
title: "FileDialogTest"
nameFilters: [ "Zip Files (*.zip )", "All files (*)" ]
selectedNameFilter: "All files (*)"
selectFolder: false
onAccepted: {
console.log("Accepted")
}
onRejected: {
console.log("Rejected")
}
}
//}
Main.cpp
int main(int argc, char *argv[])
{
/*##### This block make fileDialog work properly #####*/
/*
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
*/
/*#### This block make openDialog work have weird behavior ####*/
/* Should be commented when the previous block is uncomment */
QApplication app(argc, argv);
QMainWindow mainWindow;
QQuickWidget *quickWidget = new QQuickWidget;
quickWidget->setMinimumSize(800, 600);
quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
quickWidget->setSource(QUrl("qrc:/main.qml"));
mainWindow.setCentralWidget(quickWidget);
mainWindow.show();
return app.exec();
}
I made a simple example of my problems it can be download by https://drive.google.com/file/d/1B_qtSZnigrLtDMHS0-84mOrFe5t2ZVIj/view?usp=sharing and can be run on QtCreator to illustrate my problem.
I am new in Qt. I am working on windows desktop application and using Qt with qml. On PC that hasn't QTouchDevices, splitter between component (element that allow you resize component on window) works good with mouse (screenshot "Good distance"), but if screen is touchscreen I have next problem, please look on screenshot "Wrong distance".
My application shouldn't support any touch device. So how to disable this Qt feature? I need the same behavior like on device without touch screen.
Wrong distance
Good distance
I have tried disable touch device using privet methods using next sample:
QWindowSystemInterface::unregisterTouchDevice(QTouchDevice::devices().first());
This works, but QWindowSystemInterface is private class and it disable touchscreen. And one more in QTCreator splitters work fine, exactly as I need.
If you can't patch Qt, the only way that I can think of would be to iterate through the children, searching for the MouseArea. For example, suppose you had this QML:
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
ApplicationWindow {
width: 600
height: 400
visible: true
property alias splitView: splitView
SplitView {
id: splitView
anchors.fill: parent
Rectangle {
width: 200
Layout.maximumWidth: 400
color: "lightblue"
Text {
text: "View 1"
anchors.centerIn: parent
}
}
Rectangle {
id: centerItem
Layout.minimumWidth: 50
Layout.fillWidth: true
color: "lightgray"
Text {
text: "View 2"
anchors.centerIn: parent
}
}
}
}
You could then print out the object tree of the SplitView like so:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QDebug>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *window = engine.rootObjects().first();
QQuickItem *splitView = window->property("splitView").value<QQuickItem*>();
splitView->dumpObjectTree();
return app.exec();
}
That gives you:
SplitView_QMLTYPE_1::
QQmlComponent::
QQuickSystemPalette::
QObject_QML_2::
QQmlComponent::
QQuickItem::
QQuickItem::
QQuickItem::
QQuickLoader_QML_3::
QObject_QML_4::
QQuickMouseArea_QML_5::
QQuickRectangle::
QQmlContext::
QQuickItem::
QQmlComponentAttached::
QQuickRectangle::
QQuickText::
QQuickLayoutAttached::
QQuickRectangle::
QQuickText::
QQuickLayoutAttached::
QQuickLayoutAttached::
QObject::dumpObjectTree() prints out metaObject->className(), so we know to look for an object whose metaObject has a className matching that:
Then:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QScreen>
#include <QWindow>
#include <QDebug>
QQuickItem *findMouseArea(QQuickItem *item)
{
foreach (QQuickItem *childItem, item->childItems()) {
if (QString(childItem->metaObject()->className()).startsWith(QStringLiteral("QQuickMouseArea_QML"))) {
return childItem;
} else {
QQuickItem *mouseArea = findMouseArea(childItem);
if (mouseArea) {
return mouseArea;
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QWindow *window = qobject_cast<QWindow*>(engine.rootObjects().first());
QQuickItem *splitView = window->property("splitView").value<QQuickItem*>();
QQuickItem *mouseArea = findMouseArea(splitView);
mouseArea->setProperty("defaultMargin", QVariant(window->screen()->physicalDotsPerInch() / 25.4));
return app.exec();
}
Apparently, Screen::pixelDensity is calculated using the physical dots per inch of the screen, divided by 25.4, so we copy that as well. You could use any other value there.
You will need to adapt the code should a second MouseArea ever be introduced, for example.
It's still largely dependent on private API, but it doesn't touch Qt code, at least.
Quick&Easy: enable/disable mouse and touch synthesizing
No need to patch things or doing any crazy tree lookups, just enable or disable mouse or touch synthesizing.
Qt::AA_SynthesizeTouchForUnhandledMouseEvents
Qt::AA_SynthesizeMouseForUnhandledTouchEvents
Just disable what you don't want, either mouse or touch to accept only authentic events.
This is my first post on StackOverflow so please excuse any formatting mistakes I might have made.
I'm building a Qt Quick UI project using Qt Quick Controls 1.1 and I have a simple Button in my QML code that I would like to call into my C++ action class. I see a number of examples on this with earlier versions of Qt, but they do not seem to work in 5.3. I chose Qt Quick Controls 1.1 in the project settings. I know this must not be too complicated to do, but I can't seem to find examples using QtCreator 5.3
Here is my main.qml file:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
ApplicationWindow {
id: parentWnd
visible: true
width: 640
height: 480
Action {
id: actionSend
onTriggered: console.log("SEND")
}
Button {
id: send
text: "Send Request"
action: actionSend
signal sendSignal()
}
}
Here is my main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
Here is the action class where I would like the QML button to call "doSend":
#include<QDebug>
#include<QObject>
class Action : public QObject
{
Q_OBJECT
public:
Action();
public slots:
void doSend();
};
Finally here is my project file:
TEMPLATE = app
QT += qml quick widgets
SOURCES += main.cpp \
action.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
action.h
When I run this, I see the button and I see the logging of "SEND" to the console so I know the QML Action is setup correctly. Any help on how to make the Action call into my action class would be much appreciated!
There are three issues that you're running into here.
The first is that you haven't registered your Action class with QML in main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Action>("StevesModule", 1, 0, "Action");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
The second is that Action is a QML type in the Qt Quick Controls module. It doesn't know anything about your Action class in C++. If you want to use your Action class instead of Qt Quick Controls' Action type, you must import it in your QML file:
import StevesModule 1.0
The third is that you're not calling the doSend() slot anywhere. You can do this in the onClicked handler of Button:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import StevesModule 1.0
ApplicationWindow {
id: parentWnd
visible: true
width: 640
height: 480
Action {
id: actionSend
}
Button {
id: send
text: "Send Request"
onClicked: actionSend.doSend()
}
}