⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog.cpp.svn-base

📁 QT网络传输。用来演示QT的网络传输类。
💻 SVN-BASE
字号:
/*  * Author:  $Author: vincent $ * Date:    $Date: 2006/04/24 02:28:56 $ * Module:  $RCSfile: dialog.cpp,v $ * Revision:$Revision: 1.3 $ * Status:  $State: Exp $ */#include <QtGui>#include <QtNetwork>#include "dialog.h"#include "statisticswidget.h"Dialog::Dialog(QWidget *parent):QDialog(parent){    /* Create List Widget */    //listWidget=new QListWidget;    /* create some widgets */    statisticsWidget=new StatisticsWidget();    diWidget=new DIWidget;    diWidget->setEnabled(false);    configWidget=new ConfigWidget;    configWidget->enableConnect(true);    /* create a tcp socket */    tcpSocket=new QTcpSocket(this);    /* set timer */    timer=new QTimer();    timer->setInterval(1000);    connect(timer, SIGNAL(timeout()),            this, SLOT(updateStatistics()));    /* some condition variables */    bSend=true;    bToConnect=true;    /* statistics variables */    bytesWritten=0;    /// DIWidget Related    connect(diWidget,SIGNAL(wantToSelectFile()),            this,SLOT(selectFile()));    connect(diWidget,SIGNAL(wantToSendFile()),            this,SLOT(sendFile()));    connect(diWidget,SIGNAL(wantToStopSend()),            this,SLOT(stopSend()));    /// ConfigWidget Related    connect(configWidget,SIGNAL(wantToQuit()),            this,SLOT(close()));    connect(configWidget,SIGNAL(wantToConnect()),            this,SLOT(connectServer()));    connect(configWidget,SIGNAL(wantToDisconnect()),            this,SLOT(disconnectServer()));    /// Socket Related    connect(tcpSocket,SIGNAL(connected()),            this,SLOT(hasConnected()));    connect(tcpSocket,SIGNAL(disconnected()),            this,SLOT(hasDisconnected()));    connect(tcpSocket,SIGNAL(bytesWritten(qint64)),            this, SLOT(updateProgress(qint64)));    connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),            this,SLOT(displayError(QAbstractSocket::SocketError)));    QVBoxLayout *mainLayout=new QVBoxLayout;    mainLayout->addWidget(statisticsWidget);    mainLayout->addWidget(diWidget);    mainLayout->addWidget(configWidget);    setLayout(mainLayout);    QIcon icon(tr("bluefish.xpm"));    setWindowIcon(icon);}void Dialog::connectServer(){    /*       listWidget->addItem(tr("Server Address:%1")       .arg(configWidget->getHost()));       listWidget->addItem(tr("DI Port:%1")       .arg(configWidget->getDIPort()));       */    if(bToConnect)    {        tcpSocket->abort();        tcpSocket->connectToHost(configWidget->getHost(),                configWidget->getDIPort());    }    else    {        tcpSocket->disconnectFromHost();    }}void Dialog::disconnectServer(){    tcpSocket->disconnectFromHost();}void Dialog::selectFile(){    filename=QFileDialog::getOpenFileName(            this,            "Choose a file to send",            ".",            "Data files(*.dat)::All files(*.*)");    if(!filename.isEmpty()){        bytesWritten=0;       statisticsWidget->setFilename(filename);    }}void Dialog::sendFile(){    if(filename.isEmpty()){        QMessageBox::information(this,tr("Data Transmit"),                tr("Error: No Data File!"));        return;    }    file=new QFile(filename);    if(!file->open(QIODevice::ReadOnly)){        QMessageBox::information(this,tr("Data Receive"),                tr("Select file failed"));        return;    }     //file->seek(bytesWritten);    bSend=true;    bytesWritten=0;    bytesToWrite=file->size();    _total=0;    _time=0;    timer->start();    diWidget->enableButtons(false);    //listWidget->addItem("File Size:"+QString::number(file->size())+" bytes");    QByteArray block=file->read(PayloadSize);    bytesToWrite -= (int)tcpSocket->write(block);}void Dialog::stopSend(){    /*listWidget->addItem("Stop to send datas");*/    qDebug()<<"dialog:diStopSend";    bSend=false;    timer->stop();    diWidget->enableButtons(true);}void Dialog::checkQuit(){    qDebug()<<"checkQuit()";}void Dialog::hasConnected(){    qDebug()<<"connected";    //listWidget->addItem("Connected!");    //    bToConnect=false;    diWidget->setEnabled(true);    configWidget->enableConnect(false);}void Dialog::hasDisconnected(){    qDebug()<<"disconnected";    //listWidget->addItem("Disconnected!");    bToConnect=true;    diWidget->setEnabled(false);    configWidget->enableConnect(true);}void Dialog::updateProgress(qint64 numBytes){    qDebug()<<"write:"<<numBytes;    bytesWritten+=(int)numBytes;    _total+=numBytes;    diWidget->setMaximum(file->size());    diWidget->setValue(bytesWritten);    if(!bSend)        return;    if( bytesToWrite > 0){        QByteArray block=file->read(PayloadSize);        bytesToWrite -= (int)tcpSocket->write(block);    }    else{        //tcpSocket->disconnectFromHost();        file->seek(0);        bytesToWrite = file->size();        bytesWritten = 0;        QByteArray block=file->read(PayloadSize);        bytesToWrite -= (int)tcpSocket->write(block);    }}void Dialog::displayError(QAbstractSocket::SocketError socketError){    qDebug()<<"displayError";    QMessageBox::information(this,tr("Data Transmit"),            tr("Error occured: %1.")            .arg(tcpSocket->errorString()));}void Dialog::updateStatistics(){    _time++;    statisticsWidget->setTime(_time);    statisticsWidget->setCount(_total/1024);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -