• 4279阅读
  • 10回复

[提问]数据传递问题 [复制链接]

上一主题 下一主题
 

只看楼主 倒序阅读 楼主  发表于: 2011-10-23
关键词: 数据传递
我现在有一个ZzWorldClient.cpp,有一个函数
void ZzWorldClient::getaddress(const QString &address)
{
           label2->setText(address);
}
能返回一个address。
但是我现在想把这个address交给另外一个.cpp(showshare.cpp)函数,这两个cpp分别属于class ZzWorldClient和class showshare。我该怎么做才能让ZzWorldClient把address传给showshare呢?
大侠,help!!!
不胜感激!!

只看该作者 1楼 发表于: 2011-10-23
离线jdwx

只看该作者 2楼 发表于: 2011-10-23
两个方案:
1,class ZzWorldClient 里添加一个信号 sendAddress(const QString &address),
       在 label2->setText(address);下面添加,emit sendAddress(address);
      在showshare里添加一个槽,剩下的就不用说了吧。
2,showshare里添加一个函数setAddress(const QString&address);
      在 label2->setText(address);下面添加,
       showshare    *ss = 获得showshare对象的指针的函数;
      ss->setAddress(address);
推荐方案1
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。

只看该作者 3楼 发表于: 2011-10-23
回 2楼(jdwx) 的帖子
我用第一种方法,第一次把 connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));放在ZzWorldClient.cpp里,运行时报内存不能read。
showshareDialog在ZzWorldClient.h里声明了:
private:
    ClientSocket client;   //一个Socket类
    Login *loginDialog;  //login类
    RegisterDialog *registerDialog;//
    showshare *showshareDialog;
把connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));放在showshare.cpp又说没有这个type;
大侠,再指点下小弟!!不胜感激,都调试了两天了,就卡在这里。

只看该作者 4楼 发表于: 2011-10-23
我这个功能是双击某个用户,打开该用户设置的共享目录,所以,双击的时候,先用你刚刚说得第一种方案去getaddress取回该用户的ip,想当双击后,将该用户的ip传给showshare.cpp这样才能在showshare里显示该用户的共享目录。开始我以为是由于showshare还没打开就连接它的槽,所以地址越界,才报错,但是上面负责服务器端和客户机端通行的 ClientSocket ;
(ClientSocket client;   //一个Socket类)它在ZzWorldClient.cpp也connect很多个,也没事啊!
void ZzWorldClient::makeConnections()
{
    connect(sendPushButton,SIGNAL(clicked()),
            this,SLOT(sendMessage()));  //1 2
    connect(&client,SIGNAL(connected()),
            this,SLOT(showConnectedInLabel()));

    connect(&client,SIGNAL(connected()),
            this,SLOT(showuser()));


    connect(&client,SIGNAL(disconnected()),
            this,SLOT(showDisconnectedInLabel()));
    connect(&client,SIGNAL(addNewComerToNameList(const QString &,const QHostAddress &)),
            this,SLOT(addNewComerToNameList(const QString &,const QHostAddress &)));
    connect(&client,SIGNAL(showMessageInTextEdit(const QString &,const QString &,const QString &)),
            this,SLOT(showMessageInTextEdit(const QString &,const QString &,const QString &)));//3 4
    connect(&client,SIGNAL(nameAndPasswordIsRight()),
            this,SLOT(nameAndPasswordIsRight()));
    connect(&client,SIGNAL(nameAndPasswordIsNotRight()),
            this,SLOT(nameAndPasswordIsNotRight()));
    connect(&client,SIGNAL(removeLeaverNameFromNameList(const QString &)),
            this,SLOT(removeLeaverNameFromNameList(const QString &)));
    connect(&client,SIGNAL(registerSuccess()),
            this,SLOT(registerSuccess()));
    connect(&client,SIGNAL(registerFailed()),
            this,SLOT(registerFailed()));
    connect(&client,SIGNAL(getaddress(const QString &)),this,SLOT(getaddress(const QString &)));
    connect(nameListView,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(getuseraddress()));
    connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));
后来把写成这样:
    showshareDialog=new showshare(this);
    showshareDialog->show();
    label2->setText(address);
    connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));
    emit sendAddress(address);
这次    connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));这行没报错了,但是    emit sendAddress(address);
报错。
    系统是好的,我试过在showshare里制定个ip给它,显示都正常,现在时ip传不过去。郁闷的!!!!

只看该作者 5楼 发表于: 2011-10-23
那么多问题都解决了,就差这个就功德圆满了,真不甘心为山九刃,功亏一篑啊!大侠,在帮帮我一下!指针,你说的第二种方案,我怕到时一不小心,系统很容易就垮了!!!大侠!!!感激不尽!!

只看该作者 6楼 发表于: 2011-10-23
    inline T *data() const
    {
        return d;
    }
调试到emit sendAddress(address);报错的时候,它指向这里。(当改成 showshareDialog=new showshare(this);
    showshareDialog->show();
    label2->setText(address);
    connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));
    emit sendAddress(address);
时)
离线jdwx

只看该作者 7楼 发表于: 2011-10-24
回 6楼(键盘爱鼠标) 的帖子
首先必须先new出对象,再connect,要不然connect到哪里了,当然是没有object的地方了。
肯定会出错误,引用无效对象。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线zhy282289
只看该作者 8楼 发表于: 2011-10-24
connect(this,SIGNAL(sendAddress(const QString &)),showshareDialog,SLOT(sendAddress(const QString &)));
你确定你的address是成员变量或全局变量?
你不要用&啊..试试
connect(this,SIGNAL(sendAddress(const QString )),showshareDialog,SLOT(sendAddress(const QString)));
为什么我脸这么胖~
离线ifantasy
只看该作者 9楼 发表于: 2011-10-24
不用那么复杂吧,定义一个全局的QString,把address值赋给它;在另一个cpp中extern这个变量不就可以用了嘛。

只看该作者 10楼 发表于: 2011-10-29
用第一种方案解决了!哈哈!谢谢各位大侠!!!
快速回复
限100 字节
 
上一个 下一个