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.2k 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 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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 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(); });
        
        mrjjM 1 Reply Last reply
        0
        • U user4592357

          the actual name is different

          and absolutely no, this is what i do

          auto pWidget = new Widget;
          connect(pWidget, &Widget::leftClicked, [this] { onLeftClicked(); });
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #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
          5
          • mrjjM mrjj

            @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 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?

            mrjjM 1 Reply Last reply
            0
            • U user4592357

              @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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              2

              • Login

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