I have two QML files in my project.
main.qml:
Window{
width: 800
height: 500
visible: true
id:mainWinenter
Item {
id: inlinecomponent
Rectangle {
id: display
width: 50; height: 50
color: "blue"
}
}
MouseArea {
anchors.fill: parent
onClicked: {
var component = Qt.createComponent("qrc:/Test.qml");
var object = component.createObject(inlinecomponent, {"x": 50, "y": 50});
}
} }
and my Test.qml file:
Item{
id:mc
Rectangle{
x: 0
y: 0
id: rec
width: 150; height:75
color: "grey"
objectName: "recc"
visible: true
}}
main.cpp:
int main(int argc, char *argv[]){
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQuickView view;
view.setSource(QUrl("qrc:/Test.qml"));
QQuickItem *object = view.rootObject();
QObject *rect = object->findChild<QObject*>("recc");
if (rect)
rect->setProperty("color", "red");
view.show();
return app.exec();}
In main.cpp I use setProperty function to change the color of rectangle in Test.qml file. The color changes when I load the file in new window but it doesn't change when it loads in main.qml file. How I can change it when it loads in main.qml?
My output contain two windows. One for main.qml and one for Test.qml.
this is my main.qml output
this is my Test.qml output
The color of the rectangle in my Test.qml window is red but it loads grey in my main.qml. I want to load it red in main file and I want to do this with my cpp code.
Related
the code below shows two C++ QStringListModels.
"model" is integrated into the main.qml file as "myModel" and "model2" is integrated into the windowLoader.qml file as "myModel2".
The View of "MyModel2" is loaded when you click on an item of the View of "myModel".
My Question:
If there would be a "myModelX", is it possible to change the model property from windowLoader.qml which has the value "myModel2" or do I have to write a windowLoader.qml for every Model?
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStringListModel>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QStringListModel model;
model.setStringList( QStringList() << "foo" << "bar" << "baz" );
QStringListModel model2;
model2.setStringList( QStringList() << "a" << "b" << "c" );
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
context->setContextProperty( "myModel", &model );
context->setContextProperty( "myModel2", &model2);
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();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("StringList")
Loader {
id: windowLoader
}
ListView {
anchors.fill: parent
model: myModel
delegate: Rectangle {
id: rec1
property alias text: text_field.text
color: "green"
border.color: "lightgreen"
radius: 8
width: 100
height: 20
MouseArea {
anchors.fill: parent
onClicked: windowLoader.setSource( "qrc:/windowLoader.qml",
{ "title_text":
rec1.text } )
}
Text {
id:text_field
anchors.centerIn: parent
text: display
}
}
}
}
windowLoader.qml
import QtQuick 2.0
import QtQuick.Window 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr(title_text + "-Window")
property string title_text
ListView {
anchors.fill: parent
model: myModel2
delegate: Rectangle {
color: "blue"
border.color: "lightsteelblue"
radius: 8
width: 100
height: 20
Text {
anchors.centerIn: parent
text: display
}
}
}
}
I use open source qt5.9 for an embedded device.
I wanna to use virtual keyboard in my qml project. I know I should add a static link in .pro file like :
static {
QT += svg
QTPLUGIN += qtvirtualkeyboardplugin
}
and also add
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
to main.cpp file to use the virtual keyboard. but my virtual keyboard does not fire when I click on my text object:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.VirtualKeyboard 2.1
Window {
visible: true
width: 640
height: 480
TextInput {
id: textInput;
text:"ssssss"
height: 120;
width: parent.width - 2;
anchors.bottom: keyboard.top
color: "#000000"; // black
// http://doc.qt.io/qt-5/qinputmethod.html#properties
focus: Qt.inputMethod.visible;
verticalAlignment: TextInput.AlignVCenter;
}
}
It solved.
just put the kind of input for each lineedit . like this :
InputPanel{
id:inputpanel
visible:active
y:active?parent.height - inputpanel.height : parent.height
anchors.left: parent.left
anchors.right: parent.right
}
TextInput{
id:input
inputMethodHints: Qt.ImhDigitsOnly
focus: Qt.inputMethod.visible;
text: "123211"
}
TextInput{
id:input2
anchors.top:input.bottom
inputMethodHints: Qt.ImhLowercaseOnly
focus: Qt.inputMethod.visible;
text: "123211"
}
I am trying to build a simple media player in QML . I can't use QFile Dialog as its a single window application running on EGLFS . I managed so far to build a simple file browser for QML and can play a mp3 from a fixed location. But here is where I am stuck:
How do I set the current selected file from the treeview as source for my audio ?
How can I get the Audio play each file with file ending mp3 from the selected folder?
Thanks for your help
.pro file
QT += qml quick multimedia widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
DEFINES += QT_DEPRECATED_WARNING
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml>
#include <QFileSystemModel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("Power-Tune");
app.setOrganizationDomain("power-tune.org");
app.setApplicationName("PowerTune");
QQmlApplicationEngine engine;
//
QFileSystemModel model;
model.setRootPath("/");
engine.rootContext()->setContextProperty("my_model", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
my main.qml :
import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtMultimedia 5.8
ApplicationWindow {
visible: true
width: 800
height: 480
minimumWidth: 800
minimumHeight: 480
title: qsTr("PowerTune ")
// visibility: "FullScreen"
color: "black"
Rectangle {
width: parent.width
height: parent.height
property bool playing: false
Audio {
id: playMusic
//source: currently selected file in TreeView
}
Button {
id: previous
text: "previous"
width: 100
anchors.right: playpause.left
// onClicked: select previous item in current folder
}
Button {
id: playpause
text: "play/pause" //playing ? "Stop music" : "Start music"
width: 100
anchors.right: next.left
onClicked: {
if(playing == true) {
playMusic.stop()
playing = false
} else {
playMusic.play()
playing = true
}
}
}
Button {
id: next
text: "next"
width: 100
anchors.right: parent.right
}
TreeView {
id:mp3selector
width: parent.width/2
height: parent.height
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
model: my_model
}
}
}
Couple of notes:
property bool playing: false
Properties are better to be defined at top of hierarchy so that they accessible in all children, so prefer to put it directly in ApplicationWindow not in Rectangle element.
And, I think TreeView is not a suitable choice (Its a Quick Controls 1.0 element not available in Quick Controls 2.0 , and for mixing check this post);
You can do the model directly from QML, using a ListView with FolderListModel, you only need an additional of decorations for highlighting and selecting of files with mouse .. that's all, TreeView could be replaced with below code, it works and fancy!
...
import QtQuick.Controls 2.2
import Qt.labs.folderlistmodel 2.1
...
ListView {
id: list
width: parent.width/2
height: parent.height
model: folderModel
onCurrentIndexChanged: {
// This will handle changing playlist with all possible selection methods
playMusic.source = folderModel.get(currentIndex, "fileURL")
}
FolderListModel {
id: folderModel
folder: "file:///MP3/"
showDirs: false
nameFilters: ["*.mp3"]
}
delegate: Component {
Item {
width: parent.width
height: 40
Column {
Text { text: fileName }
}
MouseArea {
anchors.fill: parent
onClicked: {
list.currentIndex = index
}
}
}
}
highlight: Rectangle {
color: 'grey'
}
focus: true
}
For your buttons onClicked just handle currentIndex of the ListView , for example in the next button add:
onClicked: list.currentIndex += 1
you also need to add some code to avoid getting index out of range or -1 ...etc.
I have this code in Qt
int main(int argc, char *argv[]) {
QGuiApplication app(argc,argv);
QQuickView view;
QUrl q(QStringLiteral("QML:///places_map.qml"));
view.setSource(QUrl(QStringLiteral("qrc:///places_map.qml")));
//I've tried this to init qml properties
QObject *object = view.rootObject();
object->setProperty("latitude", 48.4656371);
object->setProperty("longitude", 31.04900455);
QQuickItem *item = view.rootObject();
item->setProperty("device_latitude", 48.4656371);
item->setProperty("device_longitude", 35.04900455);
}
And my qml file:
import QtQuick 2.0
import QtPositioning 5.5
import QtLocation 5.6
Rectangle {
width: 720
height: 480
property double latitude: 0
property double longitude: 0
property double device_latitude: 0 //48.4656371
property double device_longitude: 0 //35.54900455
property variant location: QtPositioning.coordinate(latitude, longitude)
property variant deviceLocation: QtPositioning.coordinate(device_latitude, device_longitude)
Plugin {
id: myPlugin
name: "osm"
}
PlaceSearchModel {
id: searchModel
plugin: myPlugin
searchTerm: "Pizza"
searchArea: QtPositioning.circle(deviceLocation)
Component.onCompleted: update()
}
Map {
id: map
anchors.fill: parent
plugin: myPlugin;
center: location
zoomLevel: 13
MapItemView {
model: searchModel
delegate: MapQuickItem {
coordinate: deviceLocation //QtPositioning.coordinate(device_latitude, device_longitude)
anchorPoint.x: image.width * 0.5
anchorPoint.y: image.height
sourceItem: Column {
Image { id: image; source: "marker.png" }
Text { text: title; font.bold: true }
}
}
}
}
}
In qml code properties double latitude and longitude set the view on map
but map shows place with latitude = 0 and longtitude = 0
If I set the correct coordinates in qml code everything works
How can I init this value from c++ code so that map will show my city ?
Using Q_PROPERTY would be better. Exposing Attributes of C++
I have it working like this:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
QQuickItem *item = view.rootObject();
item->setProperty("number", 22.002);
view.show();
return app.exec();
}
main.qml
import QtQuick 2.4
import QtQuick.Window 2.2
Rectangle{
visible: true
width: 640
height: 480
property double number : 0
Text {
id: textEdit
text: number
verticalAlignment: Text.AlignVCenter
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 20
}
}
I am a newer to Qt. I wanna develop a frameless application. But AreaDrop in QML does not work with the frameless application in Windows.
my code:
main.qml
import QtQuick 2.5
Item {
visible: true
width: 640
height: 480
Rectangle {
anchors.fill: parent
width: 640
height: 480
color: "green"
DropArea {
anchors.fill: parent
width: 640
height: 480
onEntered: {
console.log("drop enter")
}
onDropped: {
console.log("droped");
}
}
}
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQuickView viewer;
viewer.setSource(QUrl("qrc:/main.qml"));
viewer.setFlags(Qt::FramelessWindowHint);
viewer.setColor(QColor(Qt::transparent));
viewer.show();
return app.exec();
}
This code works fine in the Ubuntu. But in Windows, the file dropping is unacceptable. I cannot find a solution to resolve this problem.
So, how can I drag a file into the frameless application in Windows.
Thanks!!
You should set QWindow::flags as set of window flags and (optionally) a set of hints but not only hints. So you have to change
viewer.setFlags(Qt::FramelessWindowHint);
to
viewer.setFlags(Qt::Window | Qt::FramelessWindowHint);
But since you build your GUI in QML I advice you to do that in some way:
Window {
flags: Qt.Window | Qt.FramelessWindowHint
...
}
And btw avoid setting size since you use anchoring.