C++ QString通过c+在qml中交换+;
本例假定从TextInput读取字符串,并在单击鼠标时将其显示在另一个矩形中。然而,事实并非如此。为什么?C++ QString通过c+在qml中交换+;,c++,qml,C++,Qml,本例假定从TextInput读取字符串,并在单击鼠标时将其显示在另一个矩形中。然而,事实并非如此。为什么? //mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QDeclarativeView> #include <QDeclarativeContext> #include <QObject> #include <QString> class MainWindow
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QObject>
#include <QString>
class MainWindow : public QDeclarativeView
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(QString teamName READ getTeamName WRITE setTeamName NOTIFY nameChanged)
public:
Data(QObject *parent = 0);
~Data();
public:
QString getTeamName();
void setTeamName(QString &);
signals:
void nameChanged();
private:
QString n_teamName;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QString>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QDeclarativeView(parent)
{
}
MainWindow::~MainWindow()
{
}
Data::Data(QObject *parent) : QObject(parent)
{
//n_teamName = "Ahoj";
}
Data::~Data(){
}
QString Data::getTeamName(){
return n_teamName;
}
void Data::setTeamName(QString &newName){
if(newName != n_teamName){
n_teamName = newName;
emit nameChanged();
}
}
//main.cpp
#include "mainwindow.h"
#include <qdeclarative.h>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Data d;
qmlRegisterType<Data>("NData", 1, 0, "Data");
w.rootContext()->setContextProperty("data", &d);
w.setSource(QUrl::fromLocalFile("../klik/Main.qml"));
w.show();
return a.exec();
}
//main.qml
// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import NData 1.0
Rectangle {
id: root
width: 600
height: 400
Rectangle{
id: text
...
TextInput{
anchors.fill: text
anchors.margins: 3
focus: true
}
}
Rectangle{
...
Text{
id: copyText
anchors.centerIn: copy
text: data.setTeamName()
}
}
Rectangle{
id: klik
...
MouseArea{
...
onClicked: {
copyText.text = data.teamName
}
}
}
}
//mainwindow.h
#ifndef主窗口
#定义主窗口
#包括
#包括
#包括
#包括
类主窗口:公共QDeclarativeView
{
Q_对象
公众:
主窗口(QWidget*父窗口=0);
~main窗口();
};
类数据:公共QObject
{
Q_对象
Q_属性(QString teamName读取getTeamName写入setTeamName通知名称更改)
公众:
数据(QObject*parent=0);
~Data();
公众:
QString getTeamName();
无效设置名称(QString&);
信号:
void nameChanged();
私人:
QString n_teamName;
};
#endif//main窗口
//mainwindow.cpp
#包括“mainwindow.h”
#包括
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口)
:QDeclarativeView(父级)
{
}
MainWindow::~MainWindow()
{
}
数据::数据(QObject*父对象):QObject(父对象)
{
//n_teamName=“Ahoj”;
}
数据::~Data(){
}
QString数据::getTeamName(){
返回n_teamName;
}
void数据::setTeamName(QString和newName){
if(newName!=n_teamName){
n_teamName=新名称;
emit nameChanged();
}
}
//main.cpp
#包括“mainwindow.h”
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
数据d;
qmlRegisterType(“数据”,1,0,“数据”);
w、 rootContext()->setContextProperty(“数据”、&d);
w、 setSource(QUrl::fromLocalFile(“../klik/Main.qml”);
w、 show();
返回a.exec();
}
//main.qml
//将QtQuick 1.0//导入目标S60第5版或Maemo 5
导入QtQuick 1.1
导入NData 1.0
长方形{
id:根
宽度:600
身高:400
长方形{
id:文本
...
文本输入{
锚定。填充:文本
1.2.3页边距:3
焦点:正确
}
}
长方形{
...
正文{
id:copyText
anchors.centerIn:复制
text:data.setTeamName()
}
}
长方形{
id:klik
...
鼠耳{
...
再次点击:{
copyText.text=data.teamName
}
}
}
}
它发出错误:TypeError:表达式“data.setTeamName”[undefined]的结果不是函数。
Main.qml:51:错误:无法将[undefined]赋值给QString
teamName
是data
的属性,因此只需像data.teamName='some text'这样使用赋值运算符即可代码>
您可能应该将id
字段添加到TextInput
中
TextInput{
id: textInput
anchors.fill: text
anchors.margins: 3
focus: true
}
然后单击更改,这样
MouseArea{
...
onClicked: {
data.teamName = textInput.text
copyText.text = data.teamName // copyText.text = textInput.text is fine too
}
}
我相信你真正的例外是由于以下几行
text: data.setTeamName()
SettamName是数据对象上的一个私有函数,它不作为插槽公开,因此在调用和分配给文本时未定义。无论在执行赋值时调用集合是没有意义的。我假设这应该是data.getTeamName()。谢谢您的回答,但一定是出了什么问题。我已经尝试过了,但是文本没有被复制,错误是:无法将[undefined]分配给QStringPlease check,您尝试从中复制的对象确实存在,或者显示您具体尝试执行的操作。