Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
class Widget : public QWidget
{
    Q_OBJECT
public:
    
explicit Widget(QWidget *parent = 0);
    
~Widget();
private slots:
    
void readMessage();
    
void displayError(QAbstractSocket::SocketError);
    
void sendMessage();

    
void connected();
    
void disconnected();
    
void onError(QAbstractSocket::SocketError);
    
void onProgress();
private:
    QTcpSocket
* tcpsocket;
    
bool flag;
    QTimer timer;
    QString message;
    QPushButton
* button;
};

Widget::Widget(QWidget
* parent):
    QWidget(parent)
{
    button 
= new QPushButton("Send");
    QVBoxLayout
* layout = new QVBoxLayout(this);
    layout
->addWidget(button);

    connect(button,SIGNAL(clicked()),
this,SLOT(sendMessage()));

    tcpsocket 
= new QTcpSocket(this);
    flag 
= false;
    
//tcpsocket->abort();
    
//tcpsocket->connectToHost("10.2.100.63",80);
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
    connect(tcpsocket,SIGNAL(connected()),
this,SLOT(connected()));
    connect(tcpsocket,SIGNAL(disconnected()),
this,SLOT(disconnected()));
    connect(tcpsocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(onError(QAbstractSocket::SocketError)));

    connect(
&timer,SIGNAL(timeout()),this,SLOT(onProgress()));
    timer.start(
1800);
}

Widget::
~Widget()
{
}

void Widget::onProgress()
{
    
if(flag == false)
    {
        tcpsocket
->abort();
        tcpsocket
->connectToHost("10.2.100.63",80);
    }
}

void Widget::readMessage()
{
    QString tag 
= tcpsocket->readAll();
    std::cout
<<"tag:"<<tag.size()<<std::endl;
}

void Widget::displayError(QAbstractSocket::SocketError)
{
    qDebug() 
<< tcpsocket->errorString();
}

void Widget::sendMessage()
{
    std::cout
<<"write callback data."<<std::endl;
    tcpsocket
->write("ok");    
}

void Widget::connected()
{
    flag 
= true;
}

void Widget::disconnected()
{
    flag 
= false;
}

void Widget::onError(QAbstractSocket::SocketError)
{
}
posted on 2016-04-11 17:59 ccsdu2009 阅读(4254) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理