Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved] Signal is not being recieved immediately.



  • I am having a problem where I emit a signal in a mouseMove event, but that signal does not get recieved by a slot on another object until i release the mouse.

    Details:
    I have a class called SelectableItem, which subclasses QGraphicsObject :
    @
    class SelectableItem : public QGraphicsObject {
    Q_OBJECT
    public:
    SelectableItem();

        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) = 0;
        QRectF boundingRect() const = 0;
    
    signals:
        void moveItem(QGraphicsSceneMouseEvent *e);
        void deselectAllItems();
        void deselectItem(Parts::SelectableItem*);
        void selectItem(Parts::SelectableItem*);
        void stopSelectionDrag();
        void stopMovingParts();
        void deleteSelectedItems();
    
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *e);
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e);
        void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
        void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
    
        bool m_is_selected;
        bool m_is_hovering;
        bool m_is_selectable;
    
    };@
    

    I have two other classes that subclass the SelectableItem class. They are Part and Terminal

    Part:
    @
    class Part : public SelectableItem {
    Q_OBJECT
    public:

        friend class PartLabel;
    
        Part();
    
        // Inherited Functions
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
        QRectF boundingRect() const;
        void setPos(qreal x, qreal y);
    
        QGraphicsSvgItem* GetPartSvg();
    
    signals:
        void partRotated();
    
    private:
    
        // Inherited Functions
        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e);
        void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
        void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
        void contextMenuEvent(QGraphicsSceneContextMenuEvent *e);
    
    };@
    

    Terminal:
    @
    class Terminal : public SelectableItem {
    Q_OBJECT
    public:
    enum TerminalDirection {
    North,
    East,
    South,
    West
    };

        Terminal(const std::string &name, const QPointF &rel_coords, const std::string &direction);
    
        // Inherited Functions
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
        QRectF boundingRect() const;
    
        QPointF GetRelativeCoord();
        QPointF GetConnectionCoord();
        void SetPart(Part *p);
        bool HasPart();
        void Move();
        void RotateCW();
        void RotateCCW();
        void SetIsConnected(bool b);
        bool IsConnected();
    
        void AddConnection(Connection *c);
        void RemoveConnection(Connection *c);
        std::list<Connection*> GetConnectionList();
    
    signals:
        void connectionBegun(Parts::Terminal *t);
        void connectionCompleted(Parts::Terminal *t);
    
    private:
        // Inherited Functions
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
        void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
        void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
        //void contextMenuEvent(QGraphicsSceneContextMenuEvent *e);
    
        void IncrementDirection();
        void DecrementDirection();
    
        QPointF m_rel_coords;
        QPointF m_rotated_rel_coords;
    
        bool m_is_connected;
    
        int m_direction;
        int m_rotation_angle;
    
        QRectF m_bounding_rect;
        Part *m_part;
        std::string m_name;
        std::list<Connection*> m_connections;
    
    };@
    

    The SelectableItem class's mouseMove function is as follows:
    @
    void SelectableItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
    if(!m_is_selectable)
    return;

        if(m_is_selected)
            emit moveItem(e);
        else {
            emit deselectAllItems();
            emit selectItem(this);
        }
    }@
    

    The signal emit selectItem(this) is instantly recieved by another class SysGraphicsScene which handles item selection.

    The problem I'm having is that the Part class emits this signal via its base class and the signal is recieved by the SysGraphicsScene class instantly. However, the Terminal class emits this signal via it's base class (SelectableItem, which is the same base class that Part has.) and the signal is not recieved instantly by SysGraphicsScene but only after mouseReleased.

    I have no idea why two derived classes would work differently. Any ideas?



  • Found the problem. Some of my terminals were not getting connected to signals while others where. Stupid mistake.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Glad you found out and thanks for sharing the solution. Can you please also update the thread title prepending [solved] ? So other forum users may know a solution has been found :)


Log in to reply