2015年11月17日 星期二

[Qt] FTP續傳功能

FTP續傳功能主要分為 上傳 / 下載

在開始看續傳功能之前必須要先了解,FTP有所謂的"原生指令"(Raw Command)

Raw Command包括:

TYPE - set transfer type
      TYPE I - image (binary data)
PASV - enter passive mode
PORT - open a data port
    PORT a1,a2,a3,a4,p1,p2
SIZE - return the size of a file
REST - Sets the point at which a file transfer should start
    REST position
RETR - Begins transmission of a file from the remote host.
    RETR remote-filename

Common commandsABOR - abort a file transfer
CWD - change working directory
DELE - delete a remote file
LIST - list remote files
MDTM - return the modification time of a file
MKD - make a remote directory
NLST - name list of remote directory
PASS - send password


PWD - print working directory
QUIT - terminate the connection
RETR - retrieve a remote file
RMD - remove a remote directory
RNFR - rename from
RNTO - rename to
SITE - site-specific commands

STOR - store a file on the remote host

USER - send username
Less common commandsACCT* - send account information
APPE - append to a remote file
CDUP - CWD to the parent of the current directory
HELP - return help on using the server
MODE - set transfer mode
NOOP - do nothing
REIN* - reinitialize the connection
STAT - return server status
STOU - store a file uniquely
STRU - set file transfer structure
SYST - return system type


每一個Command各自代表其中一個意思,而續傳只需要其中五個指令而已

(上傳)
1、rawCommand("TYPE I");設置傳輸數據的類型:二進制數據或ASCII
2、rawCommand("PASV");設置服務器被動接收方式。發送PASV命令後,服務器會返回自己開?的數據傳輸的端口,等待客戶端連接進行數據傳輸。返回的數據格式?:“227 Entering Passive Mode (192, 168, 2, 18, 118, 32)”,然後從返回的信息裏面或去相關的信息,ftp服務器的IP地址:192.168.2.18;ftp服務器開?的用于數據傳輸的端口:118*256 + 32 = 30240;獲得該信息後就需要建立TcpSocket的通信鏈路,連接ftp服務器。
3、rawCommand("APPE  remote-file-path");設置服務器端remote-file-path追加的方式。如果此時改文件不存在,則服務器端會創建一個。
 4、完成上述流程後,就可以打開本地文件進行讀取,並通過tcpsocket鏈路發送出去(write)。

(下載)
1、rawCommand("TYPE I");設置傳輸數據的類型:二進制數據或ASCII
2、rawCommand("PASV");設置服務器被動接收方式。發送PASV命令後,服務器會返回自己開?的數據傳輸的端口,等待客戶端連接進行數據傳輸。返回的數據格式?:“227 Entering Passive Mode (192, 168, 2, 18, 118, 32)”,然後從返回的信息裏面或去相關的信息,ftp服務器的IP地址:192.168.2.18;ftp服務器開啟的用于數據傳輸的端口:118*256 + 32 = 30240;獲得該信息後就需要建立TcpSocket的通信鏈路,連接ftp服務器。
3、rawCommand("REST  size");該命令設置ftp服務器從本地文件的哪個地方開始進行數據傳輸。
4、rawCommand("RETR  remote-file-path");開始從遠程主機傳輸文件。

        文件上傳時在設置APPE返回之後,就可以打開本地文件進行上傳;文件下載時,收到PASV的返回信息建立tcpsocket的連接後,需要建立readyRead()的信號槽,在該槽函數中實現數據的讀取。

上面是網路上找來的資料

*所謂的PASV = passive mode 被動接收模式
*size就是要從哪一點開始續傳
*端口就是Port

End(有待補充)

2015年11月4日 星期三

[Qt] Regular Expression 正規表示法 用法

正規表示法在檢查字串是否合法時,非常好用

剛好寫到就寫起來紀錄一下。

下面有三個Regular Expression

1. QRegExp regNum("\\d*");

