Connect a complex signal from QML to Qt/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Decode HTML entity names)
Line 19: Line 19:
  Q_OBJECT
  Q_OBJECT
public:
public:
  DeclarativeView(const QUrl & source) : QDeclarativeView(source)
  DeclarativeView(const QUrl & source) : QDeclarativeView(source)
  {}
  {}
  public slots:
  public slots:
Line 37: Line 37:


QDeclarativeItem '''item = qobject_cast<QDeclarativeItem'''>(view.rootObject());
QDeclarativeItem '''item = qobject_cast<QDeclarativeItem'''>(view.rootObject());
  QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &amp;view, SLOT (testSlot(QString, QString)));
  QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &view, SLOT (testSlot(QString, QString)));


view.resize(200,200);
view.resize(200,200);

Revision as of 17:00, 12 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

Deutsch | English

Komplexes Signal von QML nach C++ senden (QtQuick 1)

Der Code-Schnipsel demonstriert, wie man unter QtQuick 1 ein komplexes Signal von QML aus zu einem in C++ implementierten Slot sendet.

main.cpp

#include <QtGui>
#include <QtDeclarative>

class DeclarativeView : public QDeclarativeView
{
 Q_OBJECT
public:
 DeclarativeView(const QUrl & source) : QDeclarativeView(source)
 {}
 public slots:
 void testSlot(QString string1, QString string2)
 {
 qDebug() << string1 << string2;
 }
};

#include "main.moc"
int main(int argc, char **argv)
{
 QString file = "main.qml";

QApplication app(argc, argv);
 DeclarativeView view(QUrl::fromLocalFile(file));

QDeclarativeItem '''item = qobject_cast<QDeclarativeItem'''>(view.rootObject());
 QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &view, SLOT (testSlot(QString, QString)));

view.resize(200,200);
 view.show();
 return app.exec();
}

main.qml

import QtQuick 1.0

Item {
 width: 200; height: 300
 signal viewClicked(string first, string second)

MouseArea {
 anchors.fill: parent
 onClicked: viewClicked("first", "second")
 }
}