[Solved]qml can not recognize the c++ parameters with &
-
pass two arguments into the c++ api from qml site
when I add & on the api, the app can not recognize the signature
of the function..hpp
@
#ifndef TESTPARAM_HPP
#define TESTPARAM_HPP#include <QList>
#include <QObject>
#include <QUrl>class testParam : public QObject
{
Q_OBJECT
public:
explicit testParam(QObject *parent = 0);signals:
public slots:
void test(QList<QUrl> &first, QList<QUrl> &second)
{}
};
#endif // TESTPARAM_HPP
@
.cpp
@
#include "testParam.hpp"testParam::testParam(QObject *parent) :
QObject(parent)
{
}@
main.qml
@
import QtQuick 2.1
import QtQuick.Dialogs 1.0import Test 1.0
Rectangle {
width: 100
height: 62TestParam{ id: testParam } FileDialog{ id: fileDialog //selectFolder: param.selectFolder selectMultiple: true nameFilters: [ "Image files (*.bmp *.jpg *.JPEG *.png *.ppm *.tiff *.xbm *.xpm)" ] onAccepted: { testParam.test(fileDialog.fileUrls, fileDialog.fileUrls); } } MouseArea{ anchors.fill: parent onClicked: { fileDialog.open(); } }
}
@error message
Error: Unknown method parameter type: QList<QUrl>&
They are fine if
1 : strip the &
2 : reduce the parameters of "test" to a single parameter, that way you can preserve the &Anyway to pass the parameters as reference into the "test" with two parameters?
-
Data will be copied anyway, as JavaScript lists and QList are different. It does seem a bit strange, though, especially the part about using only one parameter.
Try usign QVariant, maybe? And then getting the data using QVariant::value<>().
-
[quote author="sierdzio" date="1372142579"]
Try usign QVariant, maybe? And then getting the data using QVariant::value<>().[/quote]QVariant can prevent the copy of the data?
Could you show me an example? -
It might, although it probably won't. I was rather thinking about allowing you to use 2 parameters.
-
Do we have a way to pass heavy data between qml and c++ without copy?
-
By using QObject pointers.
For me, const QString references and QLists of pointers also do work. I don't know the internals of the engine well enough to answer your question with confidence.
-
[quote author="sierdzio" date="1372144978"]By using QObject pointers.
For me, const QString references and QLists of pointers also do work. I don't know the internals of the engine well enough to answer your question with confidence.[/quote]
Could you show me some example?
-
- "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfgamemanager.h
- "link":https://github.com/sierdzio/closecombatfree/blob/master/src/logic/ccfenginehelpers.h
- and "link":https://github.com/sierdzio/closecombatfree/blob/master/src/logic/ccfscenariostate.h
Feel free to check out the whole project, it can easily be browsed in Qt Creator.
-
Thanks for your help.
Since a lot of heavy jobs would be done on c++ sites, I hope in the future we could
pass the parameters between c++ and qml by reference. -
Can't find a reasonable way to transform between string and QObject
so I try const&@
/**- @brief find out the different string of new_data compare with the old data
- @param old_data : the old data
- @param new_data : the new data
- @return the combination of the old data and the different string of new_data compare with the old data
- @example old_data = {1, 2, 3, 4}, new_data = {1, 3, 4, 5}, results = {1, 2, 3, 4, 5}
- @caution : qml site could not recognize the parameters with &(even it can, it still copy the parameters rather than take the reference)
-
pass the parameters by const& seems work, but I am not sure it would not copy the data by this way
*/
QList<QString> fileProcess::unique_strs(QList<QString> const &old_data, QList<QString> const &new_data)
{
//awkward solution and maybe meaningless, hope that in the future this could be solved
return set_difference_lazy(const_cast<QList<QString>&>(new_data), const_cast<QList<QString>&>(old_data));
}
@ -
After some experiment, the last post of my solution is useless, the data of
qml and c++ are always copy