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
Related
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.
Here's my situation. I'm trying to combine qml with a mostly widget based UI. To do this, I'm using QQuickView with QWidget::createWindowContainer. I can't use QQuickWidget, because I need to convert the window into a native window, and QQuickWidget doesn't like that. But back to the issue.
My problem is that the first time the view is displayed, it takes like half a second to load causing a very obvious flicker. After that I can hide/show the view all I want, it displays immediately. It's only the first time the qml loads. And I'm fairly certain it's the loading of the qml that causes the issue. Because I have two different QQuickViews that get the same qml as their source. But after any one of them loads once, the other has no issues displaying instantly.
I tried to call show() on view early to get it to load in time. But this causes the qml to appear for a brief moment before any of the widgets get displayed.
Has anyone encountered a similar issue? How can I get the QQuickView to behave.
Edit: I'm using Qt 5.4.2, and I can't update to a newer version due to various reasons.
I was going to say that you can use the same approach as in this answer, but it seems that even that is too early to being loading the QML. It's hacky, but the only other thing I can think of is using a very short Timer:
main.cpp:
#include <QtWidgets>
#include <QtQuick>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0) :
QMainWindow(parent)
{
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setFocusPolicy(Qt::TabFocus);
view->rootContext()->setContextProperty("window", view);
view->setSource(QUrl("qrc:/main.qml"));
setCentralWidget(container);
resize(400, 400);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
main.qml:
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 2.0
Item {
anchors.fill: parent
// Connections {
// target: window
// onAfterSynchronizing: loader.active = true
// }
Timer {
running: true
repeat: true
interval: 50
onTriggered: {
loader.active = true
}
}
Loader {
id: loader
active: false
sourceComponent: Column {
anchors.fill: parent
Repeater {
model: 30000
delegate: Button {
text: index
}
}
}
}
BusyIndicator {
running: loader.status === Loader.Null
anchors.centerIn: parent
}
}
For brevity, I chucked the "heavy" QML into sourceComponent, but you can also use the source property to point to a URL.
BusyIndicator runs its animation on the render thread, so it can continue to spin while the GUI thread is blocked.
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.
I've noticed a new DropArea component in Qt5. I'm trying to drag a file from Finder (Mac) but only onEntered method is called.
import QtQuick 2.0
Rectangle {
id: background;
color: "white";
width: 300;
height: 300;
DropArea {
id: dropArea;
anchors.fill: parent;
onEntered: {
background.color = "gray";
drag.accept (Qt.CopyAction);
console.log("onEntered");
}
onDropped: {
console.log ("onDropped");
}
onExited: {
bckground.color = "white";
console.log ("onExited");
}
}
}
and here is window creation code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickView qmlView;
qmlView.setGeometry(0, 200, 600, 400);
qmlView.setResizeMode (QQuickView::SizeRootObjectToView);
qmlView.setSource(QUrl::fromLocalFile("/Users/ivann/Projects/QtGuiTestApp/testView.qml"));
qmlView.show();
return a.exec();
}
Am I missing something?
Seems to be a Mac-specific issue (it wokrs as expected on Linux at least). Filled a bugreport to Nokia: https://bugreports.qt.io/browse/QTBUG-27125
As stated in the link attached by chebum that feature is not supported by QtQuick on any platform at the time of this writing.
Only posting for letting it know to future readers.
QtQuick 5.2 supports drag and drop from external applications. See the example http://qt-project.org/doc/qt-5/qtquick-externaldraganddrop-example.html