【问题标题】:Qt Signal and slotQt 信号和槽
【发布时间】:2012-08-22 10:19:59
【问题描述】:

我有两个函数,其中一个调用另一个。我想将其中一个用作按钮中信号 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) Object::connect: (发件人名称: 'pushButton') Object::connect: (receiver name: 'planevolume')

所以如果有人知道我做错了什么,请给我一些提示或其他东西,因为这让我发疯。 :)

【问题讨论】:

    标签: qt signals slot


    【解决方案1】:

    两个问题:

    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 不知道从哪里读取这些值。

    【讨论】:

    • 那么问题将是,如果它们在 planevolume.cpp 中声明,我将如何将输入放在这里?
    • 插槽也将在 planevolume.cpp 中。它应该知道从哪里读取值。我不知道您的应用程序设计,但我的猜测类似于ui-&gt;lineEditxMin-&gt;value()
    • 和 AlignXAxis() 在 planevolume.h 中无法识别
    • 我已经用 .h 文件和 .cpp 文件中的内容更新了我的答案
    • 谢谢,它可以工作,但是当我按下按钮时,应用程序就会关闭。但我认为这可能是另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2012-10-15
    • 2015-07-18
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多