4

In C++ code below I load a qml file. But c++ slot not working. No error or warnings occurs but debug message is always "a" character. No "b" character appears. Can you tell me why? Thanks.

c++ side code

    ::::::::::::::
    view=new QDeclarativeView;
    view->setSource(QUrl("qml/groundMenu/Ground.qml"));
    rootObject = dynamic_cast<QObject*>(view->rootObject());
    QObject::connect(rootObject, SIGNAL(qmlSignal()),this, SLOT(qmlLoadedProcess()));
:::::::::::::::::
void MainWindow::qmlLoadedProcess()
{
    qDebug()<<"b";
}

//qml side code 
    Rectangle
    {
      signal qmlSignal()
      Component.onCompleted:
      {
         qmlSignal()
         console.log("a")
      }
    }
serkan gezer
  • 429
  • 1
  • 5
  • 8

1 Answers1

3

The problem is that the Component.onCompleted happens before you have called the QObject::connect(...). I.e. the Rectangle in qml is created when you call view->setSource(...) and the Component.onCompleted is called before you connect the signal to the slot. So the qmlLoadedProcess() never gets called.

JuliusG
  • 2,321
  • 21
  • 30
  • Thanks. But How can I achieve this? I need a signal when a qml file loaded. Can you tell me please? – serkan gezer Nov 01 '12 at 13:10
  • You can try to connect to [void QDeclarativeView::statusChanged ( QDeclarativeView::Status status )](http://doc.qt.digia.com/qt/qdeclarativeview.html#statusChanged) signal before you call the **setSource()** – JuliusG Nov 01 '12 at 13:14
  • Just a note: You can mark the question answered or vote it up if you find it helpful. – JuliusG Nov 01 '12 at 13:57