PDA

View Full Version : Object::connect: No such slot



kango
20th February 2013, 08:46
Hi, my program is about animating the motion of the upper arm. currently there is 4 objects on my GUI. one is panelGL which displays the animation. then there is the horizontal slider, play button and reset button. i will first like to connect the slider to my panelGL so that the animation can be controlled by the slider. then i will like to connect the reset button and play button to the slider. the reset button will reset the value of the slider and thus also reset the animation to the initial position. the play button will start moving the slider, thus resulting in the animation of the arm from start till the end.

the problem i'm currently facing is as follows

Object::connect: No such slot MyPanelOpenGL::counterChanged(int) in mainwindow.cpp:19
Object::connect: (sender name: 'horizontalSlider')
Object::connect: (receiver name: 'panelGL')
Object::connect: No such slot QSlider::SetSliderValue() in mainwindow.cpp:20
Object::connect: (sender name: 'ResetButton')
Object::connect: (receiver name: 'horizontalSlider')
Object::connect: No such slot QSlider::animate() in mainwindow.cpp:21
Object::connect: (sender name: 'PlayButton')
Object::connect: (receiver name: 'horizontalSlider')

Any help will be appreciated and my code is

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include "main.h"
#include <QDebug>



extern QList<FileData> points;
int counter=0;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);


connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),ui->panelGL, SLOT(counterChanged(int)));
connect(ui->ResetButton,SIGNAL(clicked()), ui->horizontalSlider, SLOT(SetSliderValue()));
connect(ui->PlayButton,SIGNAL(clicked()), ui->horizontalSlider, SLOT(animate()));

}

void MainWindow::counterChanged(int counter)
{
counter = ui->horizontalSlider->value();
}

void MainWindow::SetSliderValue()
{

ui->horizontalSlider->setValue(0);

}


void MainWindow::animate()
{

if ( counter < points.size()-1 )
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
counter++;
timer->start(1000);

}
}




MainWindow::~MainWindow()
{
delete ui;
}

Zlatomir
20th February 2013, 08:59
For QSlider the slot is setSliderPosition (http://qt-project.org/doc/qt-4.8/qabstractslider.html#sliderPosition-prop) or setValue (http://qt-project.org/doc/qt-4.8/qabstractslider.html#value-prop) and both need parameters passed (so can't be connected directly with clicked, so connect clicked to a slot of yours from where you can access whatever value you want to set to the slider) and QSlider doesn't have an animate().

And make sure that panelGL has counterChanged(int)

Lykurg
20th February 2013, 08:59
it has to be
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),this, SLOT(counterChanged(int)));
connect(ui->ResetButton,SIGNAL(clicked()), this, SLOT(SetSliderValue()));
connect(ui->PlayButton,SIGNAL(clicked()), this, SLOT(animate()));

kango
20th February 2013, 09:16
it has to be
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),this, SLOT(counterChanged(int)));
connect(ui->ResetButton,SIGNAL(clicked()), this, SLOT(SetSliderValue()));
connect(ui->PlayButton,SIGNAL(clicked()), this, SLOT(animate()));

If i replace with "this", how do they know which object the slots belong to?

Lykurg
20th February 2013, 09:19
They belong to "this". The third argument is the receiver.

kango
20th February 2013, 09:22
For QSlider the slot is setSliderPosition (http://qt-project.org/doc/qt-4.8/qabstractslider.html#sliderPosition-prop) or setValue (http://qt-project.org/doc/qt-4.8/qabstractslider.html#value-prop) and both need parameters passed (so can't be connected directly with clicked, so connect clicked to a slot of yours from where you can access whatever value you want to set to the slider) and QSlider doesn't have an animate().

And make sure that panelGL has counterChanged(int)

can you provide an example on connecting click() to a slot if i need to reset the slider's value to 0?

setSliderPosition(), counterChanged() and animate() are custom slots which i have created. Can i not use custom slots?


They belong to "this". The third argument is the receiver.

sorry but i don't really get what you mean. shouldn't i connect the signal of the object i'm sending from to the slot of the other object i'm receiving?
what do you mean the third argument is the receiver?

Lykurg
20th February 2013, 09:29
Ok, explained a little bit more: you have two classes derived from QObject which is needed that signal and slots works.
SomeClass *sender = new SomeClass;
SomeOtherClass *receiver = new SomeOtherClass;
Now if you want connect them you can connect a signal of sender to a slot of reciever. (Of course sender and receiver can be the same.) So you do
connect(sender, SIGNAL(signalOfSender()), receiver, SLOT(slotOfReceiver()));

In your case you want connect to slots of the same class where you establish the connection. So you need a pointer like "sender". This is "this" in your case.

EDIT: "this" is a pointer to the class where you use "this".

kango
20th February 2013, 09:41
sorry but i still don't really get what you mean as i'm new to programming? how do the signal know which object the slots belong to if i use "this"?
is there alot more changes needed to be done to my code?

Lykurg
20th February 2013, 10:00
As I told: you only have to use "this". Then your program should work.

Another try: The signal knows nothing. It is simply emitted. With the connect syntax you "catch" a signal defining the actual initialization of a class (pointer) and the signal. (sender and signalOfSender in my example) And then you bind that signal to a slot which is again defined by a pointer to a class and its slot (receiver and slotOfReceiver).
In your case you want to connect to slots of MainWindow. Therefore you need a pointer to it which you don't have. Therefore use "this" which is a pointer to the actual instance of MainWindow.