Cannot queue argument of type "QTextBlock"
-
Hi! I have decided to use
QProgressDialog
to monitor loading of big text file, but I have some issue withQProgressBar
.Code:
QProgressDialog progressDialog; progressDialog.setWindowTitle(QObject::tr("Test")); progressDialog.setFixedSize(350, 100); progressDialog.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); progressDialog.setCancelButton(false); QProgressBar *progressBar = new QProgressBar(&progressDialog); progressBar->setTextVisible(false); progressDialog.setBar(progressBar); progressDialog.setMinimum(0); progressDialog.setValue(0); progressDialog.setMaximum(100); progressDialog.setLabelText(QObject::tr("Loading data. Please wait...")); QFutureWatcher<void> watcher; connect(&progressDialog, &QProgressDialog::canceled, &watcher, &QFutureWatcher<void>::cancel); connect(&watcher, &QFutureWatcher<void>::finished, &progressDialog, &QProgressDialog::reset); //connect(&watcher, &QFutureWatcher<void>::progressRangeChanged, &progressDialog, &QProgressDialog::setRange); connect(&watcher, &QFutureWatcher<void>::progressValueChanged, &progressDialog, &QProgressDialog::setValue); QFuture<void> future = QtConcurrent::run(this, &Test::loadDataFile); watcher.setFuture(future); progressDialog.exec(); watcher.waitForFinished();
void Test::loadDataFile() { ui->plainTextEdit->setPlainText(dbTextStream.readAll()); }
The problem is that it displays empty progress or when I use
connect(&watcher, &QFutureWatcher<void>::progressRangeChanged, &progressDialog, &QProgressDialog::setRange);
it displays a busy indicator. I want to display standard progressbar, like in the image below. Also I get warnings:QObject::connect: Cannot queue arguments of type 'QTextBlock' (Make sure 'QTextBlock' is registered using qRegisterMetaType().) QObject: Cannot create children for a parent that is in a different thread. (Parent is QTextDocument(0x5a28e48), parent's thread is QThread(0x8dbcc0), current thread is QThread(0x5a1f700) QObject::connect: Cannot queue arguments of type 'QTextCursor' (Make sure 'QTextCursor' is registered using qRegisterMetaType().)
How to fix it?Image:
-
Hi,
That question was unrelated to your other topic so I moved to its own thread.
The error itself is pretty clear: QTextBlock is unknown to the Qt meta type system so you have to make it known to it.
You should take a look at Qt's documentation for qRegisterMetaType for more information.
-
Did you forgot the
Q_DECLARE_METATYPE
?By the way, remove the string parameter from your
qRegisterMetaType
calls. That version of the function should only be used to register aliases. -
I have changed to:
qRegisterMetaType<QTextBlock>(); qRegisterMetaType<QTextCursor>();
but now I need to declare
Q_DECLARE_METATYPE
I have set it outsize the class in a header:
Q_DECLARE_METATYPE(QTextBlock)
But it displays error:
Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
Where I should declare
Q_DECLARE_METATYPE
? -
Before your calls to qRegisterMetaType. You can do it in your main.cpp file before the main implementation.
-
Hi! I have fixed it by implementing
QThread
withworker
class and passing the file name argument toQMetaObject::invokeMethod
function and now I don't need to display theQProgressDialog
.