您不能讓QBuffer::open()
返回false (*)。所以你不能在你的場景中使用QBuffer
。
但對於子類,只是重寫open()
總是返回false?
class UnopenableDevice : public QBuffer {
public:
bool open(QIODevice::OpenMode m) { return false; }
};
(*)至少不使用標誌WriteOnly
和/或ReadOnly
。傳遞無效標誌是使其返回false的唯一可能性。引用的Qt 4.8.0來源:
corelib的/ IO/qbuffer.cpp:
332 bool QBuffer::open(OpenMode flags)
333 {
334 Q_D(QBuffer);
335
336 if ((flags & (Append | Truncate)) != 0)
337 flags |= WriteOnly;
338 if ((flags & (ReadOnly | WriteOnly)) == 0) {
339 qWarning("QBuffer::open: Buffer access not specified");
340 return false; // <----- only possibility to return false!
341 }
342
343 if ((flags & Truncate) == Truncate)
344 d->buf->resize(0);
345 d->ioIndex = (flags & Append) == Append ? d->buf->size() : 0;
346
347 return QIODevice::open(flags);
348 }
corelib的/ IO/qiodevice.cpp:
540 bool QIODevice::open(OpenMode mode)
541 {
542 Q_D(QIODevice);
543 d->openMode = mode;
544 d->pos = (mode & Append) ? size() : qint64(0);
545 d->buffer.clear();
546 d->accessMode = QIODevicePrivate::Unset;
547 d->firstRead = true;
548 #if defined QIODEVICE_DEBUG
549 printf("%p QIODevice::open(0x%x)\n", this, quint32(mode));
550 #endif
551 return true;
552 }
爲什麼你需要一個指針到你的'QIODevice'? – leemes
因爲我簡化了示例的設置代碼,所以我沒有仔細考慮。我會刪除它,因爲這是不必要的。 – cgmb
好的。我只是想知道......;) – leemes