samedi 25 avril 2015

Cannot connect slots from another class


I currently try to write an little application for experiments and more. I write everything in its own file (Menu -> menu.cpp etc.). Now I want to create a menu action, this works but interacting with the action doesnt. Thats what I've done so far:

menu.cpp

#include "menu.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>


void Menu::setupMenu(QMainWindow *window) {
    QMenuBar *mb = new QMenuBar();
    QMenu *fileMenu = new QMenu("File");
    QAction *newAct = new QAction("New...", window);
    window->connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
    fileMenu->addAction(newAct);
    mb->addMenu(fileMenu);
    window->setMenuBar(mb);
}

void Menu::newFile() {
    printf("hello world!");
}

menu.h

#ifndef MENU_H
#define MENU_H

#include <QObject>
#include <QMainWindow>
#include <QWidget>
#include <QMenuBar>

class Menu : public QObject
{
public:
    void setupMenu(QMainWindow *window);
private slots:
    void newFile();
};

#endif // MENU_H

But its not printing out 'hello world', the only message I get is:

QObject::connect: No such slot QObject::newFile() in ../from Scratch written UI app C++/src/ui/menu.cpp:11

What can I do to fix this?

~ Jan


Aucun commentaire:

Enregistrer un commentaire