Is there a way of accessing signals(such as clicked()) of a QML control such as a button, from c++. Assume that I have the memory address of that specific control. I just want to simulate a click event from c++ code.
Easy. You just create a slot in a C++ object that has a QObject base to it, make sure its registered as a QML type, then you "instantiate" it in the QML document, and connect the desired signal to the C++ object through QML using connect() and handle the logic from the C++ side.
Example:
I have a Rectangle I want to get the onWidthChanged signal from and use it in my class ShapeTracker which tracks when shapes change or whatever
in main.cpp:
#include "shapetracker.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
/* this is where you register ShapeTracker as a QML type that can be
accessed through the QML engine even though its a C++ QObject */
qmlRegisterType<ShapeTracker>("my_cpp_classes", 1, 0, "ShapeTracker");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
then in
main.qml
import QtQuick 2.6
/* import the C++ class you registered into this QML document */
import my_cpp_classes 1.0
Window {
visible: true
Item {
/* set up a parent item that has a signal which is exposed to
the ShapeTracker object and the Rectangle you want to track */
id: myRootItem
signal rectangleChanged(var newWidth)
/* Heres our special Rectangle from QML that will send a signal when
its width changes */
Rectangle {
id: specialRectangle
width: 250
/* send the signal rectangleChanged(width) from our parent item
root item whenever width changes
*/
onWidthChanged: function() { rectangleChanged(width); }
}
/* Special Button that when clicked enlarges the Rectangle but
10px */
Button {
id: mySpecialButton
onClicked: { click_handler(mouse); }
function click_handler(mouse): {
if (specialRectangle.width < 500)
specialRectangle.width += 10;
}
}
/* Heres the actual ShapeTracker instance, which exists
as a QObject inside of the QML context, but has its methods
which are declared in C++, and exposed to the QML engine */
ShapeTracker {
id: myShapeTracker
/* similar to a constructor, but more like a callback */
Component.onCompleted: {
/* connect our signal from the parent root Item called
"rectangleChanged" to the ShapeTracker's Slot called
"myRectangleChangeHandler" */
myRootItem.rectangleChanged.connect(myShapeTracker.myRectangleChangeHandler);
/* connect send_mouse_click to the click_handler of
the button */
myShapeTracker.send_mouse_click.connect(mySpecialButton.click_handler)
}
}
}
}
in shapetracker.h you simply add a new slot with the name myRectangleChangeHandler and it will receive that signal whenever it is send via QML to be processed via C++
class ShapeTracker : public QObject {
Q_OBJECT
public:
ShapeTracker(QObject *parent = 0 );
signal:
void send_mouse_click(QMouseEvent *event);
public slots:
void myRectangleChangeHandler(QVariant newWidth) {
/* Perform a mouse click on our QML Object mySpecialButton
using QQuickItem::mousePressEvent and sending it via
signal back to QML */
QMouseEvent myEvent(QEvent::MouseButtonPress, QPointF(1,1), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QMouseEvent* pressEvent = QQuickItem::mousePressEvent(&myEvent);
emit send_mouse_click(pressEvent);
}
};
In Summary, you expose a C++ QObject to QML, then you use
object.signal.connect(cppObject.desired_slot)
To connect them -- all the extra stuff was for a functional example in case anyone needs it later
In reality, you don't even need this functionality because anything happening in an onClick event could just as easily be put into any other property such on
Rectangle {
id: rect
signal customClick(var var1)
onCustomClick : { console.log(var1); }
}
Item {
rect.customClick(1);
}
The easy way would be to call all the receiving SLOTS manually. But that would be tedious and error prone.
You might try implementing a sub-class of QObject that has one slot onClicked() which emits the signal clicked() and use it as a shim between the button and elements controlled by the button. Connect the button clicked() to the new object onClicked() and then connect the new object to the original receivers. Then calling onClicked() would trigger the behavior.
This is a very simple example, and I haven't run it through the compiler.
ButtonShim.hpp
#include <QObject>
class ButtonShim : public QObject {
Q_OBJECT
public:
ButtonShim(QObject *parent = 0);
virtual ~ButtonShim();
public slots:
void onClicked();
signals:
void clicked();
};
ButtonShim.cpp
#include "ButtonShim.hpp"
ButtonShim::ButtonShim(QObject *parent) : QObject(parent) {
}
ButtonShim::~ButtonShim() {
}
void ButtonShim::onClicked() {
// All we do here is emit the clicked signal.
emit clicked();
}
SomeFile.cpp
#include <bb/cascades/Button>
#include "ButtonShim.hpp"
...
ButtonShim * pButtonShim = new ButtonShim(pButton); // pButtonShim will live as long as pButton
bool c = connect(pButton, SIGNAL(clicked()), pButtonShim, SLOT(onClicked()));
c = connect(pButtonShim, SIGNAL(clicked()), pSomeObject, SLOT(onButtonClicked()));
...
// to simulate a click of pButton
pButtonShim->onClicked();
SomeFile.qml
// assuming ButtonShim has been exposed to QML from your application
...
attachedObjects: [
ButtonShim {
id: buttonShim
onClicked: {
clickedLabel.text = "I've been clicked";
}
}
]
...
Label {
id: clickedLabel
text: "I haven't been clicked"
}
Button {
text: "Click Me"
onClicked: {
buttonShim.onClicked();
}
}
I think that you can look at code for tests. There they get object from QML file loaded into engine.
If you have an QObject you can just call signal because, AFAIR
public signals:
void clicked();
is expanded by moc into
public:
void clicked();
Related
Problem: A QML action must call a Q_INVOKABLE C++ function that takes some time to run.
When we do it the simple way: as soon as a user presses the Enter key, the method is called and the GUI freezes immediately while the code is executed, which is to be expected.
We'd like to update the GUI first (e.g. to display an hourglass) and then to execute the C++ method once the QML page has been updated.
(I imagine we've got to put a Qt event at the end of the Event Queue so it's processed after the others events that will update the QML page).
QML:
...
...
Keys.onEnterPressed: {
MyCplusplusClass.myInvokableMethod();
}
..
C++:
class MyCplusplusClass : public QObject
{
Q_OBJECT
...
Q_INVOKABLE void myInvokableMethod();
...
}
Now we want to run it asynchronously so the GUI won't freeze while it is executed. How is the best method to do that. I'll reproduce below the solution we're using, but I feel there is better to do (I'm not a Qt expert at all and would need some pointer).
QML:
...
property bool _displayHourglass;
...
Keys.onEnterPressed: {
_displayHourglass = true ;
MyCplusplusClass.asynchronousInvokableMethod();
}
...
...
Connections {
target: MyCplusplusClass
signal signalSynchronousMethodFinished
onSignalSynchronousMethodFinished: {
_displayHourglass = false ;
}
}
...
Image {
visible: _displayHourglass
anchors.centerIn: parent;
source: "qrc:/images/icons/hourglass.png"
}
C++:
class MyCplusplusClass : public QObject
{
Q_OBJECT
...
Q_INVOKABLE void asynchronousInvokableMethod() {
QTimer::singleShot(1, this, SLOT(synchronousMethod()));
}
...
private slots:
void synchronousMethod() {
...
emit signalSynchronousMethodFinished() ;
}
signals:
...
signalSynchronousMethodFinished();
...
}
What would be a better solution, please?
I'd prefer to avoid the solution with a worker thread, it's okay for the GUI to freeze if there is an hourglass displayed.
Scenario:
I have a Qt app which runs on Qt 5.9.4 commercial edition. Its a QtQuick and QML based application which is running on iOS and Android.
I have a QML item on the UI like this:
SomeItem {
text: qsTr("Some Item")
objectName: "someitem"
visible: false
onClicked: {
console.log("Some item was clicked")
}
}
I have a C++ function which can easily control the properties of SomeItem.
void MyCppClass::Func() {
QQuickItem *someItem = qml_engine->rootObjects()[0]->findChild<QQuickItem*>("someitem");
someItem->setVisible(true); // this works
// How to listen to someItem's onClick event here
}
Question:
I want to listen to the onClick event of someItem in a C++ method or a lambda without changing anything in the QML. Basically hook up to the onClick signal signal of someItem from C++ side itself. How can I do it?
The method to use to interact can be dangerous in the general case because the life cycle of the items depends on QML, so make sure it does not happen. Going to your request and I assume that MyCppClass inherits from QObject or a child class you must create a slot and use the old connection syntax:
*.h
class MyCppClass: public QObject
{
...
private slots:
void on_clicked();
...
};
*.cpp
void MyCppClass::Func() {
QQuickItem *someItem = qml_engine->rootObjects()[0]->findChild<QQuickItem*>("someitem");
if(!someItem)
return;
someItem->setVisible(true); // this works
connect(someItem, SIGNAL(clicked()), this, SLOT(on_clicked()));
}
void MyCppClass::on_clicked(){
qDebug()<<"on clicked";
}
In example code:
class MyWidget : public QWidget
{
Q_OBJECT
protected:
void mousePressEvent(QMouseEvent *event)
{
qDebug() << event;
event->accept();
QInputDialog::getText(NULL, "", "");
}
};
When I click Right mouse button on widget Input dialog appear on screen. After I click any button on dialog it closed and mousePressEvent call again and again and show dialog. If I click Left mouse button or Ctrl+Left Mouse button on widget all work fine.
This bug apper only on Mac OS (under Windows work fine).
Please help me to avoid this bug.
Those static/synchronous dialog functions always seemed a bit dubious to me -- they are implemented by recursively re-invoking the Qt event loop routine from within the getText() call, and so it's easy to get "interesting" re-entrancy issues when you use them. (For example, if some event in your program were to delete the MyWidget object before the user had dismissed the QInputDialog, then after QInputDialog::getText() returned, the program would be executing from within the mousePressEvent() method of a deleted MyWidget object, which is a situation that is just begging to cause undefined behavior)
In any case, my recommended fix is to avoid the static/synchronous getText() call and use signals instead, something like this:
#include <QWidget>
#include <QInputDialog>
#include <QMouseEvent>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget() : dialog(NULL) {}
~MyWidget() {delete dialog;}
protected:
void mousePressEvent(QMouseEvent *event)
{
event->accept();
if (dialog)
{
dialog->raise();
dialog->setFocus();
}
else
{
dialog = new QInputDialog;
connect(dialog, SIGNAL(finished(int)), this, SLOT(dialogDismissed()));
dialog->show();
}
}
private slots:
void dialogDismissed()
{
if (dialog)
{
int result = dialog->result();
QString t = dialog->textValue();
printf("Dialog finished, result was %i, user entered text [%s]\n", result, t.toUtf8().constData());
dialog->deleteLater(); // can't just call delete because (dialog) is what is calling this function (via a signal)!
dialog = NULL;
}
}
private:
QInputDialog * dialog;
};
I am developing a basic qml-cpp application to understand how one interacts with another. I have a MessageSetter C++ class and one main.qml. Since I wish to understand two-way communication, I exposed MessageSetter properties to qml using setContextProperty and also registered MessageSetter class with qml (instantiable registration). Exposed properties work fine. Now when a qml button is clicked, then the signal (qmlBtnClicked) is successfully caught in a MessageSetter slot(onQmlButtonClicked). This slot further emits another MessageSetter signal (colorChanged). This new (C++) signal should be caught in qml registered MessageSetter's signal handler (onColorChanged) but it does not arrive here in any case. Below is main.cpp code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MessageSetter>("com.SkillLotto.MessageSetter", 1, 0, "SetMessage");
MessageSetter data;
engine.rootContext()->setContextProperty("msgSetter", &data);
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
QObject *object = component.create()->findChild<QObject*>("setTextBtn");
QObject::connect(object, SIGNAL(qmlBtnClicked()), &data, SLOT(onQmlButtonClicked()));
return app.exec();
}
This is MessageSetter slot that emits another signal:
void MessageSetter::onQmlButtonClicked()
{
emit colorChanged("red");
}
This is qml code, this signal handler never gets called:
SetMessage{
onColorChanged: {
rect.color = color //rect is some rectangle in this file.
}
}
As I stated, qml signal is successfully caught in C++ slot but I am unable to catch this C++ signal in qml signal handler. Any help please.
This question, as I see, is focussed on qmlRegisterType() and should not be duplicate of this question? I also want to know whether qmlRegisterType() and setContextProperty() cant be used simultaneously or not ?
I think your code should work well.
I don't have the whole code so I don't know if you have the right methods implemented.
In order to get the signal using qmlRegisterType you need some requirements. Check if you have the Q_PROPERTY macro call implemented. Any property that is writable should have an associated NOTIFY signal that is emitted whenever the property value has changed.
If so, when you change the color property in the SetMessage component, the signal onColorChanged should be triggered.
Here you have an example where two signals are emitted: the first one when the size property is updated and the second one if the C++ mouseClick method is called using a MouseArea.
By the way, you don't need setContextProperty to integrate your C++ class with QML. qmlRegisterType should be enough. Or vice versa, depending on your needs. You can use both, but then you will have two different objects to work with. It really depends on what you want to achieve.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "customitem.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<CustomItem>("CustomItem", 1,0, "CustomItem");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
customitem.h
#ifndef CUSTOMITEM_H
#define CUSTOMITEM_H
#include <QObject>
class CustomItem: public QObject
{
Q_OBJECT
/*
* Any property that is writable should have an associated NOTIFY signal.
* Ref: http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties
*/
Q_PROPERTY(int size READ size WRITE setSize NOTIFY sizeChanged)
public:
CustomItem(QObject *parent = 0);
int size() const;
void setSize(int);
Q_INVOKABLE void mouseClick();
private:
int m_size;
signals:
void sizeChanged(int size);
void clicked();
public slots:
};
#endif // CUSTOMITEM_H
customitem.cpp
#include "customitem.h"
#include <QDebug>
CustomItem::CustomItem(QObject *parent)
: QObject(parent), m_size(0)
{
}
int CustomItem::size() const
{
return m_size;
}
void CustomItem::setSize(int size)
{
m_size = size;
emit sizeChanged(m_size);
}
void CustomItem::mouseClick()
{
qDebug() << "CustomItem::mouseClick()";
emit clicked();
}
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import CustomItem 1.0
Window {
visible: true
TextInput {
id: mySize
x: 0
y: 0
text: "100"
}
CustomItem {
id: customItem
size: mySize.text
onSizeChanged: console.log("size changed:", size)
onClicked: console.log("onClicked!")
}
Rectangle {
id: rect
x: 50
y: 50
width: customItem.size
height: customItem.size
color: "red"
MouseArea {
anchors.fill: parent
onClicked: { customItem.mouseClick() }
}
}
}
Because you are using two different instances of your MessageSetter, one is data in main.cpp and other is new instance SetMessage. Use only one to connect both signals/slots.
You are expecting onColorChanged signal from SetMessage but the signal is coming from data (in main.cpp).
Why do you need instantiable if you want to create a context property?
Add this in your main.qml file
Connections {
target: msgSetter
onColorChanged: {
console.log("received color changed signal");
}
}
I'm trying to change text of a class Label from another class. I have class MainWindow, which contains Label.
I also have a Bot class from which I wanna change the value of label.
I'm trying to create signal and slots but I have no idea where to start.
I created signal and slots like so:
//in mainwindow.h
signals:
void changeTextSignal();
private slots:
void changeText();
//in mainwindow.cpp
void MainWindow::changeText(){
this->label->setText("FooBar");
}
But I have no idea how to connect a signal to be able to change Label's text from another class.
Read up on Qt signal-slot mechanism. If I understand you correctly, you are trying to signal from Bot to MainWindow that the Label text needs to change. Here's how you do it...
//bot.h
class Bot
{
Q_OBJECT;
//other stuff here
signals:
void textChanged(QString);
public:
void someFunctionThatChangesText(const QString& newtext)
{
emit textChanged(newtext);
}
}
//mainwindow.cpp
MainWindow::MainWindow
{
//do other stuff
this->label = new QLabel("Original Text");
mybot = new Bot; //mybot is a Bot* member of MainWindow in this example
connect(mybot, SIGNAL(textChanged(QString)), this->label, SLOT(setText(QString)));
}
void MainWindow::hello()
{
mybot->someFunctionThatChangesText("Hello World!");
}