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. [SOLVED] How to use signal to execute any function with arguments?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to use signal to execute any function with arguments?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.8k 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.
  • O Offline
    O Offline
    OskarPyke
    wrote on last edited by OskarPyke
    #1

    Suppose I have a QPropertyAnimator animating (moving), say, a button - slightly to the left over the course of 10 seconds.

    When the button reaches its destination on the other side of the window it should change its text to "banana" using the QLineEdit::setText() function.

    If the QLineEdit::setText() function is issued directly after the animation start;

    QPropertyAnimator *animator = new QPropertyAnimator(someButton, "pos");

    animator->setDuration(10000);
    animator->setStartValue(current position of the button);
    animator->setEndValue(current position of the button with x-100);
    animator->start();

    QLineEdit::setText(QString("Banana"));

    The text changes before it has the chance to start moving. Luckily, QPropertyAnimator emits a signal when the animation is completed - aptly titled finished().

    One would like to just be able to:

    connect(animator, SIGNAL(finished()), someButton, SLOT(setText("Banana")));

    But since you can't pass an argument to a slot that won't work.

    How do I change the text after the animation is complete without creating different "proxy" functions (slots) to do it without arguments? I've been told that C++11 can be used for this purpose. How would one be integrated?

    Kind regards,
    Oskar

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jakob
      wrote on last edited by
      #2

      I don't have a ready made solution for you, but your remark about c++11 got me thinking that maybe you should have a look at std::bind, something like:

      connect(animator, &QPropertyAnimator::finished, someButton, std::bind(&QPropertyAnimator::setText, animator, "Banana"))
      
      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        You could also

        add a function/slot to your main window
        like

        void Mainwin::WhatToDoonFinish() {
        ui->someButton->setText("Banana");
        }

        and then hook up the finished with that one
        connect(animator, SIGNAL(finished()), mainwindow, SLOT(WhatToDoonFinish()));

        Edit:
        Also you can have arguments but SetText is not a slot (as far as i know) and cannot be used as such.
        You could read about signal/slots here
        http://doc.qt.io/qt-5/signalsandslots.html
        It has example that use arguments.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          OskarPyke
          wrote on last edited by
          #4

          Thanks for the responses!

          I managed to get it working by using the new QT connect() syntax in synergy with c++11 lambdas:

          connect(animator, &QPropertyAnimation::finished, [&] () {

          doSomething();

          });

          /Oskar

          mrjjM 1 Reply Last reply
          0
          • O OskarPyke

            Thanks for the responses!

            I managed to get it working by using the new QT connect() syntax in synergy with c++11 lambdas:

            connect(animator, &QPropertyAnimation::finished, [&] () {

            doSomething();

            });

            /Oskar

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

            @OskarPyke

            That is pretty cool.
            Looks a bit strange on first read when not used to lambdas that much :)

            1 Reply Last reply
            0

            • Login

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