Signal and Slot between QML and c++ - c++

my problem is, that I can't get a signal and slot connections between a cpp and a qml file.
First of all I've found some solutions in the web, but it doesn't work.
I'm sure, that the mistake is mine, but I didn't find it.
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "Hotfolder.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QScopedPointer<cReadJson> jsonReader(new cReadJson);
QScopedPointer<cHotfolder> hotfolder(new cHotfolder);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("jsonReader", jsonReader.data());
engine.rootContext()->setContextProperty("hotfolder", hotfolder.data());
QObject *topLevel = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
QObject::connect(&cHotfolder, SIGNAL(sigNewOrder()), window, SLOT(
// Here is the mistake, that I can't find the Slot in QML
return app.exec();
}
This is my main.cpp file. In this file I found the Signal from the cpp file, but not the Slot in QML.
main.qml:
function bla()
{
console.log("bla")
}
This is the function in my main.qml file.
So where ist mistake?
Many thanks in advance!
Ben

The solution is:
Just connect to the signal from within QML and not from C++
example:
Component.onCompleted: hotfolder.sigNewOrder.connect(bla)

Related

Using QT; QWidget: Must construct a QApplication before a QWidget

I am using vtk QT, i've installed VTK, PCL libraries well, after run the project the realese mode; i found the following error.
QWidget: Must construct a QApplication before a QWidget
As the error message says, you will need to create a QApplication before you can instantiate and use a QWidget. For example:
#include <QApplication>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog dialog;
dialog.setModal(true);
dialog.show();
return app.exec();
}

Using a Designer .ui File in Your Application example not working

I'm trying to build a UI form and interface to it using the example from:
https://doc.qt.io/qt-5/designer-using-a-ui-file.html
#include "ui_item.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget widget;
Ui::Item ui;
ui.setupUi(&widget);
widget.show();
return app.exec();
}
However, it just doesn't work. In the line
ui.setupUi(&widget); I get the error:
invalid conversion from ‘QWidget*’ to ‘QFrame*’
Is there something I'm missing? The top-level widget in Ui::Item is a QFrame. I'm using Qt 5.12 and Qt-Creator 4.12.4.
The issue was that my UI top level was a QFrame and it needed to be a QWidget. For whatever reason, the UI file generated as a QFrame.

Open widget in another window on macOS

The app opens another widget as a pane. I guess this follows macOS guidelines but is it possible to force widget to open in another window?
Like this:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("Main Widget");
w.show();
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.show();
return a.exec();
}
To do that, you need to set the flag of the second widget to Qt::Dialog
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.setWindowFlag(Qt::Dialog);
anotherWidget.show();
Open the anotherWidget as a QMainWindow type instead of QWidget.

Creating a QVideoWidget in Qt5

I have the following piece of code:
#include <QtWidgets/QtWidgets>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QMediaPlayer>
int main(int argc, char * argv[])
{
QApplication testQt(argc, argv);
QMainWindow w;
QWidget videoContainer(&w);
w.setCentralWidget(&videoContainer);
QVideoWidget videoWidget(&videoContainer);
QCamera cam(&w);
cam.setViewfinder(&videoWidget);
cam.start();
w.show();
return testQt.exec();
}
in which I am trying to create a main window, create a container widget to display video, create a videowidget in that container, and then finally set the viewfinder of the camera to that videowidget. However, when I try to do this I get the error
Variable has incomplete type 'QVideoWidget'
Why am I getting this error?
You need to include the corresponding header as follows:
#include <QVideoWidget>
You may also need to add this to your project file:
QT += multimediawidgets

Qt Error: Signal QDeclarativeEngine::quit() emitted, but no receivers connected to handle it

I am very new to Qt so that some Qt issues I can't figure out. I will really appreciate if somebody can help me.
I am trying to get rid of application window's frame and create an exit button in qml in order to exit the application. Hence, I make a program as following:
main.cpp
#include <QtGui/QApplication>
#include <QDeclarativeContext>
#include <QObject>
#include "qmlapplicationviewer.h"
#include "myclass.h"
#include "mainwindow.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyClass myClass;
MainWindow window;
window.rootContext()->setContextProperty("myObject", &myClass);
window.show();
return app.exec();
}
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QDeclarativeView(parent)
{
// No window decorations
setWindowFlags(Qt::FramelessWindowHint);
// Set QML file
setSource(QUrl::fromLocalFile("qml/Test2/main.qml"));
}
// Destructor.
MainWindow::~MainWindow()
{
}
myclass.cpp
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <stdio.h>
#include "myclass.h"
MyClass::MyClass()
{
click_count = 0;
}
int MyClass::click_function(void)
{
click_count++;
fprintf(stderr, "CLICK COUNT in CPP: %d\n", click_count);
return click_count;
}
qml
MouseArea {
id: mouse_exit
anchors.fill: parent
onClicked: {
console.log("Click on exit button: ")
console.log("click count: ", myObject.click_function())
Qt.quit();
}
}
It compiles successfully, however, whenever I click on the exit button, the Qt error "Signal QDeclarativeEngine::quit() emitted, but no receivers connected to handle it" occurs. Based on my searching online for this issue, it seems like I have to connect the QDeclarativeEngine::quit() signal to the QApplication::quit() slot. But there isn't too much information for using connect() function. I tried many ways to use connect(), but I still couldn't know how to use it for this case.
Can anybody help me solve this problem? I will really really appreciate!
The syntax is:
connect(sender, SIGNAL(signalName(args)), receiver, SLOT(slotName(args)));
You can put it in your MyClass constructor:
connect(this, SIGNAL(quit()), qApp, SLOT(quit()));
Or in the main function, since connect() is a static function, as Slavic81 pointed out in the comment below.
Note that qApp is a macro for the global application.