0

I want to pass a QString to a thread.Using this answer, Here is my code: in MainWindow.cpp:

mmthread = new mythread;
        mmthread->start();

        connect(this,SIGNAL(sendtothread(QString)),mmthread,SLOT(getfrom_main(QString)),Qt::QueuedConnection);

        emit sendtothread(mystr);

in mainwindow.h:

signals:
    void sendtothread(QString);

in mythread.cpp:

void mythread::getfrom_main(QString str)
{
    //something
}

in mythread.h:

public slots:
    void getfrom_main(QString);

But it seems getfrom_main is not called at all. Where is my mistake?

EDIT:

I have 3 similar threads like this:

in mythread.cpp:

mythread :: mythread()
{
    moveToThread(this);
}
void mythread::run(){
    //something1
}
void mythread::getfrom_main(QString comm)
{
    comment = comm;
    emit message(comment);
}

in mythread.h:

    class mythread : public QThread
{
    Q_OBJECT
public:
    explicit mythread();
    void run();
signals:
    void message (QString);
private:
       QString comment;
public slots:
    void getfrom_main(QString);
};

something1 always executes in all my threads.but not about getfrom_main.Thanks.

Community
  • 1
  • 1
lord.h
  • 153
  • 3
  • 14
  • Do you have in both classes the Q_OBJECT macro? Without this macro, signals and slots don't work. – mtb Jul 12 '16 at 11:25
  • @mtb Yes. both classes! I have a signal/slot for showing thread's Qstrings comments by a QMessagebox in mainwindow and it works perfect! – lord.h Jul 12 '16 at 11:27
  • 1
    Please, show an SSCCE: http://sscce.org/ – Dmitry Sazonov Jul 12 '16 at 11:32
  • Try starting thread after you connect signal and slot. – Anže Jul 12 '16 at 11:34
  • @Anže. Nothing changed! – lord.h Jul 12 '16 at 11:38
  • @DmitrySazonov I don't like to read long texts :) ... It means I give a complete project that just has this part and this issue on it? – lord.h Jul 12 '16 at 11:40
  • The code that you provide should work correct. Maybe the problem is in the part of the code that you do not provide. So, just as a suggestion, put the complete files (.h and .cpp) if there are not to big. – mtb Jul 12 '16 at 11:51
  • @mtb i will do that, but what about these answers? – lord.h Jul 12 '16 at 12:00
  • @lord.h that means, that your question couldn't be answered, because you didn't do enough research by yourself and didn't provide enough information. – Dmitry Sazonov Jul 12 '16 at 12:03
  • Btw, are there any warning messages in output? – Dmitry Sazonov Jul 12 '16 at 12:11
  • @DmitrySazonov No there are not... Btw it seems that your are the boss! :) ... I wrote what is needed from my code clearly and also I linked similar question... anyway thank you :) – lord.h Jul 12 '16 at 12:21
  • What is the class `mythread` - can you post the complete class? - for example have you base-classed QThread and overridden the `run()` at all? - The example you point to seems to be very different from your code. We need to see how you start your thread - it could be that the thread is not even running because you have not called `exec()` or something like this - but we can't tell you about it because we can't see the code - just post all of it up - its been 20 hours since people have asked for more info.... come on, shake-a-leg : ) – code_fodder Jul 13 '16 at 07:37
  • @code_fodder .. I edited the question. thanks :) – lord.h Jul 13 '16 at 08:20
  • @lord.h you need to read Qt documentation, because you misunderstood a lot: http://doc.qt.io/qt-5/qthread.html – Dmitry Sazonov Jul 13 '16 at 08:25

1 Answers1

1

Wrong:

mythread :: mythread()
{
    moveToThread(this); // you don't need to do it
}

Wrong (you really don't need to inherit QThread in your code):

void mythread::run()
{
    //something1
    // after "something" you need to run an event loop:
    exec();
}

exec() will run an event loop that will process all your signals and slots.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • Thanks :)... I am really beginner in Qt programming and also C++. My expert skill is not programming and now I forced to create some GUI for my purpose. So you are right about me. But look at you my friend!Excuse me but I hope other guys in `stackoverflow.com` don't be like you! You could just saying that `exec() will run an event loop . just try that!` :) ... and it worked for me without changing anything else!! I used `moveToThread(this);` after some problems and I need it :) .. so please change your answer, then i can accept that. – lord.h Jul 13 '16 at 08:36
  • I'm not a mind reader. I thing that my answer is complete and good enough (because it solved your problem). It will be wrong to modify it, because someone else may have similar problems. Feel free to leave it unaccepted. I don't care about rating/reputation ;) – Dmitry Sazonov Jul 13 '16 at 08:45
  • 1
    @lord.h ahhh... new friends : ) - it is as I suspected in my comment - you just missed calling `exec()` in `run()` - if you are not going to populate run() with any tasks then you can omit overriding this function because the default (base-class) run() function calls exec(). Without this call your thread runs, but very briefly (with no event loop) - in fact it finishes before you emit your signal and and so there is no possibility it can receive the signal. – code_fodder Jul 13 '16 at 10:36
  • @code_fodder ...thanks :) .Now I can understand what I wrote :) – lord.h Jul 13 '16 at 10:56