Qt信号和槽

3

我有两个函数,其中一个调用另一个。我想将它们中的一个作为插槽来使用,连接到PushButton的clicked()信号上。以下是我的planevolume.h代码的部分内容,以便您理解:

#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

我会放在这里一些我的planevolume.cpp代码,这样你就可以理解我遇到的错误了。

#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);
    }

当我编译它时,好像一切都没有问题,但是当我点击PushButton时,应用程序的输出窗口只显示如下内容:

Object::connect: 在planevolume.cpp文件的第386行中没有名为planevolume :: AlignXAxis(int,int,vtkImagePlaneWidget)的槽。 Object::connect: (发送者名称:“pushButton”) Object::connect: (接收者名称:“planevolume”)

如果有人知道我做错了什么,请给我一些提示或建议,因为这让我感到非常疯狂。 :)

1个回答

3

两个问题:

1.) 插槽需要一个指向最后参数的指针,而不是一个对象,因此在连接中缺少*号(这是在被删除的答案中提出的建议)。然而,还有一个更大的问题:

2.) 在进行连接时,SIGNAL中的参数不能少于SLOT中的参数。

正确的方法应该像这样:

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

(Note:如果你好奇我为什么要使用这么多空格:我这样做是为了方便识别哪个对象连接到哪个其他对象,哪个信号连接到哪个槽,并且参数是否匹配)
然后像这样有一个槽:(.cpp文件)
/* 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 );
}

使用包含以下内容的.h文件:

private slots:
    void onButtonClicked();

您当前连接的问题在于clicked()信号没有为xMin、xMax和planeX提供任何值。Qt不知道从哪里读取这些值。

那么问题就在于,如果它们在planevolume.cpp中声明,我该如何在这里输入呢? - SamuelNLP
插槽也将在planevolume.cpp中。它应该知道从哪里读取值...我不知道你的应用程序设计,但我的猜测是类似于ui->lineEditxMin->value() - Tim Meyer
AlignXAxis()在planevolume.h中未被识别。 - SamuelNLP
我已经更新了我的答案,告诉你应该在.h文件和.cpp文件中放什么。 - Tim Meyer
谢谢,它可以工作,但是当我按下按钮时,应用程序就会关闭。但我认为这可能是另一个问题。 - SamuelNLP
我有另一个Q_OBJECT类,如何从pushButtonclicked()事件触发它的事件? - Junaid Qadir Shekhanzai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接