我有两个函数,其中一个调用另一个。我想将其中一个用作按钮中信号 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);
}
所以它们编译正常,但是当我点击按钮时,它只在应用程序的输出中显示:
Object::connect: planevolume.cpp:386 中没有这样的插槽 planevolume::AlignXAxis(int, int, vtkImagePlaneWidget) 对象::连接:(发件人姓名:'pushButton') 对象::连接:(接收者名称:'planevolume')
所以如果有人知道我做错了什么,请给我一些提示或其他东西,因为这让我发疯。 :)
最佳答案
两个问题:
1.) 插槽需要最后一个参数的指针而不是对象,因此连接中缺少 * (这在同时删除的答案中提出)。然而,还有一个更大的问题:
2.) 进行连接时,SIGNAL 中的参数不可能少于 SLOT 中的参数
正确的做法应该是这样的:
connect( ui->pushButton, SIGNAL( clicked () ),
this , SLOT ( onButtonClicked() ) )
(注意:如果你好奇为什么我为此使用这么多空格:我这样做是为了我可以很容易地发现哪个对象连接到另一个对象,哪个信号连接到哪个插槽以及参数是否匹配)
然后有一个像这样的插槽:(.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 不知道从哪里读取这些值。
关于Qt 信号与槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071016/