SamuelNLP
SamuelNLP

Reputation: 4136

Qt Signal and slot

i have two functions and one of them call the other one. I want to use one of them as a slot to the signal clicked() in a pushbutton so here's part of my planevolume.h code so you can understand:

#ifndef PLANEVOLUME_H
#define PLANEVOLUME_H

#include <QMainWindow>

namespace Ui {
    class planevolume;
}

//more stuff here

class planevolume : public QMainWindow {
    Q_OBJECT
public:
    planevolume(QWidget *parent = 0);
    ~planevolume();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::planevolume *ui;
//more stuff here


public slots:
    void AlignXAxis(int xmin, int xmax, vtkImagePlaneWidget* planeX);
    void AlignCamera(vtkImagePlaneWidget* current_widget);

};

#endif // PLANEVOLUME_H

And I'll put here some of my planevolume.cpp so you can understand what's the error that i get afetr it.

#include <qwidget.h>
#include <qaction.h>
#include <qobject.h>

//more includes

// Define AlignCamera
void planevolume::AlignCamera(vtkImagePlaneWidget* current_widget)
    {
    //regular statements of a function

        vtkCamera *camera=ren->GetActiveCamera();
        camera->SetViewUp(vx, vy, vz);
        camera->SetFocalPoint(cx, cy, cz);
        camera->SetPosition(px, py, pz);
        camera->OrthogonalizeViewUp();
        ren->ResetCameraClippingRange();
        renWin->Render();
    }

// Define the action of AlignXAxis
void planevolume::AlignXAxis(int xmin, int xmax, vtkImagePlaneWidget* planeX)
    {
        //regular statements of a function
        vtkImagePlaneWidget *current_widget= planeX;
        ui->horizontalScrollBar->setValue(slice_number);
        ui->horizontalScrollBar->setMinimum(xmin);
        ui->horizontalScrollBar->setMaximum(xmax);
        AlignCamera(current_widget);//here I call the other one
    }
planevolume::planevolume(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::planevolume)

{
    ui->setupUi(this);

    //regular stuff
//I think here is the problem when i make the connect
        connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(AlignXAxis(int, int, vtkImagePlaneWidget)));


    // Start the initialization and rendering
        renWin->Render();
        //iren->Initialize();
        //iren->Start();

    // Assign the rendering window to the qvtkwidget
      ui->qvtkWidget->SetRenderWindow(renWin);
    }

so them it compiles ok, but when i clicked in the pushbutton it just shows this on the output of the application:

Object::connect: No such slot planevolume::AlignXAxis(int, int, vtkImagePlaneWidget) in planevolume.cpp:386 Object::connect: (sender name: 'pushButton') Object::connect: (receiver name: 'planevolume')

so if someone knows what I'm doing wrong please give me some hint or something because this is driving me crazy. :)

Upvotes: 3

Views: 541

Answers (1)

Tim Meyer
Tim Meyer

Reputation: 12600

Two problems:

1.) The slot needs a pointer for the last argument rather than an object, so the * is missing in the connection (this was suggested in a meanwhile deleted answer). However, there is a much bigger issue:

2.) When doing a connection, it is not possible to have less arguments in the SIGNAL than in the SLOT

The right way would be something like this:

connect( ui->pushButton, SIGNAL( clicked        () ),
         this          , SLOT  ( onButtonClicked() ) )

(Note: If you are curious why I use so many spaces for this: I do this so i can easily spot which object is connect to which other object, which signal is connected to which slot and if the arguments match)

and then have a slot like this: (.cpp file)

/* private slot */
void planevolume::onButtonClicked()
{
    int xMin = /* get the value from somewhere */
    int xMax = /* get the value from somewhere */
    vtkImagePlaneWidget* planeX = /* get the value from somewhere */
    AlignXAxis( xMin, xMax, planeX );
}

with the .h file containing:

private slots:
    void onButtonClicked();

The problem about your current connection is that the clicked() signal does not supply any values for xMin, xMax and planeX. Qt does not know where to read these values from.

Upvotes: 3

Related Questions