0

I have a QThread that starts an external Linux binary. I use connect() to call a slot(), which is part of the QThread, whenever I get an output from that external Linux binary. This seems not to work for me. Can anyone tell me what is wrong in the below code?

class ThreadPowerSubSystem : public QThread
{

public:
ThreadPowerSubSystem(){ }

private slots:
void checkForTwoSecPress()
{
    qWarning("button pressed");
}

private:


void run()
{
    QProcess *powerProcess = new QProcess();
    powerProcess->start("/home/testApp/button");
    connect(powerProcess, SIGNAL(readyRead()), this, SLOT(checkForTwoSecPress()));
    exec();
}
};

Also I need to call a function to display a dialog inside that slot. Can anyone show me an example on how to do it?

Mat
  • 202,337
  • 40
  • 393
  • 406
jxgn
  • 741
  • 2
  • 15
  • 36
  • 1
    Do not inherit from QThread, unless you want to change how Qt manages threads. I recommend reading this article and following its method of using QThread: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ – TheDarkKnight Apr 20 '15 at 15:11
  • 1
    Why do you use threads, anyway? Usually you can use `QProcess` in the main thread without problems, as it operates asynchronously. – Pavel Strakhov Apr 20 '15 at 16:16
  • 1
    @TheDarkKnight you are not constructive. It is not necessary to follow your guideline at all. Way of implementing multithreading depends on exact situation. There are no mandatory templates like "create an worker and call moveToThread". Stop imposing this thing... Anyway, your comment is completely not related to the question – Dmitry Sazonov Apr 20 '15 at 16:37
  • 1
    1) execute() is a static function and does not do anything with powerProcess. thus readyRead() will never be emitted. 2) You cannot show dialogs outside the main UI thread, you would need to trigger it in the main thread e.g. via a cross-thread signal/slot connection. – Frank Osterfeld Apr 20 '15 at 21:29
  • Sorry about that. I'll try with start. I am aware of UI threads can not be accessed from another thread and that's why I am looking for a way to do that? Can you show me an example with signals and slots? – jxgn Apr 21 '15 at 03:04
  • @FrankOsterfeld When i use start() to call the external binary, it never launches that binary. – jxgn Apr 21 '15 at 05:06
  • @SaZ, so what you're saying is that I'm not entitled to an opinion?! And you state MY comment is not constructive! I wasn't imposing anything and they are not my guidelines. This is also the view of some of the Qt developers. Perhaps you would be better off making a useful comment to the OP, rather than simply criticising the opinions of others. – TheDarkKnight Apr 21 '15 at 07:57
  • You comment begins with words "DO NOT...". If you want to point on something, you should write "There are good practice in multithreading with Qt ". Comments are for discussion. Answers are for something usefull. – Dmitry Sazonov Apr 21 '15 at 16:41

0 Answers0