A
A
Anton2015-12-28 09:50:53
Qt
Anton, 2015-12-28 09:50:53

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)
   }
}

The beginning of my "rectangle":
Rectangle {
    id: mainWindow
    visible: true

    width: 800
    height: 600

    signal deleteNews(int news_id)

    // ...

By the way, this code quietly displays the ID in the console:
Component.onCompleted: {
    mainWindow.deleteNews.connect(test)
}

function test(news_id) {
    console.log("News ID: " + news_id)
}

That is, like, everything works in this part.
Let's go to the backend.
Slot:
public slots:
    void deleteNews(int news_id);

And finally connect:
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;
}

As a result, nothing is written to the console. This connect seems to be ignored altogether.
What am I doing wrong? Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2015-12-28
@Zifix

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 question

Ask a Question

731 491 924 answers to any question