Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is a thread attempted to be created, when a bool value is accessed?
Forum Updated to NodeBB v4.3 + New Features

Why is a thread attempted to be created, when a bool value is accessed?

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 4 Posters 2.8k 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.
  • M micha_eleric

    QSerialPortTest.pro

    QT -= gui
    QT += widgets
    QT += core serialport
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += debug
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
    CControls.cpp \
    CGUI.cpp \
    CThreadController.cpp \
    main.cpp
    
    HEADERS += \
    CControls.h \
    CGUI.h \
    CThreadController.h \
    ui_main.h
    
    FORMS += \
    mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    DISTFILES +=
    

    main.cpp

    #include "CGUI.h"
    #include "CThreadController.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        CControls con; //CCONTROLS_H
        CGUI gui; //CGUI
        CThreadController thrCon; //CThreadController
    
        gui.SetController(&thrCon);
        thrCon.SetC1(&con);
        thrCon.startThreads();
        gui.show();
        return a.exec();
    }
    

    CGUI.h

    // cgui
    #ifndef CCLASS2_H
    #define CCLASS2_H
    
    #include <iostream>
    #include <QMainWindow>
    #include <QObject>
    #include <QThread>
    #include "ui_main.h"
    
    class CThreadController;
    
    class CGUI : public QMainWindow
    {
        Q_OBJECT
    public:
        // data
        Ui_MainWindow *m_ui;
        CThreadController *m_threadcontroller; //thread controller
    
        // constructor
        CGUI(QWidget *parent = 0);
        // destructor
        virtual ~CGUI();
    
        // get/set
        void SetController(CThreadController *controller);
    
        // functions
        void Button();
    };
    
    #endif // CCLASS2_H
    

    CGUI.cpp

    // cgui
    #include "CGUI.h"
    #include "CThreadController.h"
    
    CGUI::CGUI(QWidget *parent): QMainWindow(parent)
    {
        std::cout << "CClass2 "<< '\n' << std::flush;
        QString qs = QString::number((long long)(QThread::currentThreadId()), 16);
        std::cout << qs.toStdString() << '\n' << std::flush;
        m_ui = new Ui_MainWindow();
        m_ui->setupUi(this);
        connect(m_ui->pushButton, &QPushButton::clicked, this, &CGUI::Button);
    }
    
    CGUI::~CGUI()
    {
    }
    
    // get/set
    void CGUI::SetController(CThreadController *controller)
    {
        m_threadcontroller = controller;
    }
    
    // functions
    void CGUI::Button()
    {
        std::cout << "Button "<< '\n' << std::flush;
        QString qs = QString::number((long long)(QThread::currentThreadId()), 16);
        std::cout << qs.toStdString() << '\n' << std::flush;
        emit m_threadcontroller->EmitPortName_1();
    }
    

    CThreadController.h

    #ifndef CCLASS3_H
    #define CCLASS3_H
    
    #include <QObject>
    #include <QThread>
    #include "CControls.h"
    #include "CGUI.h"
    
    class CThreadController : public QThread
    {
        Q_OBJECT
        CControls *m_controls;
        QThread* m_threadClass1;
    
    public:
        CThreadController();
        CThreadController(QObject *parent);
        // destructor
        ~CThreadController();
    
        // get/set
        void SetC1(CControls *c1);
    
        // function
        void startThreads();
    
    signals:
        void EmitPortName_1();
    };
    
    #endif // CCLASS3_H
    

    CThreadController.cpp

    #include "CThreadController.h"
    
    CThreadController::CThreadController()
    {
        m_threadClass1 = new QThread();
    }
    
    CThreadController::CThreadController(QObject *parent = 0) : QThread(parent)
    {
        m_threadClass1 = new QThread();
    }
    
    CThreadController::~CThreadController()
    {
    
    }
    
    // get/set
    void CThreadController::SetC1(CControls *c1)
    {
        std::cout << "SetC1 "<< '\n' << std::flush;
        QString qs = QString::number((long long)(QThread::currentThreadId()), 16);
        std::cout << qs.toStdString() << '\n' << std::flush;
        m_controls = c1;
        m_controls->moveToThread(m_threadClass1);
        connect(m_threadClass1, &QThread::finished, m_controls, &QObject::deleteLater);
        connect(m_threadClass1, &QThread::finished, m_threadClass1, &QThread::deleteLater);
        m_threadClass1->start();
    }
    
    // function
    void CThreadController::startThreads()
    {
        connect(this, &CThreadController::EmitPortName_1, m_controls, &CControls::SetPortName_1);
    }
    

    CControls.h

    //CCONTROLS_H
    #ifndef CCLASS1_H
    #define CCLASS1_H
    
    #include <iostream>
    #include <QObject>
    #include <QSerialPort>
    #include <QThread>
    
    class CControls : public QObject
    {
        Q_OBJECT
    
    private:
        QSerialPort m_ser_1;
    public:
        CControls();
        CControls(QObject *parent);
        ~CControls();
    
    public slots:
        void SetPortName_1();
    };
    #endif // CCLASS1_H
    

    CControls.cpp

    //CCONTROLS_H
    #include "CControls.h"
    //#include "ui_CClass1.h"
    
    CControls::CControls() : QObject()
    {
    }
    
    CControls::CControls(QObject *parent = 0) : QObject(parent)
    {
    }
    
    CControls::~CControls()
    {
    }
    
    void CControls::SetPortName_1()
    {
        std::cout << "SetPortName_1 "<< '\n' << std::flush;
        QString qs = QString::number((long long)(QThread::currentThreadId()), 16);
        std::cout << qs.toStdString() << '\n' << std::flush;
        m_ser_1.close();
        m_ser_1.setPortName("/dev/ttyACM0");
    
        //m_ser_1.
        bool open = m_ser_1.open(QIODevice::ReadWrite);
        bool isWritable = m_ser_1.isWritable();
        std::cout << "open = " << open << '\n' << std::flush;
        std::cout << "isWritable = " << isWritable << '\n' << std::flush;
    }
    

    snippet of larger program that compiles, but snippet throws these errors:

    :-1: error: CGUI.o: in function `CGUI::Button()':
    /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CGUI.cpp:31: error: undefined reference to `CThreadController::EmitPortName_1()'
    :-1: error: CThreadController.o: in function `CThreadController::startThreads()':
    /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CThreadController.cpp:34: error: undefined reference to `CThreadController::EmitPortName_1()'
    :-1: error: collect2: error: ld returned 1 exit status
    

    i have looked at larger program, and snippet side by side, every way i could think of, but can not find what is throwing compile error.

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #12

    @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

    CThreadController::EmitPortName_1()

    You defined this function but there's no implementation for it.
    If you want to be that this is a slot you should declare it as a such.

    Also why is CThreadController a QThread?

    Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

    And as always - there is no need for a separate thread here.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    M 4 Replies Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

      CThreadController::EmitPortName_1()

      You defined this function but there's no implementation for it.
      If you want to be that this is a slot you should declare it as a such.

      Also why is CThreadController a QThread?

      Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

      And as always - there is no need for a separate thread here.

      M Offline
      M Offline
      micha_eleric
      wrote on last edited by
      #13

      @Christian-Ehrlicher the need is the gui is to run on a different thread than the controls

      Christian EhrlicherC 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

        CThreadController::EmitPortName_1()

        You defined this function but there's no implementation for it.
        If you want to be that this is a slot you should declare it as a such.

        Also why is CThreadController a QThread?

        Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

        And as always - there is no need for a separate thread here.

        M Offline
        M Offline
        micha_eleric
        wrote on last edited by
        #14

        @Christian-Ehrlicher when

        void CGUI::Button()
        

        calls

        emit m_threadcontroller->EmitPortName_1();
        

        it is linked to

        connect(this, &CThreadController::EmitPortName_1, m_controls, &CControls::SetPortName_1);
        

        which calls

        void CControls::SetPortName_1()
        

        which is suppose to be on a different thread than

        void CGUI::Button()
        
        Christian EhrlicherC 1 Reply Last reply
        0
        • M micha_eleric

          @Christian-Ehrlicher the need is the gui is to run on a different thread than the controls

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #15

          @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

          the need is the gui is to run on a different thread than the controls

          Why? Qt is asynchronous...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          1
          • M micha_eleric

            @Christian-Ehrlicher when

            void CGUI::Button()
            

            calls

            emit m_threadcontroller->EmitPortName_1();
            

            it is linked to

            connect(this, &CThreadController::EmitPortName_1, m_controls, &CControls::SetPortName_1);
            

            which calls

            void CControls::SetPortName_1()
            

            which is suppose to be on a different thread than

            void CGUI::Button()
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #16

            @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

            which calls
            void CControls::SetPortName_1()

            which is suppose to be on a different thread than

            I never said something else. Please properly read my answer, take a look when a member is a child of a QObject and make m_ser_1 a child of your CControls.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

              the need is the gui is to run on a different thread than the controls

              Why? Qt is asynchronous...

              M Offline
              M Offline
              micha_eleric
              wrote on last edited by
              #17

              @Christian-Ehrlicher because the gui locks up, until all of the controls is finished. graphics on gui will not update

              Christian EhrlicherC 1 Reply Last reply
              0
              • M micha_eleric

                @Christian-Ehrlicher because the gui locks up, until all of the controls is finished. graphics on gui will not update

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #18

                @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                because the gui locks up, until all of the controls is finished. graphics on gui will not update

                Then you're doing something wrong but a thread is also the wrong solution here.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                M 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                  because the gui locks up, until all of the controls is finished. graphics on gui will not update

                  Then you're doing something wrong but a thread is also the wrong solution here.

                  M Offline
                  M Offline
                  micha_eleric
                  wrote on last edited by
                  #19

                  @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                  @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                  because the gui locks up, until all of the controls is finished. graphics on gui will not update

                  Then you're doing something wrong but a thread is also the wrong solution here.

                  it maybe the wrong solution for this snippet, but is needed for the larger program. this snippet is a small part of a " thread attempted to be created, when a bool value is accessed"

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                    CThreadController::EmitPortName_1()

                    You defined this function but there's no implementation for it.
                    If you want to be that this is a slot you should declare it as a such.

                    Also why is CThreadController a QThread?

                    Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

                    And as always - there is no need for a separate thread here.

                    M Offline
                    M Offline
                    micha_eleric
                    wrote on last edited by micha_eleric
                    #20

                    @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                    @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                    CThreadController::EmitPortName_1()

                    You defined this function but there's no implementation for it.

                    if EmitPortName_1() is defined, then it throws a moc compile error

                    If you want to be that this is a slot you should declare it as a such.

                    thought EmitPortName_1() was a signals

                    Also why is CThreadController a QThread?

                    because CThreadController is what controls [starts, makes, do thread stuff]

                    Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

                    where is m_ser_1 attempted to be moveToThread?

                    And as always - there is no need for a separate thread here.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      micha_eleric
                      wrote on last edited by
                      #21
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        micha_eleric
                        wrote on last edited by
                        #22

                        once

                            thrCon.startThreads();
                        

                        was added to main.cpp, snippet gave same error as large program

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                          CThreadController::EmitPortName_1()

                          You defined this function but there's no implementation for it.
                          If you want to be that this is a slot you should declare it as a such.

                          Also why is CThreadController a QThread?

                          Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

                          And as always - there is no need for a separate thread here.

                          M Offline
                          M Offline
                          micha_eleric
                          wrote on last edited by
                          #23

                          @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                          Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

                          well, i ran with debug, and flagged

                          bool open = m_ser_1.open(QIODevice::ReadWrite);
                          

                          the only thing it did next was

                          Q_DECL_CONSTEXPR inline QFlags(Enum flags) Q_DECL_NOTHROW : i(Int(flags)) {}
                          

                          in qflags.h

                          i was hopping for something more. maybe somewhere that call moveToThread()

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • M micha_eleric

                            @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                            Since m_ser_1 is not a child of CControls QObject::moveToThread() will not move it to the new thread.

                            well, i ran with debug, and flagged

                            bool open = m_ser_1.open(QIODevice::ReadWrite);
                            

                            the only thing it did next was

                            Q_DECL_CONSTEXPR inline QFlags(Enum flags) Q_DECL_NOTHROW : i(Int(flags)) {}
                            

                            in qflags.h

                            i was hopping for something more. maybe somewhere that call moveToThread()

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #24

                            @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                            maybe somewhere that call moveToThread()

                            When you would simply do what I suggested you would not have the problems but since you're ignoring me - happy coding.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            M 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                              maybe somewhere that call moveToThread()

                              When you would simply do what I suggested you would not have the problems but since you're ignoring me - happy coding.

                              M Offline
                              M Offline
                              micha_eleric
                              wrote on last edited by micha_eleric
                              #25

                              @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                              @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                              maybe somewhere that call moveToThread()

                              When you would simply do what I suggested you would not have the problems but since you're ignoring me - happy coding.

                              if i was ignoring you, then i would not have replied to you

                              i keep going back over everything said, trying to figure out what was said.

                              tried to

                              class CControls : public QSerialPort
                              

                              but it gives

                              /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CControls.cpp:5: error: type ‘QObject’ is not a direct base of ‘CControls’
                                  5 | CControls::CControls() : QObject()
                                    |                          ^~~~~~~
                              

                              and it was a wild guess of making m_ser_1 a child of CControls

                              making

                              QSerialPort m_ser_1
                              

                              a child of CControls, makes no sense

                              J.HilkJ 1 Reply Last reply
                              0
                              • M micha_eleric

                                @Christian-Ehrlicher said in Why is a thread attempted to be created, when a bool value is accessed?:

                                @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                maybe somewhere that call moveToThread()

                                When you would simply do what I suggested you would not have the problems but since you're ignoring me - happy coding.

                                if i was ignoring you, then i would not have replied to you

                                i keep going back over everything said, trying to figure out what was said.

                                tried to

                                class CControls : public QSerialPort
                                

                                but it gives

                                /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CControls.cpp:5: error: type ‘QObject’ is not a direct base of ‘CControls’
                                    5 | CControls::CControls() : QObject()
                                      |                          ^~~~~~~
                                

                                and it was a wild guess of making m_ser_1 a child of CControls

                                making

                                QSerialPort m_ser_1
                                

                                a child of CControls, makes no sense

                                J.HilkJ Online
                                J.HilkJ Online
                                J.Hilk
                                Moderators
                                wrote on last edited by J.Hilk
                                #26

                                @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                tried to
                                class CControls : public QSerialPort

                                but it gives

                                /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CControls.cpp:5: error: type ‘QObject’ is not a direct base of ‘CControls’
                                5 | CControls::CControls() : QObject()
                                | ^~~~~~~

                                seems like you forgot to change your cpp file to the new baseclass, now that you have changed it.

                                @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                and it was a wild guess of making m_ser_1 a child of CControls

                                It was not, its the root of your problems, actually the root of your problems is using waitForXXX functions, but thats an other topic.

                                @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                making
                                QSerialPort m_ser_1

                                a child of CControls, makes no sense

                                do you think that changing the base class from QObject to QSerialPort would somehow reparent your serialport instance?

                                BaseClass != parent/child relation

                                -->

                                CControls::CControls() : QObject(), m_ser_1(this)
                                {
                                }
                                

                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                M 1 Reply Last reply
                                3
                                • J.HilkJ J.Hilk

                                  @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                  tried to
                                  class CControls : public QSerialPort

                                  but it gives

                                  /home/michaeleric/Qt_Creator_Projects/QSerialPortTest/CControls.cpp:5: error: type ‘QObject’ is not a direct base of ‘CControls’
                                  5 | CControls::CControls() : QObject()
                                  | ^~~~~~~

                                  seems like you forgot to change your cpp file to the new baseclass, now that you have changed it.

                                  @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                  and it was a wild guess of making m_ser_1 a child of CControls

                                  It was not, its the root of your problems, actually the root of your problems is using waitForXXX functions, but thats an other topic.

                                  @micha_eleric said in Why is a thread attempted to be created, when a bool value is accessed?:

                                  making
                                  QSerialPort m_ser_1

                                  a child of CControls, makes no sense

                                  do you think that changing the base class from QObject to QSerialPort would somehow reparent your serialport instance?

                                  BaseClass != parent/child relation

                                  -->

                                  CControls::CControls() : QObject(), m_ser_1(this)
                                  {
                                  }
                                  
                                  M Offline
                                  M Offline
                                  micha_eleric
                                  wrote on last edited by
                                  #27

                                  @J-Hilk thank you for explaining it. has one more guess of what you meant, that i was about to wild guess at. and no, i did not think what i wild guessed at would work, but worth a try.

                                  1 Reply Last reply
                                  0
                                  • M micha_eleric has marked this topic as solved on

                                  • Login

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