?? network.html
字號:
<li><code>start()</code> just before starting listing the children <li><code>newChildren()</code> when new children are read </ul> <li>Emit in <code>operationMkDir:</code> <ul> <li><code>createdDirectory()</code> after the directory has been created <li><code>newChild()</code> (or newChildren()) after the directory has been created (as a new directory is a new child) </ul> <li>Emit in <code>operationRemove:</code> <ul> <li><code>removed()</code> after the child has been removed </ul> <li>Emit in <code>operationRename:</code> <ul> <li><code>itemChanged()</code> after the child has been renamed </ul> <li>Emit in <code>operationGet:</code> <ul> <li><code>data()</code> each time new data has been read <li><code>dataTransferProgress()</code> each time new data has been read to indicate how much of the data has been read now. </ul> <li>Emit in <code>operationPut:</code> <ul> <li><code>dataTransferProgress()</code> each time data has been written to indicate how much of the data has been written. Although you know the whole data when this operation is called, it's suggested not to write the whole data at once, but to do it step by step to avoid blocking the GUI and also this way the progress can be made visible to the user. </ul></ul><p>And remember, always emit the <code>finished()</code> signal the the end!<p>For more details about the arguments of these signals take a lookat the <code>QNetworkProtocol</code> class documentation.<p>Now, as argument in such a method you get the <code>QNetworkOperation</code>which you process. Here is a list which arguments of the <code>QNetworkOperation</code>you can get and which you have to set in which method:<p>(To get the URL on which you should work, use the <code>QNetworkProtocol::url()</code> methodwhich returns the pointer to the URL operator. Using that you can get the path, host,name filter and everything else of the URL)<p><ul> <li>In <code>operationListChildren:</code> <ul> <li>Nothing. </ul> <li>In <code>operationMkDir:</code> <ul> <li><code>QNetworkOperation::arg(</code> 0 ) contains the name of the directory which should be created </ul> <li>In <code>operationRemove:</code> <ul> <li><code>QNetworkOperation::arg(</code> 0 ) contains the name of the file which should be removed. Normally this is a relative name. But it may be absolute too, so use QUrl( op->arg( 0 ) ).fileName() to get the filename. </ul> <li>In <code>operationRename:</code> <ul> <li><code>QNetworkOperation::arg(</code> 0 ) contains the name of the file which should be renamed <li><code>QNetworkOperation::arg(</code> 1 ) contains the name to which it should be renamed. </ul> <li>In <code>operationGet:</code> <ul> <li><code>QNetworkOperation::arg(</code> 0 ) contains the full URL of the file which should be retrieved. </ul> <li>In <code>operationPut:</code> <ul> <li><code>QNetworkOperation::arg(</code> 0 ) contains the full URL of the file in which the data should be stored. <li><code>QNetworkOperation::rawArg(</code> 1 ) contains the data which should be stored in <code>QNetworkOperation::arg(</code> 0 ) </ul></ul><p>So, to sum it up: If you reimplement such an operation method, youhave to emit some special signals and <b>always</b> at the end a<code>finished()</code> signal, either on success or on failure. Also you have to changethe state of the <code>QNetworkOperation</code> during processing it and can getand set arguments of the operation as well.<p>But it's unlikely that the network protocol you implement supportsall these operations. So, just reimplement the operations, whichare supported by the protocol. Additionally you have to specify whichoperations are supported then. This is done by reimplementing<p><pre> int QNetworkProtocol::supportedOperations() const;</pre><p>In your implementation of this method return an int valuewhich is constructed by or'ing together the correct values(supported operations) of the following enum (of <code>QNetworkProtocol):</code><p><pre> enum Operation { OpListChildren = 1, OpMkdir = 2, OpRemove = 4, OpRename = 8, OpGet = 32, OpPut = 64 };</pre><p>So, if your protocol e.g. supports listing children and renaming them, doin your implementation of <code>supportedOperations():</code><p><pre> return OpListChildren | OpRename;</pre><p>The last method you have to reimplement is<p><pre> bool QNetworkProtocol::checkConnection( <a href="qnetworkoperation.html">QNetworkOperation</a> *op );</pre><p>Here you have to return TRUE, if the connection is up and ok (this meansoperations on the protocol can be done). If the connection is not ok,return FALSE and start to try opening it. If you will not be able to open theconnection at all (e.g. because the host is not found), emit a <code>finished()</code>signal and set an error code and the <code>QNetworkProtocol::StFailed</code> state tothe <code>QNetworkOperation</code> pointer you get here.<p>Now, you never need to check before doing an operation yourself,if the connection is ok. The network architecture does this, this meansusing <code>checkConnection()</code> it looks if an operation could be done and ifnot, it tries it again and again for some time and only calls an operationmethod if the connection is ok.<p>Using this knowledge it should be possible to implement network protocols. Finallyto be able to use it with a QUrlOperator (and so e.g. in the QFileDialog), you have toregister the network protocol implementation. This can be done like this:<p><pre> <a href="qnetworkprotocol.html#e0692e">QNetworkProtocol::registerNetworkProtocol</a>( "myprot", new QNetworkProtocolFactory<MyProtocol> );</pre><p>In this case <code>MyProtocol</code> would be a class you implemented like described here(derived from <code>QNetworkProtocol)</code> and the name of the protocol would bemyprot. So if you want to use it, you would do something like<p><pre> <a href="qurloperator.html">QUrlOperator</a> op( "myprot://host/path" ); op.<a href="qurloperator.html#42b8a8">listChildren</a>();</pre><p>Finally as example for a network protocol implementation you could look at theimplementation of QLocalFs. The network extension will also contain an exampleimplementation of a network protocol<p><h2>Error Handling</h2><p>Error handling is important for both, implementing new network protocols and usingthem (through <code>QUrlOperator).</code> So first some words about error handling when usingthe network protocols:<p>As already mentioned quite some times after processing an operation has been finishedthe network operation and so the QUrlOperator emits the <code>finished()</code> signal. This hasas argument the pointer to the processed <code>QNetworkOperation.</code> If the state of this operationis <code>QNetworkProtocol::StFailed,</code> the operation contains some more information about thiserror. Following error codes are defined in <code>QNetworkProtocol:</code><p><ul> <li><code>QNetworkProtocol::NoError</code> - No error occurred <li><code>QNetworkProtocol::ErrValid</code> - The URL you are operating on is not valid <li><code>QNetworkProtocol::ErrUnknownProtocol</code> - There is no protocol implementation available for the protocol of the URL you are operating on (e.g. if the protocol is http and no http implementation has been registered) <li><code>QNetworkProtocol::ErrUnsupported</code> - The operation is not supported by the protocol <li><code>QNetworkProtocol::ErrParse</code> - Parse error of the URL <li><code>QNetworkProtocol::ErrLoginIncorrect</code> - You needed to login but the username and or password are wrong <li><code>QNetworkProtocol::ErrHostNotFound</code> - The specified host (in the URL) couldn磘 be found <li><code>QNetworkProtocol::ErrListChlidren</code> - An error occurred while listing the children <li><code>QNetworkProtocol::ErrMkdir</code> - An error occurred when creating a directory <li><code>QNetworkProtocol::ErrRemove</code> -An error occurred while removing a child <li><code>QNetworkProtocol::ErrRename</code> - An error occurred while renaming a child <li><code>QNetworkProtocol::ErrGet</code> - An error occurred while getting (retrieving) data <li><code>QNetworkProtocol::ErrPut</code> - An error occurred while putting (uploading) data <li><code>QNetworkProtocol::ErrFileNotExisting</code> - A file which is needed by the operation doesn't exist <li><code>QNetworkProtocol::ErrPermissionDenied</code> - The permission for doing the operation has been denied</ul><p><code>QNetworkOperation::errorCode()</code> returns then one of these codes or maybe a different oneif you use an own network protocol implementation which defines additional error codes.<p><code>QNetworkOperation::protocolDetails()</code> may also return a string which contains an errormessage then which could e.g. be displayed for the user.<p>According to this information it should be possible to react on errors.<p>Now, if you implement your own network protocol, you will need to tell about errorswhich occurred. First you always need to be able to access the <code>QNetworkOperation</code>which is processed at the moment. This can be done using <code>QNetworkOperation::operationInProgress(),</code>which returns a pointer to the current network operation or 0 if no operation is processed at the moment.<p>Now if and error occurred and you need to handle it, do<p><pre> if ( operationInProgress() ) { operationInProgress()->setErrorCode( error_code_of_your_error ); operationInProgress()->setProtocolDetails( detail ); // optional! emit finished( operationInProgress() ); return; }</pre><p>That's all. The connection to the <code>QUrlOperator</code> and so on is done automatically. Additionally,if the error was really bad so that no more operations can be done in the current state (e.g.if the host couldn't be found), call, <b>before emitting <code>finished()</b></code><code>QNetworkProtocol::clearOperationStack().</code><p>Now, as error code you should use, if possible, one of the predefined error codesof <code>QNetworkProtocol.</code> If this is not possible, you can add own error codes - they arejust normal <code>integers.</code> Just be careful that the value of the error code doesn't conflictwith an existing one.<p>Documentation about the low-level classes like QSocket, QDns, etc. will be includedin the seperate network extension.<p><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -