我有一个类,MainWindow带有指向Canvas的指针,mCanvas ......
... mainwindow.h
#include "canvas.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
//snip
private:
Canvas* mCanvas;
};
... canvas.h
class MainWindow;
class Canvas
{
public:
Canvas(MainWindow* parent);
//snip
public slots:
void doSomething();
private:
MainWindow* mParent;
};
... Canvas.cpp
Canvas::Canvas(MainWindow* parent)
{
mParent = parent;
}
void Canvas::doSomething()
{
//snip
}
在MainWindow.cpp中,在MainWindow构造函数中,我将mCanvas指向Canvas(this)。然后我尝试使用动作Canvas :: doSomething()创建一个新的QShortcut。
... MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
new QShortcut(QKeySequence(Qt::Key_BracketLeft),
mCanvas, SLOT(doSomething()) );
但是,当我尝试运行程序时,我收到错误NO MATCHING FUNCTION CALL for doSomething(),这意味着编译器不认为doSomething()存在。在新的QShortcut中,我将mCanvas写成mCanvas,* mCanvas,& mCanvas;什么都行不通。
出了什么问题?
答案 0 :(得分:0)
要在Canvas中使用信号和插槽,您需要从QObject
(在您的情况下从QWidget
)继承它并使用Q_OBJECT
宏。此外,在使用new QShortcut ...
之前,您的mCanvas未初始化。
或者做这样的事情:
auto shortcut = new QShortcut(QKeySequence(Qt::Key_BracketLeft), this);
QObject::connect(shortcut, &QShortcut::activated, this, [ this ] { mCanvas->doSomething(); } );