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

📄 tripplanner.cpp

📁 Qt4的一些例子
💻 CPP
字号:
#include <QtGui>#include <QtNetwork>#include "tripplanner.h"TripPlanner::TripPlanner(QWidget *parent)    : QDialog(parent){    setupUi(this);    QDateTime dateTime = QDateTime::currentDateTime();    dateEdit->setDate(dateTime.date());    timeEdit->setTime(QTime(dateTime.time().hour(), 0));    progressBar->hide();    progressBar->setSizePolicy(QSizePolicy::Preferred,                               QSizePolicy::Ignored);    tableWidget->verticalHeader()->hide();    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);    connect(searchButton, SIGNAL(clicked()),            this, SLOT(connectToServer()));    connect(stopButton, SIGNAL(clicked()), this, SLOT(stopSearch()));    connect(&tcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));    connect(&tcpSocket, SIGNAL(disconnected()),            this, SLOT(connectionClosedByServer()));    connect(&tcpSocket, SIGNAL(readyRead()),            this, SLOT(updateTableWidget()));    connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),            this, SLOT(error()));}void TripPlanner::connectToServer(){#if 1    tcpSocket.connectToHost(QHostAddress::LocalHost, 6178);#else    tcpSocket.connectToHost("tripserver.zugbahn.de", 6178);#endif    tableWidget->setRowCount(0);    searchButton->setEnabled(false);    stopButton->setEnabled(true);    statusLabel->setText(tr("Connecting to server..."));    progressBar->show();    nextBlockSize = 0;}void TripPlanner::sendRequest(){    QByteArray block;    QDataStream out(&block, QIODevice::WriteOnly);    out.setVersion(QDataStream::Qt_4_1);    out << quint16(0) << quint8('S') << fromComboBox->currentText()        << toComboBox->currentText() << dateEdit->date()        << timeEdit->time();    if (departureRadioButton->isChecked()) {        out << quint8('D');    } else {        out << quint8('A');    }    out.device()->seek(0);    out << quint16(block.size() - sizeof(quint16));    tcpSocket.write(block);    statusLabel->setText(tr("Sending request..."));}void TripPlanner::updateTableWidget(){    QDataStream in(&tcpSocket);    in.setVersion(QDataStream::Qt_4_1);    forever {        int row = tableWidget->rowCount();        if (nextBlockSize == 0) {            if (tcpSocket.bytesAvailable() < sizeof(quint16))                break;            in >> nextBlockSize;        }        if (nextBlockSize == 0xFFFF) {            closeConnection();            statusLabel->setText(tr("Found %1 trip(s)").arg(row));            break;        }        if (tcpSocket.bytesAvailable() < nextBlockSize)            break;        QDate date;        QTime departureTime;        QTime arrivalTime;        quint16 duration;        quint8 changes;        QString trainType;        in >> date >> departureTime >> duration >> changes >> trainType;        arrivalTime = departureTime.addSecs(duration * 60);        tableWidget->setRowCount(row + 1);        QStringList fields;        fields << date.toString(Qt::LocalDate)               << departureTime.toString(tr("hh:mm"))               << arrivalTime.toString(tr("hh:mm"))               << tr("%1 hr %2 min").arg(duration / 60)                                    .arg(duration % 60)               << QString::number(changes)               << trainType;        for (int i = 0; i < fields.count(); ++i)            tableWidget->setItem(row, i,                                 new QTableWidgetItem(fields[i]));        nextBlockSize = 0;    }}void TripPlanner::stopSearch(){    statusLabel->setText(tr("Search stopped"));    closeConnection();}void TripPlanner::connectionClosedByServer(){    if (nextBlockSize != 0xFFFF)        statusLabel->setText(tr("Error: Connection closed by server"));    closeConnection();}void TripPlanner::error(){    statusLabel->setText(tcpSocket.errorString());    closeConnection();}void TripPlanner::closeConnection(){    tcpSocket.close();    searchButton->setEnabled(true);    stopButton->setEnabled(false);    progressBar->hide();}

⌨️ 快捷键说明

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