Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. [SOLVED]Cann't create signals ?
QtWS25 Last Chance

[SOLVED]Cann't create signals ?

Scheduled Pinned Locked Moved Installation and Deployment
5 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DiIvPa
    wrote on last edited by
    #1

    Dear Qt Forumers, I wrote simple programm to learn more about threads ssynchronization and have encountered problem when tried to build executable. The compiler couldn't find reference to one of signals SetMyText(QString, int). Could you please be so kind to help me with this bug (I waste enough time and exhausted). Thank you for help in advance. Please, take a look on codes:
    main.cpp
    @
    #include <QApplication>
    #include "ThreadAB.h"
    #include "MyThread.h"
    #include "MyString.h"
    int main(int argc, char *argv[]){

    QApplication a(argc, argv);
    //    QDebugStream cerr(std::cerr);
    //    QDebugStream cout(std::cout);
    
    ThreadAB *window = new  ThreadAB();
    window -> show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    MyThread.h

    @
    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include <QApplication>
    #include <QThread>
    class Thread : public QThread
    {
    public:
    Thread();

    void run();
    void stop();
    void setThreadName(QString);
    QString thread_name;
    int running_number;

    private:
    volatile bool stopped;

    signals:
    void SetMyText (QString, int);
    };
    #endif //MYTHREAD_H
    @

    MyThread.cpp

    @
    #include "MyThread.h"
    #include <QDebug>
    Thread::Thread()
    {
    stopped = false;
    }
    void Thread::run()
    {
    while (!stopped) {
    running_number += 1;
    if(thread_name.left(1) == "A")
    qDebug() << thread_name << ": running_number= " << running_number << endl;
    else
    qDebug() << " "
    << thread_name << ": running_number= " << running_number << endl;

      sleep(1);
    

    }
    emit SetMyText(thread_name, running_number);
    // stopped = false;
    }
    void Thread::stop()
    {
    stopped = true;
    }
    void Thread::setThreadName(QString message)
    {
    thread_name = message;
    running_number = 0;
    }
    @

    ThreadAB.h

    @
    #ifndef THREADAB_H
    #define THREADAB_H
    #include "MyThread.h"
    #include "QDialog"
    #include <QCloseEvent>
    class ThreadAB : public QDialog
    {
    Q_OBJECT

    public: 
    

    ThreadAB(QWidget *parent = 0);

    protected:
    void closeEvent(QCloseEvent *event);

    private:
    Thread *threadA;
    Thread *threadB;
    QPushButton *threadAButton;
    QPushButton *threadBButton;
    QPushButton *quitButton;

    private slots:
    void startOrStopThreadA();
    void startOrStopThreadB();

    };
    #endif //THREADAB_H
    @

    ThreadAB.cpp

    @
    #include "ThreadAB.h"
    #include "MyString.h"
    #include <QTextCodec>
    #include <QPushButton>
    #include <QHBoxLayout>
    ThreadAB::ThreadAB(QWidget *parent)
    : QDialog(parent)
    {
    QTextCodec *utfcodec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForTr(utfcodec);
    QTextCodec::setCodecForCStrings(utfcodec);

    this->setWindowTitle("Threads");
    threadA = new Thread();
    threadB = new Thread();

    threadA -> setThreadName("A thread");
    threadB -> setThreadName("B thread");
    // threadA -> running_number=0;
    // threadB -> running_number=0;
    threadAButton = new QPushButton(tr("Start A"), this);
    threadBButton = new QPushButton(tr("Start B"), this);
    quitButton = new QPushButton(tr("Quit"), this);
    quitButton -> setDefault(true);
    // layout : start
    QHBoxLayout *layout = new QHBoxLayout;
    layout -> addWidget(threadAButton);
    layout -> addWidget(threadBButton);
    layout -> addWidget(quitButton);
    this -> setLayout(layout);

    connect(threadAButton, SIGNAL(clicked()),
    this, SLOT(startOrStopThreadA()));
    connect(threadBButton, SIGNAL(clicked()),
    this, SLOT(startOrStopThreadB()));
    connect(quitButton, SIGNAL(clicked()),
    this, SLOT(close()));

    MyString *p_str =new MyString();

    QObject::connect(
    threadA, SIGNAL(SetMyText (QString, int)),
    p_str, SLOT (set_mes_str(QString, int)) );
    QObject::connect(
    threadB, SIGNAL(SetMyText (QString, int)),
    p_str, SLOT (set_mes_str(QString, int)) );

    }
    void ThreadAB::startOrStopThreadA()
    {
    if (threadA -> isRunning()) {
    threadA -> stop();
    threadAButton -> setText(tr("Start A"));
    } else {
    threadA -> start();
    threadAButton -> setText(tr("Stop A"));
    }
    }
    void ThreadAB::startOrStopThreadB()
    {
    if (threadB -> isRunning()) {
    threadB -> stop();
    threadBButton -> setText(tr("Start B"));
    } else {
    threadB -> start();
    threadBButton -> setText(tr("Stop B"));
    }
    }
    void ThreadAB::closeEvent(QCloseEvent *event)
    {
    threadA -> stop();
    threadB -> stop();
    threadA -> wait();
    threadB -> wait();
    event -> accept();
    }
    @

    MyString.h

    @
    #ifndef MySTRING_H
    #define MySTRING_H
    #include <QMessageBox>
    class MyString : public QObject
    {
    Q_OBJECT

    public:
    

    MyString();

    public slots:
    void set_mes_str(QString, int);
    void show_mes_str();

    signals:
    void mes_str_is_set();

    private:
    QString message_string;
    };
    #endif //MySTRING_H
    @

    MyString.cpp

    @
    #include "MyString.h"
    #include <QDebug>

    MyString::MyString(){
    QObject::connect(
    this, SIGNAL(mes_str_is_set()),
    this, SLOT (show_mes_str() ) );
    }

    void MyString::set_mes_str(QString thread_name, int running_number)
    {
    message_string = thread_name + "->" + running_number;
    qDebug() << "set_mes_str : message_string = " << message_string;
    emit mes_str_is_set();
    }
    void MyString::show_mes_str()/
    {
    QMessageBox msg;
    msg.setText(message_string);
    msg.exec();
    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DiIvPa
      wrote on last edited by
      #2

      Let me add: if you put in signal definition {} then compiler finds the function (not signal) ?!

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You forgot the Q_OBJECT macro in your MyThread class.

        Don't forget to re-run qmake once you added it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DiIvPa
          wrote on last edited by
          #4

          Dear SGaist, thank you very much ! It works now. With best wishes, Dmitri.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You're welcome !

            Since you signals are working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved