[Interest] QProgressDialog not showing & processEvents()
Mandeep Sandhu
mandeepsandhu.chd at gmail.com
Mon Nov 18 10:38:42 CET 2013
> I strongly object to using a thread for something this simple. Threading
> is to be avoided whenever possible, because it's so hard to debug. Use
There's no black magic involved in threads. Though I agree it might be
an overkill in some cases, not so sure about this case.
> the chunks method instead. Something like this:
>
> class FileLoader : public QObject {
> Q_OBJECT
>
> public:
> explicit FileLoader(const QString& fileName) {
> m_file(fileName);
> m_timer.setSingleShot(false);
> m_timer.setInterval(0);
> m_timer.start();
> connect(&m_timer, SIGNAL(triggered()), SLOT(readChunk()));
>
> m_file.open(QIODevice::ReadOnly));
> }
>
> signals:
> void done(const QByteArray& bytes);
> void failed();
> void progress(qreal percent);
>
> private slots:
> void readChunk() {
> if (!m_file.isOpen()) {
> m_timer.stop();
> emit failed();
> } else {
> m_bytes.append(m_file.read(1024));
The read/write chunk size needs to be selected with care. The exact
answer will depend on the underlying filesystem in use. I think it
should not be smaller than the file system block size.
-mandeep
More information about the Interest
mailing list