Answer the question
In order to leave comments, you need to log in
Why is connect not working?
Hello!
Task: to catch a signal in a QML file and then pass the values to the desired slot in the backend.
Everything works in one place of the project, but not in this one.
There is a button:
Button {
id: deleteNewsButton
text: "Del"
onClicked: {
mainWindow.deleteNews(id)
}
}
Rectangle {
id: mainWindow
visible: true
width: 800
height: 600
signal deleteNews(int news_id)
// ...
Component.onCompleted: {
mainWindow.deleteNews.connect(test)
}
function test(news_id) {
console.log("News ID: " + news_id)
}
public slots:
void deleteNews(int news_id);
QQuickView *view = new QQuickView;
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
QObject *object = view->rootObject();
QObject::connect(object, SIGNAL(deleteNews(int)), this, SLOT(deleteNews(int)));
// connect(object, SIGNAL(deleteNews(int)), this, SLOT(deleteNews(int))); // аналогичная ситуация
void MainWindow::deleteNews(int news_id) {
qDebug() << "DELETE " << news_id;
}
Answer the question
In order to leave comments, you need to log in
On the one hand, it is, of course, a pain to work in an application with a mess of QWidgets and QML. Surely you can not completely switch to QML? If not, then a working, albeit not a beautiful, solution is to make a singleton for working with signals, create it on the QML side and write a link to itself there as an instance.
This is how STDataProvider works here and here .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question