Qt找不到插槽
Qt not finding slot
我有一个具有插槽fileNew()
的基本窗口。当我运行我的应用程序,我得到以下错误:
QObject::connect: No such slot QMainWindow::fileNew()
为什么找不到插槽?
SdiWindow.h
class SdiWindow : public QMainWindow
{
public:
SdiWindow(QWidget * parent = 0);
private slots:
void fileNew();
private:
QTextEdit * docWidget;
QAction * newAction;
void createActions();
void createMenus();
};
SdiWindow.cpp
SdiWindow::SdiWindow(QWidget * parent) : QMainWindow(parent)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle( QString("%1[*] - %2").arg("unnamed").arg("SDI") );
docWidget = new QTextEdit(this);
setCentralWidget(docWidget);
connect(
docWidget->document(), SIGNAL(modificationChanged(bool)),
this, SLOT(setWindowModified(bool))
);
createActions();
createMenus();
statusBar()->showMessage("Done");
}
void SdiWindow::createActions()
{
newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this );
newAction->setShortcut( tr("Ctrl+N") );
newAction->setStatusTip( tr("Create a new document") );
connect(newAction, SIGNAL(triggered()), this, SLOT(fileNew()));
}
void SdiWindow::createMenus()
{
QMenu * menu = menuBar()->addMenu( tr("&File") );
menu->addAction(newAction);
}
void SdiWindow::fileNew()
{
(new SdiWindow())->show();
}
SdiWindow需要将Q_OBJECT宏作为其第一行。
class SdiWindow : public QMainWindow
{
Q_OBJECT
public: ....
您还必须在头文件上使用moc。moc工具生成信号和槽框架所需的所有c++代码。
生成的moc代码必须被编译并为链接器所知。为此,我将生成的文件包含在实现文件中,如下所示
#include SdiWindow.h
#include SdiWindow.moc
drescherjm还建议单独编译它。
编辑:在这种情况下,你是从QMainWindow继承的,为了将来的参考,你的类将需要以某种方式从QObject继承,以便能够使用信号/槽框架。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 为什么在qt c++中找不到我的插槽?
- 菜单操作连接找不到插槽
- Qt找不到插槽