I´m starting multiple QProcesses from a textfile (for example different programs like notepad.exe and so on) and want to regularly check if they are still running.
A while loop isn´t the solution, since it is blocking the ui-thread.
How can this be achieved? In Java (Android) I would do this with a asynctask
connected to a ScheduldExecutorService. Is something similar available for qt?
This is where I start my processes:
void mywidget::startprocesses(QString &text)
{
QProcess *process = new QProcess(this);
this->myprocess.append(process);
process->start(text);
int state = process->state();
addlabelstatus(state);
}
And here the method is called:
while(!stream->atEnd()) //check stream until empty and assign line as a caption to a new QLabel
{
this->fileread = stream->readLine();
if(!this->fileread.isEmpty())
{
central->addlabel(this->fileread);
central->startprocesses(this->fileread);
}
}
void mywidget::addlabelstatus(QProcess::ProcessState newstate)
{
QString sstring;
if(newstate == 0)
{
QString sstring = "Wird nicht ausgeführt";
QLabel *label = new QLabel(sstring);
this->processstatus.append(label);
this->vrarea->addWidget(label);
}
else if (newstate == 1)
{
QString sstring = "Wird gestartet!";
QLabel *label = new QLabel(sstring);
this->processstatus.append(label);
this->vrarea->addWidget(label);
}
else if (newstate == 2)
{
QString sstring = "Wird ausgeführt!";
QLabel *label = new QLabel(sstring);
this->processstatus.append(label);
this->vrarea->addWidget(label);
}
else
{
QString sstring = "kein Status vorhanden!";
QLabel *label = new QLabel(sstring);
this->processstatus.append(label);
this->vrarea->addWidget(label);
}
}
Each QProcess has finished(int exitCode, QProcess::ExitStatus exitStatus) and stateChanged(QProcess::ProcessState newState) signals, that emits when some process has been terminated or changed (by type of). So your code can be:
.H side:
public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QProcess::ProcessState newState);
.CPP side:
void mywidget::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
<...>
}
void mywidget::addlabelstatus(QProcess::ProcessState newState)
{
switch(newState) {
<...>
};
}
// Your code change
QProcess *process = new QProcess(this);
this->myprocess.append(process);
connect(process, &QProcess::finished, this, &mywidget::processFinished);
connect(process, &QProcess::stateChanged, this, &mywidget::addlabelstatus);
process->start(text);
UPDATE FOR COMMENT QUESTION
You can try this way:
.H side
public slots:
void processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus);
void addlabelstatus(QLabel *label, QProcess::ProcessState newState);
.CPP side
void mywidget::processFinished(QLabel *label, int exitCode, QProcess::ExitStatus exitStatus)
{
<...>
}
void mywidget::addlabelstatus(QLabel *label, QProcess::ProcessState newState)
{
switch(newState) {
<...>
};
}
while(!stream->atEnd()) {
this->fileread = stream->readLine();
if(!this->fileread.isEmpty()) {
QLabel *label = new QLabel(this->fileread);
QProcess *process = new QProcess(this);
this->myprocess.append(process);
connect(process, &QProcess::finished, [=]
(int exitCode, QProcess::ExitStatus exitStatus)
{ processFinished(label, exitCode, exitStatus); });
connect(process, &QProcess::stateChanged, [=]
(QProcess::ProcessState newState)
{ addlabelstatus(label, newState); });
process->start(text);
}
}
A while loop isn´t the solution, since it is blocking the ui-thread.
Correct. However, since Qt is an event-driven framework, you could use a timer:-
// Assuming we have a list of processes QList<QProcess*> called timerList
QTimer* pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){
foreach(QProcess* proc, timerList)
{
// get state
int state = process->state();
// update UI
addlabelstatus(state);
}
});
pTimer->start(1000); // every second
Or alternatively, as #someoneinthebox answered, connect to each QProcess's stateChanged signal to notify you when it occurs and react to that.
Related
I Keep getting this error when i try and build this app. Help?
The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning: Segmentation fault
Here is the code for the mainwindow.cpp file
It says the error is on line 122 which is the
copyAct->setEnabled(false);
this is about half way down.
#include "mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QPlainTextEdit;
setCentralWidget(textEdit);
createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();
connect(textEdit->document(), SIGNAL(contentsChanged()),this, SLOT(documentWasModified()));
setCurrentFile("");
setUnifiedTitleAndToolBarOnMac(true);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(maybeSave()){
writeSettings();
event->accept();
}
else{
event->ignore();
}
}
void MainWindow::newFile()
{
if(maybeSave()){
textEdit->clear();
setCurrentFile("");
}
}
void MainWindow::open()
{
if(maybeSave()){
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::save()
{
if(curFile.isEmpty()){
return saveAs();
}
else{
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this);
if(fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Application"),
tr("Whatever you want it to say here!"));
}
void MainWindow::documentWasModified()
{
setWindowModified(textEdit->document()->isModified());
}
void MainWindow::createActions()
{
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save an open file"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("&Save As"), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->setStatusTip(tr("Save a new file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAct = new QAction(tr("&Exit"), this);
exitAct->setShortcuts(QKeySequence::Close);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
cutAct = new QAction(tr("&Cut"), this);
cutAct->setShortcuts(QKeySequence::Cut);
cutAct->setStatusTip(tr("Cut the selected text"));
pasteAct = new QAction(tr("&Paste"), this);
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip(tr("Paste selected text"));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(setEnabled(bool)));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setShortcuts(QKeySequence::UnknownKey);
aboutAct->setStatusTip(tr("About the application"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(show()));
cutAct->setEnabled(false);
copyAct->setEnabled(false);
connect(textEdit, SIGNAL(copyAvailable(bool)), cutAct, SLOT(setEnabled(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), copyAct, SLOT(setEnabled(bool)));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
}
void MainWindow::createStatusBar()
{
statusBar()->showMessage(tr("Ready"));
}
void MainWindow::readSettings()
{
QSettings settings("Zach Starnes", "Zach's Text Editor");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
resize(size);
move(pos);
}
void MainWindow::writeSettings()
{
QSettings settings("Zach Starnes", "Zach's Text Editor");
settings.setValue("pos", pos());
settings.setValue("size", size());
}
bool MainWindow::maybeSave()
{
if (textEdit->document()->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return save();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}
void MainWindow::loadFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QTextStream in(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
textEdit->setPlainText(in.readAll());
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
}
bool MainWindow::saveFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << textEdit->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
textEdit->document()->setModified(false);
setWindowModified(false);
QString shownName = curFile;
if (curFile.isEmpty())
shownName = "untitled.txt";
setWindowFilePath(shownName);
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
Looks like for every other action you have (which I assume are member data pointers), you've allocated some memory for:
aboutAct = new QAction(tr("&About"), this);
copyAct seems to be the only action you haven't allocated anything for, and therefore you are dereferencing an invalid pointer.
copyAct->setEnabled(false); // segfault :(
You are missing something like this, I assume:
copyAct = new QAction(tr("&Copy"), this);
I hope this isn't your entire cpp file either, as I don't see a destructor nor any delete's to match all those new commands.
I'm junior programmer
recently, I have implemented grabbing of Image using Halcon library.
when I press live button, Timer start to grab image. it works but main screen freezes to the timer cycle.
so, I am improving performance grabbing of Image using Thread
first I implemented thread like this
[ImageUpdateWorker.h]
class ImageUpdateWorker : public QObject
{
Q_OBJECT
public:
explicit ImageUpdateWorker(QObject* parent = 0, QString strThreadName = "ImageUpdateWorker");
~ImageUpdateWorker();
signals:
void finished();
void grab();
public slots:
void run();
private:
bool m_bStop{ false };
};
[ImageUpdateWorker.cpp]
ImageUpdateWorker::ImageUpdateWorker(QObject* parent, QString strThreadName)
: QObject(parent)
{
setObjectName(strThreadName);
}
ImageUpdateWorker::~ImageUpdateWorker()
{
}
void ImageUpdateWorker::run()
{
while (m_bStop == false)
{
emit grab();
}
emit finished();
}
second I implemented inherited QWidget UI Widget with output Screen like this
m_pThread = new QThread();
m_pUpdateWorker = new ImageUpdateWorker(nullptr, strName);
m_pUpdateWorker->moveToThread(m_pThread); // UpdateWorker move to Thread
connect(m_pThread, SIGNAL(started()), m_pUpdateWorker, SLOT(run()));
connect(m_pThread, SIGNAL(finished()), m_pThread, SLOT(deleteLater()));
connect(m_pUpdateWorker, SIGNAL(finished()), m_pThread, SLOT(quit()));
connect(m_pUpdateWorker, SIGNAL(finished()), m_pUpdateWorker, SLOT(deleteLater()));
connect(m_pUpdateWorker, SIGNAL(grab()), this, SLOT(onGrab()));
when I call "m_pThread->start();" screen starts to blokcing :(
If you have any advice or information, I would appreciate it. thank you for reading.
Use m_pImageUpdateThread->moveToThread(m_pThread);
I don't know in QT.
I sent you the code I used in C#.
Mainly you must use the delegates if you don't want to freeze the GUI.
hdisplay is the object HalconDotNet:HWindowControlWPF.
camera is a class where I define the camera parameters.
inside camera.Grab there is the code:
HOperatorSet.GrabImage(out ho_Image, _AcqHandle);
h_Image = new HImage(ho_Image);
At the initialization there is the code:
// Initialise the delegate
updateLiveDelegate = new UpdateLiveDelegate(this.UpdateLive);
HImage ho_Image = new HImage();
Here the code I use:
// ==================
// LIVE
// ==================
bool stopLive = true;
// Declare the thread
private Thread liveThread = null;
// Declare a delegate used to communicate with the UI thread
private delegate void UpdateLiveDelegate();
private UpdateLiveDelegate updateLiveDelegate = null;
private void btnLive_Click(object sender, RoutedEventArgs e)
{
try
{
stopLive = !stopLive;
// if stopLive = false, live camera is activated
if (!stopLive)
{
// Launch the thread
liveThread = new Thread(new ThreadStart(Live));
liveThread.Start();
}
}
catch (Exception ex)
{
// Error
}
}
private void Live()
{
try
{
while (stopLive == false)
{
if (camera.Grab(out ho_Image))
{
// Show progress
Dispatcher.Invoke(this.updateLiveDelegate);
}
else
{
// No grab
stopLive = true;
}
}
// here stopLive is true
}
catch (Exception ex)
{
// Error
}
}
private void UpdateLive()
{
try
{
int imageHeight;
int imageWidth;
string imageType;
ho_Image.GetImagePointer1(out imageType, out imageWidth, out imageHeight);
hDisplay.HalconWindow.SetPart(0, 0, imageHeight - 1, imageWidth - 1);
// display
hDisplay.HalconWindow.DispImage(ho_Image);
}
catch (Exception ex)
{
// Error
}
}
I'm Using qt Built-in Examples in /examples/activeqt/webbrowser, and i'm trying to use DocumentComplete SIGNAL.
my purpose is using embedded Internet explorer inside QAxWidget and popup a message (with contetnt from the website) after DocumentComplete. NavigateComplete Signal is not good enough for me use...
the code can be seen here : qt Web browser Example
class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void on_WebBrowser_TitleChange(const QString &title);
void on_WebBrowser_ProgressChange(int a, int b);
void on_WebBrowser_CommandStateChange(int cmd, bool on);
void on_WebBrowser_BeforeNavigate();
void on_WebBrowser_NavigateComplete(const QString &address);
void on_WebBrowser_DocumentComplete(IDispatch*,QVariant&)
void on_actionGo_triggered();
void on_actionNewWindow_triggered();
void on_actionAddBookmark_triggered();
void on_actionAbout_triggered();
void on_actionAboutQt_triggered();
void on_actionFileClose_triggered();
private:
QProgressBar *m_progressBar;
};
MainWindow::MainWindow()
{
setupUi(this);
connect(m_addressEdit, SIGNAL(returnPressed()), actionGo, SLOT(trigger()));
connect(actionBack, SIGNAL(triggered()), WebBrowser, SLOT(GoBack()));
connect(actionForward, SIGNAL(triggered()), WebBrowser, SLOT(GoForward()));
connect(actionStop, SIGNAL(triggered()), WebBrowser, SLOT(Stop()));
connect(actionRefresh, SIGNAL(triggered()), WebBrowser, SLOT(Refresh()));
connect(actionHome, SIGNAL(triggered()), WebBrowser, SLOT(GoHome()));
connect(actionSearch, SIGNAL(triggered()), WebBrowser, SLOT(GoSearch()));
connect(WebBrowser,SIGNAL(DocumentComplete(IDispatch*,QVariant&), this, SLOT(on_WebBrowser_DocumentComplete(IDispatch*,QVariant&)))
}
void MainWindow::on_WebBrowser_DocumentComplete(IDispatch*,QVariant&)
{
QMessangeBox x;
x.setText("pop-up");
x.exec();
}
the dubbuger says: No such slot MainWindow::on_WebBrowser_DocumentComplete(IDispatch*,QVariant&)
As long as the workaround needs more than comment:
MyWidgetUsesActiveX::MyWidgetUsesActiveX()
{
///
connect(m_pActiveX, SIGNAL(signal(const QString&, int, void*)),
this, SLOT(activexEventHandler(const QString&, int, void*)));
}
void MyWidgetUsesActiveX::activexEventHandler(
const QString &name, int argc, void *argv)
{
VARIANTARG *params = (VARIANTARG*)argv;
// See what events fired
qDebug() << "Event:" << name << "argc:" << argc;
// some concrete example
if (name == "OnRemoteWindowDisplayed")
{
// Extract parameters
_variant_t varValue = params[argc-1];
bool bDisplayed = (bool)varValue;
// hwnd
varValue = params[argc-2];
int iResult = (int)varValue;
varValue = params[argc-3];
int windowAttribute = (int) varValue;
// CALL that stubborn slot we cannot have called otherwise
onRemoteWindowDisplayed(bDisplayed, iResult, windowAttribute);
}
else if (name == "OnSomethingElse")
{
// extract concrete parameters and call the handler from here
// onSomethingElse(param1, param2, param3);
}
else
{
// Log it?
}
}
i solved the issue by regenerating the .moc file. under build configuration the folder location of qmake was wrong. by correcting it to project location fixed my problem. the slot is now recognized. thank all for you help.
Qt Build Configuration
I want to create a start/stop push button for starting and killing a process. So here is my code:
void MainWindow::on_pushButton_clicked(){
QProcess* ping_process = new QProcess(this);
if ( this->myTimer->isActive() == true ) {
this->myTimer->stop();
ui->pushButton->setText("Start");
//...
ping_process->start("ping", QStringList() << "8.8.8.8");
} else {
this->myTimer->start(500);
ui->pushButton->setText("Stop");
ping_process->terminate();
}
}
Starting the process is successful, and changing the text for push button is also successful. But ping_process->terminate(); is not working. I also tried kill() and close(), but nothing happened. Any help?
In this case each click on button will be create new QProcess and call start or terminate for him. Not for previcously created QProcess. QProcess must be in member area, like this:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
...
private:
...
QProcess* ping_process;
};
MainWindow::MainWindow(QWidget *parent) :
...
ping_process(nullptr),
...
{
...
}
void MainWindow::on_pushButton_clicked() {
if (!ping_process)
{
ping_process= new QProcess(this);
}
if ( this->myTimer->isActive() == true ) {
this->myTimer->stop();
ui->pushButton->setText("Start");
//...
ping_process->start("ping", QStringList() << "8.8.8.8");
} else {
this->myTimer->start(500);
ui->pushButton->setText("Stop");
ping_process->terminate();
}
}
Why every tick of QTimer creates new thread? My application needs to run as long as possible, but after xx of ticks it freeze, it still running (it's responding), but next ticks are not executed. I looked into debug info and i saw:
QThread::start: Failed to create thread () QThread::start: Failed to
create thread () QThread::start: Failed to create thread ()
QThread::start: Failed to create thread () QThread::start: Failed to
create thread () QThread::start: Failed to create thread ()
QThread::start: Failed to create thread () QThread::start: Failed to
create thread ()
waat?
Tick are executed every xx seconds, signal is located into QWidged (which is a one of tab of TabWidget)
namespace Ui {
class accountTab;
}
class accountTab : public QMainWindow
{
Q_OBJECT
public:
explicit accountTab(QWidget *parent = 0);
class player *_player;
~accountTab();
private slots:
void on_clean_timer_clicked();
public:
Ui::accountTab *ui;
};
void accountTab::on_clean_timer_clicked()
{
if(user->timers.value("clean")->isActive()) {
_player->timers.value("clean")->stop();
}
else if(!user->timers.value("clean")->isActive()) {
_player->timers.value("clean")->start(1800000); //900000
}
}
_player is a simple class.
_player->clean() execute a few static classes, which are try/catched.
player.h
class player : public QObject
{
Q_OBJECT
public:
player();
~player();
player(Ui::accountTab *tab, std::string login, std::string password);
player(Ui::accountTab *tab, User user);
public:
bool logIn();
Ui::accountTab *tab = new Ui::accountTab();
public slots:
void clean();
private:
User user;
QMap<std::string, QTimer*> timers;
void initializeTimers();
};
player.cpp
player::player(Ui::accountTab *tab, std::string login, std::string password)
{
this->tab = tab;
this->user.login = login;
this->user.password = password;
}
player::~player()
{
delete this->manager;
delete this->tab;
}
bool player::logIn()
{
...
Log::writeLog("Login completed!", *this);
return true;
}
bool player::setup(bool saved, bool save)
{
if(!this->logIn())
return false;
Packets::sendPacket("getSimulation", *this);
this->initializeTimers();
return true;
}
void player::initializeTimers()
{
this->timers.insert("clean", new QTimer(this));
connect(this->timers.value("clean"), SIGNAL(timeout()), this, SLOT(cleanZoo()));
}
void player::clean()
{
Packets::sendPacket()
}
user class in player class keeps login and password.
timers is a QMap: QMap timers;
Packets::sendPacket() is static void
and sendPacket()
QString httpManager::sendPacket()
{
QNetworkRequest request("https://www.google.pl/");
if(headers.size() > 0) {
for (QMap<const char*, const char*>::iterator i = headers.begin(); i != headers.end(); ++i)
request.setRawHeader(i.key(), i.value());
}
QNetworkAccessManager *manager = new QNetworkAccessManager();
manager->setCookieJar(this->cookies);
QNetworkReply *reply = manager->get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QList<QNetworkCookie> cookies = reply->manager()->cookieJar()->cookiesForUrl(QUrl(reply->url()));
foreach(QNetworkCookie cookie, cookies)
{
this->cookies->insertCookie(cookie);
}
return reply->readAll().data();
}
QEventLoop is executed to get response in the same void. Is this creating new threads?
There are a few things wrong here:
Your application should have a single QNetworkAccessManager that all code uses, don't create one for every call, create one in main and pass that to where it is needed.
You need to delete QNetworkReply using deleteLater as explained in the manual.
Creating another event loop in a function is generally not a good idea. Create a slot on the class httpManager connected to the QNetworkAccessManager::finished(QNetworkReply * reply) signal, read the reply and call deleteLater from here.