0
我有以下代碼:Qt的C++返回字符串值
filter.h
#pragma once
#include <QObject>
#include <QSortFilterProxyModel>
class FilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit FilterModel(QObject *parent = 0);
Q_INVOKABLE QString getText (QString text);
};
filter.cpp
#include "filter.h"
#include <QDebug>
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent) {}
QString FilterModel::getText(QString text)
{
QString qmltext = text;
qmltext != NULL ? qDebug() << qmltext
: qDebug() << "TEXT = NULL";
return qmltext;
}
的main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "abonentstable.h"
#include "filter.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
AbonentsSqlModel *abonentsSqlModel = new AbonentsSqlModel;
abonentsSqlModel->setQuery("SELECT * FROM abonents");
FilterModel *filterModel = new FilterModel;
filterModel->setSourceModel(abonentsSqlModel);
filterModel->setFilterKeyColumn(0);
filterModel->setFilterWildcard("9");
QQmlContext *context = engine.rootContext();
context->setContextProperty("abonents", filterModel);
context->setContextProperty("filter", filterModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
而.qml文件部分:
TextField {
id: textField
...
onTextChanged: {
filter.getText(textField.text)
}
...
}
方法getText()
從QML TextField
中獲取文本(突然顯示!)並將其打印到調試器中,它工作正常。但正如你所看到的,我有用於表格排序的代碼。 下面的問題是:現在排序面具是「9」,它的工作原理,但我需要從getText()
以某種方式返回QString qmltext
,放入filterModel->setFilterWildcard()
在main.cpp
這樣的:
QString qmlText = filterModel.getText(QString);
...
filterModel->setFilterWildcard(qmlText);
當然,這只是一個例子,它不起作用,我不知道如何做到這一點。