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. casting sender() to wanted type returns nullptr
Forum Updated to NodeBB v4.3 + New Features

casting sender() to wanted type returns nullptr

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.3k Views 2 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on 9 Jan 2019, 18:50 last edited by
    #1

    in my event handling function i have such code:

    void Widget::mousePressEvent(QMouseEvent *event)
    {
    	const auto button = event->button();
    	if (button == Qt::LeftButton)
    	{
    		emit leftClicked(getName());
    	}
            ...
    }
    
    QString Widget::getName() const
    {
             // return some string
    }
    

    then, somewhere else i connect the signal to a slot, where i use the passed string.
    now, if i write the above code as

    void Widget::mousePressEvent(QMouseEvent *event)
    {
    	const auto button = event->button();
    	if (button == Qt::LeftButton)
    	{
    		emit leftClicked(); // <---- not passing the argument
    	}
            ...
    }
    

    and in the connected slot do this

    void OtherWidget::onLeftClicked()
    {
         if (auto widget = qobject_cast<Widget *>(sender())) // <--- widget == nullptr
             // use widget->getName() analogously 
    }
    

    what's wring here?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Jan 2019, 19:35 last edited by
      #2

      Hi
      The type of sender() cannot be altered by omitting a parameter.
      Something else must be going on.

      Do you connect onLeftClicked to any other object than a Widget? ( bad name btw :)

      1 Reply Last reply
      1
      • U Offline
        U Offline
        user4592357
        wrote on 9 Jan 2019, 19:39 last edited by
        #3

        the actual name is different

        and absolutely no, this is what i do

        auto pWidget = new Widget;
        connect(pWidget, &Widget::leftClicked, [this] { onLeftClicked(); });
        
        M 1 Reply Last reply 9 Jan 2019, 19:41
        0
        • U user4592357
          9 Jan 2019, 19:39

          the actual name is different

          and absolutely no, this is what i do

          auto pWidget = new Widget;
          connect(pWidget, &Widget::leftClicked, [this] { onLeftClicked(); });
          
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Jan 2019, 19:41 last edited by mrjj 1 Sept 2019, 19:42
          #4

          @user4592357
          Ah, its a lambda.
          please see
          https://forum.qt.io/topic/30568/how-to-get-the-sender-in-a-c-lambda-signal-slot-handler/5

          Basically you have to capture the sender manually.

          U 1 Reply Last reply 9 Jan 2019, 20:15
          5
          • M mrjj
            9 Jan 2019, 19:41

            @user4592357
            Ah, its a lambda.
            please see
            https://forum.qt.io/topic/30568/how-to-get-the-sender-in-a-c-lambda-signal-slot-handler/5

            Basically you have to capture the sender manually.

            U Offline
            U Offline
            user4592357
            wrote on 9 Jan 2019, 20:15 last edited by
            #5

            @mrjj
            oh thanks!
            i'm on mobile so can't try to run the code.
            if i capture the sender, and call the slot from within lambda, and then get sender in that slot, will that be okay? or i need to use the sender object directly in lambda body?

            M 1 Reply Last reply 9 Jan 2019, 20:27
            0
            • U user4592357
              9 Jan 2019, 20:15

              @mrjj
              oh thanks!
              i'm on mobile so can't try to run the code.
              if i capture the sender, and call the slot from within lambda, and then get sender in that slot, will that be okay? or i need to use the sender object directly in lambda body?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Jan 2019, 20:27 last edited by
              #6

              @user4592357
              You use the captured sender directly in the lambda.

              1 Reply Last reply
              2

              2/6

              9 Jan 2019, 19:35

              4 unread
              • Login

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