接受所有數字(原本為\d,但C++ compiler需要再一個\ 所以就變成double \ )

2.QRegExp regCode39("[A-Z0-9+-./$%]*");

接受大寫英文,數字+-./$%,記得後面要加上* 這樣所有字元才都能檢查到,沒有*會產生邏輯上的錯誤

3.  QRegExp regCode128("[\\w\\S]*");

\w 是接受所有大小寫英文(a~z + A~Z)

\S則是接受所有特殊字元(除空白)

\s則是各種空白( \r\t\n\f)

用法很簡單,只要使用exactMatch(QString)就可以知道是否正確了

可以加速,不需要for跑很久!

END

2015年10月14日 星期三

[Qt] Read Qss file (讀取QSS檔案)

qss file內容約為:

CImageBrowseDialog {
border: none;
background-image: url(Skins/CImageBrowseDialog/Common/004-back.jpg);
}

QPushButton#previousButton {
qproperty-geometry: rect(15 656 98 70);
background-image: url(Skins/CImageBrowseDialog/Common/004-normal_25.jpg);
}

QPushButton#previousButton:pressed {
background-image: url(Skins/CImageBrowseDialog/Common/004-push_25.jpg);
}

QPushButton#previousButton:disabled {
background-image: url(Skins/CImageBrowseDialog/Common/004-gray_25.jpg);
}

QPushButton#nextButton {
qproperty-geometry: rect(907 658 97 70);
background-image: url(Skins/CImageBrowseDialog/Common/004-normal_28.jpg);
}

QPushButton#nextButton:pressed {
background-image: url(Skins/CImageBrowseDialog/Common/004-push_28.jpg);
}

QPushButton#nextButton:disabled {
background-image: url(Skins/CImageBrowseDialog/Common/004-gray_28.jpg);
}

QLabel#PreLabel
{
  border: none;
  qproperty-geometry: rect(15 720 98 40);
  color : white;
}
 
QLabel#NextLabel
{
  border: none;
  qproperty-geometry: rect(907 720 98 40);
  color : white;
}


該如何讀取呢?

在main.cpp裡面

#include <QtGui/QApplication>
#include "mainwindow.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QFile file("D:\\MyCode\\Qt\\CImageBrowseDialog(QSS file)\\COMMON.QSS");
    file.open(QFile::ReadOnly);
    QString styleSheet(file.readAll());
    qDebug()<<styleSheet;
    a.setStyleSheet(styleSheet);


    return a.exec();
}


讀取路徑可以設為相對(這邊是絕對路徑)

file.readAll()就會把所有的都讀近QString當中,最後設定application的setStyleSheet()即可完成

(當然UI介面要有相對應的button/label,objectName也要相同就是了)

END

2015年10月12日 星期一

[Qt] SQLite + Qt 組合

開始要把Qt搭配SQLite了,

遇到一些問題把他先記錄起來

(*為你的project name)

1. *.pro檔案裡面需要加入

QT += sql 

2. *.h需要

 #include <QSqlDatabase>

3.使用如下:

    QSqlDatabase my_db;
    my_db = QSqlDatabase::addDatabase("QSQLITE","Qt_DB1");
    //QSLITE = 參數(driver)
    //Qt_DB1 = database名稱(請勿使用default,也就是不設,這樣使用其他的程式也呼叫default的時候會覆蓋)
    my_db.setDatabaseName("test.sqlite");
    if(my_db.open()) QMessageBox::information(0,"Ok","Connection ok");
    else QMessageBox::information(0,"Error","some error.");

(使用QMessageBox也是要include,這邊就不多贅述 // sqlite檔案使用firefox 外掛SQLite來創建)

2015年10月7日 星期三

[Qt] qmake 如何跳到非C槽(ex D槽,E槽....)



相信一定有人也遇到這樣的問題,所以上來搜尋

其實很簡單 ,跳到其他槽時候不要加cd即可

end