• 7060阅读
  • 3回复

[提问]怎样在Qt Runnable子类下编写信号和槽? [复制链接]

上一主题 下一主题
离线478964147
 
只看楼主 倒序阅读 楼主  发表于: 2013-03-08
在QRunnable的子类MyRunnable中,怎样编写一个signal,使得这个signal与QTextBrowser的append方法连接?
我写了一个,老是说,我的connect不匹配,求教啊!



#ifndef MYRUNNABLE_H
#define MYRUNNABLE_H

#include <QRunnable>
#include <QTcpSocket>
#include <QTextBrowser>
#include <QtCore>

class MyRunnable : public QRunnable
{
    Q_OBJECT
public:
    explicit MyRunnable(QObject *parent = 0);
    int SocketDescriptor;
    void Initial(QTextBrowser*,int);
    QTextBrowser *text;
    QTcpSocket *socket;
protected:
    void run();
    void Login();
    void TimeLink();
    void SendMessage();
    void SiPin();
signals:
    void UpdateUi(QString);
public slots:
    
};

#endif // MYRUNNABLE_H




#include "myrunnable.h"

MyRunnable::MyRunnable(QObject *parent) :
    QRunnable()
{
}
void MyRunnable::Initial(QTextBrowser *text,int SocketDescriptor )
{
    this->text=text;
    this->SocketDescriptor=SocketDescriptor;

    connect(this,SIGNAL(UpdateUi(QString)),text,SLOT(append(QString)));
}
void MyRunnable::run()
{

    if(!SocketDescriptor){
        qDebug()<<"!sock";
        return;
    }
    else{
        QTcpSocket socket;
        socket.setSocketDescriptor(SocketDescriptor);
        qDebug()<<"****************************\n";


       // text->append("****************************");
       // text->append("User:"+QString::number(SocketDescriptor));
       // socket.r
        //qDebug()<<socket.readAll();
        //  socket.waitForReadyRead(3000);
       //text->append(socket.readAll());
       // text->append("****************************\n");
       // socket.write("aaaaaaaaaaaaaaaaaaaaaaaaa");
          emit UpdateUi("*************************");
          emit UpdateUi("User:"+QString::number(SocketDescriptor));
          socket.waitForReadyRead(3000);
//        emit UpdateUi("User:"+QString::number(SocketDescriptor));
          emit UpdateUi(socket.readAll());
          emit UpdateUi("*************************");

        socket.close();
    }
}

报以下错误
..\MyServer\myrunnable.cpp:3:1: warning: unused parameter 'parent' [-Wunused-parameter]
..\MyServer\myrunnable.cpp: In member function 'void MyRunnable::Initial(QTextBrowser*, int)':
..\MyServer\myrunnable.cpp:12:70: error: invalid conversion from 'MyRunnable* const' to 'SOCKET {aka unsigned int}' [-fpermissive]
..\MyServer\myrunnable.cpp:12:70: error: cannot convert 'const char*' to 'const sockaddr*' for argument '2' to 'int connect(SOCKET, const sockaddr*, int)'
求教啊
离线pxiao_xiao

只看该作者 1楼 发表于: 2013-03-09
QRunnable是QObject的派生类?
离线478964147
只看该作者 2楼 发表于: 2013-03-09
回 1楼(pxiao_xiao) 的帖子
解决了,我把他多继承,同时继承QOject
离线pxiao_xiao

只看该作者 3楼 发表于: 2013-03-09
呵呵 好
快速回复
限100 字节
 
上一个 下一个