Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
From main UI thread i am creating new thread(counterThread) and in new thread i am trying to accesss QfileDialog. it gives me error msg No matching function for call.
C#
void MainWindow::on_pushButton_clicked()
{
    //Start Counter Thread
    QThread* workerThread = new QThread;   
    counterThread* worker = new counterThread();  
    worker->moveToThread(workerThread);
    
    connect(workerThread, SIGNAL(started()), worker, SLOT(processCounter()));
    
    workerThread->start(); // start the thread with name Workerthread

}


and in counterThread
void counterThread::processCounter()
{
   QString logfileName = QFileDialog::getOpenFileName(this, tr("Open File"),"C://readfiles",tr("Trace files (*.trc)"));
   
}
Posted

1 solution

This won't work in a Qt worker thread.
From http://doc.qt.io/qt-5/thread-basics.html[^]:
Quote:
As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.

You must move the GUI related operations to the main thread and perform only the time consuming operations inside the worker thread. Especially things like asking the user to select a file should be performed before starting the worker thread. In your case I would also open the file in the main thread and pass the file handle to the worker thread. But note that there must be some kind of synchronization or locking when shared objects may be used at the same time by multiple threads.
 
Share this answer
 
Comments
XamBEE 3-Dec-15 10:48am    
but how i can pass the filename string when my signal is started()

connect(workerThread, SIGNAL(started()), worker, SLOT(processCounter()));

i need the filename in the beginning of the thread.
Jochen Arndt 3-Dec-15 10:57am    
Qt has an example for threads: http://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html.

Basically you derive your own class from QThread. Then you can pass arguments via the constructor (e.g. just a pointer to your QMainWindow based class which can be used to access variables of that).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900