我正在尝试从静态方法中显示一个弹出窗口:
void MainThread::popup(void){
static klassePopup* roiPopup;
roiPopup = new SavingROI();
roiPopup->show();}
这段代码工作正常,我的窗口有两个QPushbuttons,但我不明白何时应该用一个SLOT连接SIGNAL clicked()
。
以下代码不起作用:
connect(roiPopup->getsaveROIButton(),SIGNAL(clicked()),this,SLOT(saveROI(cv::Mat)));
我知道问题不明确,但复制代码有点复杂
答案 0 :(得分:2)
让我们仔细看看你的连接:
connect( roiPopup->getsaveROIButton(), SIGNAL( clicked( ) ),
this , SLOT ( saveROI( cv::Mat ) ) );
我故意重新格式化它以展示一些事情:
clicked
连接到需要参数的插槽saveROI
。这不起作用,因为连接不知道从哪里获取cv::Mat
的值。 this
。主要问题是,为什么需要静态方法?因为你只想要一个弹出窗口?如果这是唯一的原因,这就是你通常的做法:
MainThread.h:
class klassePopup; // Note: This is a "forward declaration".
// Google it if you don't know what that is.
class MainThread : public QObject // or anything else which inherits QObject
{
public:
MainThread();
~MainThread();
public:
void popup(); // not static
private slots:
saveROI(); // no parameter
private:
klassePopup* _roiPopup;
};
MainThread.cpp:
#include "klassePopup.h"
#include "SavingROI.h"
MainThread::MainThread() :
_roiPopup( new SavingROI() )
{
bool bConnectionSucceeded
= connect( _roiPopup->getsaveROIButton(), SIGNAL( clicked() ),
( this , SLOT ( saveROI() ) );
}
MainThread::~MainThread()
{
delete _roiPopup;
}
void MainThread::popup()
{
_roiPopup.show();
}
void MainThread::saveROI()
{
// retrieve your cv::mat parameter from somewhere else
}
此解决方案可行,但如果您想从静态方法调用它,则必须在该静态方法中创建MainThread实例。