?? dialog.cpp
字號:
#include <QtGui>
#include <QtNetwork>
#include <QFile>
#include <QDir>
#include <stdlib.h>
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
tcpSocket = new QTcpSocket(this);
serverLabel = new QLabel(QString::fromLocal8Bit("服務器ip:"));
ipInfoLabel = new QLabel(QString::fromLocal8Bit("服務器port:"));
sqlipLabel = new QLabel(QString::fromLocal8Bit("數據庫ip:"));
databaseLabel = new QLabel(QString::fromLocal8Bit("數據庫名:"));
sqluserLabel = new QLabel(QString::fromLocal8Bit("用戶名:"));
sqlpassLabel = new QLabel(QString::fromLocal8Bit("數據庫密碼:"));
startdateLabel = new QLabel(QString::fromLocal8Bit("開始日期:"));
starttimeLabel = new QLabel(QString::fromLocal8Bit("開始時間:"));
ComboBox = new QComboBox;
ComboBox->addItem(tr("local"));
ComboBox->addItem(tr("center"));
ComboBox->addItem(tr("liantong"));
ComboBox->addItem(tr("yidong"));
ipInfoLineEdit = new QLineEdit(tr("hj"));
sqlipLineEdit = new QLineEdit(tr("hj"));
databaseLineEdit = new QLineEdit(tr("hj"));
sqluserLineEdit = new QLineEdit(tr("hj"));
sqlpassLineEdit = new QLineEdit(tr("hj"));
startdateLineEdit=new QLineEdit(QDate::currentDate().toString("yyyy.MM.dd"));
starttimeLineEdit=new QLineEdit(QTime::currentTime().toString("h:m:s"));
sqlpassLineEdit->setEchoMode(QLineEdit::Password);
// hostLineEdit->setReadOnly(true);
ipInfoLineEdit->setReadOnly(true);
startdateLineEdit->setReadOnly(true);
starttimeLineEdit->setReadOnly(true);
//display.setReadOnly(true);
beginButton = new QPushButton(tr("Begin"));
beginButton->setDefault(true);
beginButton->setEnabled(true);
quitButton = new QPushButton(tr("Quit"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(beginButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(beginButton,SIGNAL(clicked()), this, SLOT(begin()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(serverLabel, 0, 0);
mainLayout->addWidget(ComboBox, 0, 1);
mainLayout->addWidget(ipInfoLabel, 0, 2);
mainLayout->addWidget(ipInfoLineEdit, 0, 3);
mainLayout->addWidget(sqlipLabel, 1, 0);
mainLayout->addWidget(sqlipLineEdit, 1, 1);
mainLayout->addWidget(databaseLabel,1,2);
mainLayout->addWidget(databaseLineEdit,1,3);
mainLayout->addWidget(sqluserLabel,2,0);
mainLayout->addWidget(sqluserLineEdit,2,1);
mainLayout->addWidget(sqlpassLabel,2,2);
mainLayout->addWidget(sqlpassLineEdit,2,3);
mainLayout->addWidget(startdateLabel,3,0);
mainLayout->addWidget(startdateLineEdit,3,1);
mainLayout->addWidget(starttimeLabel,3,2);
mainLayout->addWidget(starttimeLineEdit,3,3);
mainLayout->addWidget(&display,4,0,20,4);
mainLayout->addWidget(buttonBox,24,0,1,4);
setLayout(mainLayout);
setWindowTitle(tr("Fortune Client-getdata"));
}
void Dialog::begin()
{
//創建日志文件夾
QString filepath;
QDir dir;
filepath = dir.currentPath();
if(!filepath.contains("log_center_receive"))
{
dir.mkdir("log_center_receive");
filepath += "/log_center_receive/";
dir.setCurrent(filepath);
}
//連接數據庫,用戶名和密碼從界面上填入獲得
db = QSqlDatabase::addDatabase("QSQLITE"); // 使用sqlserver數據庫驅動
db.setDatabaseName("E:/sqlite/oracle.db"); // 之前建立的數據庫名
//db.setUserName(username); // 之前創建的用戶名
//db.setPassword(password); //用戶的密碼
if(!db.open())
{
display.append("cannot open database."); // 打開數據庫失敗,顯示錯誤原因
display.append("Reason: " + db.lastError().databaseText());
}
getip(); //獲得ip
tcpSocket->connectToHost(host,port);
if(tcpSocket->waitForConnected(3000)==false)
{
display.append("Failed to connect to the server!");
return;
}
//和服務器交互信息
QByteArray block("IamOracleW"); //請求數據包格式
tcpSocket->write(block);
if(tcpSocket->waitForBytesWritten()==false)
{
display.append("Failed to send the information to the server!");
return;
}
display.append("The client is working now!");
}
void Dialog::getip()
{
QSqlQuery queryip;
QSqlError errorip;
queryip.prepare("select * from ipInfo where servername=:name ");
queryip.bindValue(":name",ComboBox->currentText());
if(queryip.exec())
{
queryip.first();
//對port和host賦值
port=queryip.value(2).toInt();
host=queryip.value(1).toString();
ipInfoLineEdit->setText(host+" "+queryip.value(2).toString());
}
else
{
//如果查詢失敗,顯示錯誤
errorip= queryip.lastError();
display.append("From sqlite database,the ipInfo table: " + errorip.databaseText());
}
}
void Dialog::readFortune()
{
QByteArray tempArray[5];
QByteArray temp=tcpSocket->readAll();
if(temp.size()==0)
{
display.append("error");
}
QByteArray type=temp.mid(9,6);
display.append(temp);
if(type=="211001")
{
writeData(temp.left(100)); //長度未知????
}
else
{
int i,leng[5],j=0;
for(i=0;i<5;i++)
{
leng[i]=temp.mid((temp.size()-20+i*4),4).toInt(); //格式:socket號+數據
tempArray[i]=temp.mid(j,leng[i]);
j=j+leng[i];
if(leng[i]!=0)
{
writeData(tempArray[i]);
}
}
}
display.append("write");
tcpSocket->write("OracleWReady"); //返回確認信息
if(tcpSocket->waitForBytesWritten()==false)
{
display.append("Failed to send the ACK to the server!");
return;
}
}
void Dialog::writeData(QByteArray temparray)
{
QByteArray socketnum;
QByteArray data;
display.append(temparray);
socketnum=temparray.left(5);
data=temparray.right(temparray.size()-5); //未取年份
QSqlQuery query;
QSqlError error;
query.prepare("insert into oracle(packet,socketnum) values (:pa,:socket)");
query.bindValue(":pa",(QString)data);
query.bindValue(":socket",(QString)socketnum);
if(!query.exec())
{
//如果查詢失敗,返回錯誤信息
error = query.lastError();
QString temp="From oracle database,the oracle table: "+ error.databaseText();
display.append(temp);
QString fortune="Not insert sucesfully"+QString::fromLocal8Bit(data+socketnum)+QTime::currentTime().toString("HH:mm:ss");
QFile file(fortune);
file.open(QIODevice::WriteOnly);
QTextStream log(&file);
log <<(data+socketnum)<<"\n";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -