Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to connect signals and slots inside QObject subclass?
QtWS25 Last Chance

How to connect signals and slots inside QObject subclass?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
slotsconnect failureqobjectsignals
3 Posts 3 Posters 2.9k 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
    diredko
    wrote on 15 Jan 2016, 20:34 last edited by
    #1

    Hello everyone)
    Currently I'm trying to implement simple chess board but I stuck on on a proble I don't know how to solve:

    I have a class - BoardController and I'm trying to connect its slot to view's signal but it seems it doesn't happen - slot is never called.
    When I perform connection directly in main method it works fine and slot is being called.

    Would you please tell me what is wrong with my code?

    Thanks in advance)

    BoardController.h

    class BoardController : public QObject, protected Controller
    {
    
        Q_OBJECT
    
    protected:
    
        QObject* boardView;
    
        Board& boardModel;
    
    public:
        BoardController(QQmlEngine& engine, QObject* _boardView, Board& _boardModel);
        virtual ~BoardController(){}
    
        void start();
    
        void move(int fromRow, int fromCol, int toRow, int toCol);
    
    public slots:
    
        void movePiece(int fromRow, int fromCol, int toRow, int toCol);
    
    };
    
    

    BoardController.cpp

    BoardController::BoardController(QQmlEngine& _engine, QObject* _boardView, Board& _boardModel)
        :Controller(_engine), boardView(_boardView), boardModel(_boardModel)
    {
    
    }
    
    void BoardController::start()
    {
        if(boardView)
        {
            // I'm sure the control reaches this point 
            // but movePiece slot is never called when I perform connection inside this class
            connect(boardView, SIGNAL(movePiece(int, int, int, int)), this, SLOT(movePiece(int, int, int, int)));
        }
    
        QQmlContext* context = engine.rootContext();
    
        if(context)
        {
            context->setContextProperty("myBoardModel", QVariant::fromValue(boardModel.getListModel()));
        }
    }
    
    void BoardController::move(int fromRow, int fromCol, int toRow, int toCol)
    {
        boardModel.move(fromRow, fromCol, toRow, toCol);
    }
    
    void BoardController::movePiece(int fromRow, int fromCol, int toRow, int toCol)
    {
        boardModel.move(fromRow, fromCol, toRow, toCol);
    }
    
    
    A 1 Reply Last reply 17 Jan 2016, 01:57
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Jan 2016, 23:06 last edited by
      #2

      Hi,

      You should rather do the connections in the constructor otherwise you are going to have several connections since you are adding one each time you call start.

      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 diredko
        15 Jan 2016, 20:34

        Hello everyone)
        Currently I'm trying to implement simple chess board but I stuck on on a proble I don't know how to solve:

        I have a class - BoardController and I'm trying to connect its slot to view's signal but it seems it doesn't happen - slot is never called.
        When I perform connection directly in main method it works fine and slot is being called.

        Would you please tell me what is wrong with my code?

        Thanks in advance)

        BoardController.h

        class BoardController : public QObject, protected Controller
        {
        
            Q_OBJECT
        
        protected:
        
            QObject* boardView;
        
            Board& boardModel;
        
        public:
            BoardController(QQmlEngine& engine, QObject* _boardView, Board& _boardModel);
            virtual ~BoardController(){}
        
            void start();
        
            void move(int fromRow, int fromCol, int toRow, int toCol);
        
        public slots:
        
            void movePiece(int fromRow, int fromCol, int toRow, int toCol);
        
        };
        
        

        BoardController.cpp

        BoardController::BoardController(QQmlEngine& _engine, QObject* _boardView, Board& _boardModel)
            :Controller(_engine), boardView(_boardView), boardModel(_boardModel)
        {
        
        }
        
        void BoardController::start()
        {
            if(boardView)
            {
                // I'm sure the control reaches this point 
                // but movePiece slot is never called when I perform connection inside this class
                connect(boardView, SIGNAL(movePiece(int, int, int, int)), this, SLOT(movePiece(int, int, int, int)));
            }
        
            QQmlContext* context = engine.rootContext();
        
            if(context)
            {
                context->setContextProperty("myBoardModel", QVariant::fromValue(boardModel.getListModel()));
            }
        }
        
        void BoardController::move(int fromRow, int fromCol, int toRow, int toCol)
        {
            boardModel.move(fromRow, fromCol, toRow, toCol);
        }
        
        void BoardController::movePiece(int fromRow, int fromCol, int toRow, int toCol)
        {
            boardModel.move(fromRow, fromCol, toRow, toCol);
        }
        
        
        A Offline
        A Offline
        ambershark
        wrote on 17 Jan 2016, 01:57 last edited by
        #3

        @diredko Qt will output signal/slot problems on the console. Run your application from the console and you should see something like:

        QObject::connect: No such slot QObject::movePiece(int, int, int, int)

        Or something similar if there is an issue.

        Also you can add a qDebug() << "Got here" to your connect() but like @SGaist said, you are connecting in a bad place. qDebugs() will show up on the console as well.

        If you are in windows you will have to add CONFIG += console to your project file. Linux and OSX should be fine without.

        And finally add qDebug() messages to your move and movePiece to see if it's getting in there.

        Or of course you could use the debugger for all this. ;)

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        0

        1/3

        15 Jan 2016, 20:34

        • Login

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