2

此示例应该从 TextInput 读取字符串并在单击鼠标时将其显示在另一个 Rectangle 中。但是,事实并非如此。为什么?

//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
            }
        }
    }
}

它发出错误:TypeError:表达式'data.setTeamName'[undefined]的结果不是函数。Main.qml:51:错误:无法将 [未定义] 分配给 QString

4

2 回答 2

0

我相信您的实际异常是由于以下行

text: data.setTeamName()

setTeamName 是数据对象上的私有函数,它不作为插槽公开,因此在调用并分配给文本时未定义。不要介意在执行任务时调用集合是没有意义的。我假设这应该是 data.getTeamName()。

于 2013-04-22T15:42:45.323 回答
0

teamName是 的一个属性data,所以只需使用赋值运算符,如data.teamName = 'some text';

可能您应该在喜欢的地方添加id字段TextInput

TextInput{
    id: textInput
    anchors.fill: text
    anchors.margins: 3
    focus: true
}

改变onClicked,所以

MouseArea{
   ...
    onClicked: {
        data.teamName = textInput.text
        copyText.text = data.teamName // copyText.text = textInput.text is fine too
    }
}
于 2013-04-19T19:06:18.170 回答