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. error: static assertion failed: Signal and slot arguments are not compatible
Forum Updated to NodeBB v4.3 + New Features

error: static assertion failed: Signal and slot arguments are not compatible

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.5k Views 1 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.
  • I Offline
    I Offline
    Iftekhar
    wrote on last edited by
    #1

    I am getting this error with connect function

    QTimer timer;
           QObject::connect(&timer, &QTimer::timeout,this, &MainWindow::draw);
           timer.start(1000);t
    

    both timeout and draw are predefined functions. How can I solve this? Even If I use a different function like sprite = new Sprite(this);

    QTimer timer;
       QObject::connect(&timer, &QTimer::timeout,sprite, &Sprite::drawNextBall);
       timer.start(1000);
    
    void Sprite::drawNextBall(QPainter &painter)
    {
        qDebug() <<"Sprite::drawNextBall() called";
        painter.drawEllipse(x, y, 15, 15);
    }
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Hi, welcome to the forum.

      As the error says - the signal and slot arguments need to match as the argument is passed from one to the other. In this case QTimer::timeout does not have any arguments and Sprite::drawNextBall takes a QPainter. There's no way Qt can automatically create a painter for you that you'd like. You have to do it yourself.

      So to solve it you need a function that doesn't take any parameters. Inside you can create a painter that you want and call drawNextBall with it. Alternatively you can replace the slot in that connect statement with a parameterless lambda and do the same inside it - create a painter and call the thing you want with it.

      Also, with painting you don't usually call draw functions directly, but rather schedule a paint event and let Qt call paintEvent at the right time with the right arguments. So something like

      QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);
      

      update() is a function that schedules a paint event, so you can override paintEvent method and do your drawing there.

      Christian EhrlicherC I 2 Replies Last reply
      3
      • Chris KawaC Chris Kawa

        Hi, welcome to the forum.

        As the error says - the signal and slot arguments need to match as the argument is passed from one to the other. In this case QTimer::timeout does not have any arguments and Sprite::drawNextBall takes a QPainter. There's no way Qt can automatically create a painter for you that you'd like. You have to do it yourself.

        So to solve it you need a function that doesn't take any parameters. Inside you can create a painter that you want and call drawNextBall with it. Alternatively you can replace the slot in that connect statement with a parameterless lambda and do the same inside it - create a painter and call the thing you want with it.

        Also, with painting you don't usually call draw functions directly, but rather schedule a paint event and let Qt call paintEvent at the right time with the right arguments. So something like

        QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);
        

        update() is a function that schedules a paint event, so you can override paintEvent method and do your drawing there.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #3

        @Chris-Kawa said in error: static assertion failed: Signal and slot arguments are not compatible:

        QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);

        Nearly, sadly QWidget::update() has some overloads so

        QObject::connect(&timer, &QTimer::timeout, this, qOverload<>(&MainWindow::update));

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        I 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          @Chris-Kawa said in error: static assertion failed: Signal and slot arguments are not compatible:

          QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);

          Nearly, sadly QWidget::update() has some overloads so

          QObject::connect(&timer, &QTimer::timeout, this, qOverload<>(&MainWindow::update));

          I Offline
          I Offline
          Iftekhar
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in error: static assertion failed: Signal and slot arguments are not compatible:

          QObject::connect(&timer, &QTimer::timeout, this, qOverload:<>:of(&MainWindow::update));

          I get an error if I use the above connect function

          mainwindow.cpp:15:54: error: use of undeclared identifier 'qOverload'
          
          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            Then your compiler is to old - use QOverload

            https://doc.qt.io/qt-5/qtglobal.html#qOverload

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • Chris KawaC Chris Kawa

              Hi, welcome to the forum.

              As the error says - the signal and slot arguments need to match as the argument is passed from one to the other. In this case QTimer::timeout does not have any arguments and Sprite::drawNextBall takes a QPainter. There's no way Qt can automatically create a painter for you that you'd like. You have to do it yourself.

              So to solve it you need a function that doesn't take any parameters. Inside you can create a painter that you want and call drawNextBall with it. Alternatively you can replace the slot in that connect statement with a parameterless lambda and do the same inside it - create a painter and call the thing you want with it.

              Also, with painting you don't usually call draw functions directly, but rather schedule a paint event and let Qt call paintEvent at the right time with the right arguments. So something like

              QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);
              

              update() is a function that schedules a paint event, so you can override paintEvent method and do your drawing there.

              I Offline
              I Offline
              Iftekhar
              wrote on last edited by
              #6

              @Chris-Kawa
              I have already overridden the paintEvent() in the Mainwindow class.
              Please refer to this post and provide some help
              https://forum.qt.io/topic/135315/ball-creation-at-regular-intervals-falling-ball-game/2

              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