0

I would like to specify a slot from within another class to execute on button click, how does one do this?

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->Open, SIGNAL(clicked()),
                     this,SLOT(Slotindiffrentclass);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Thanks.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
user4217633
  • 71
  • 3
  • 10
  • 1
    Searching, and [reading](http://qt-project.org/doc/qt-5/signalsandslots.html) [documentation](http://qt-project.org/doc/qt-5/qobject.html#connect) is a good start. – Some programmer dude Nov 05 '14 at 08:25

2 Answers2

0

The 3rd argument in connect is a pointer to a class where a slot is located. So just replace this with a pointer to a needed class.

Ezee
  • 4,214
  • 1
  • 14
  • 29
0

Use something like:

QObject::connect(ui->Open, SIGNAL(clicked()),
                 differentClass,SLOT(Slotindiffrentclass);
ABCplus
  • 3,981
  • 3
  • 27
  • 43
  • 1
    You might want to say that `differentClass` is a pointer to an *instance* of the class `Slotindifferentclass`. – Some programmer dude Nov 05 '14 at 08:22
  • `differentClass` is a pointer to an instance of the class _owning the slot_ `Slotindifferentclass` – ABCplus Nov 05 '14 at 08:25
  • Thanks, I though it had something to do with the 3rd row of connect. I tried to create a new instance of the class FileProcessor in the main window class and its telling me its and undeclared identifer. Something like FileProcessor p; QObject::connect(ui->Open, SIGNAL(clicked()), p,SLOT(Slotindiffrentclass); – user4217633 Nov 05 '14 at 08:37
  • Should be: `QObject::connect(ui->Open, SIGNAL(clicked()), &p,SLOT(Slotindiffrentclass);`. The third parameter of the connect is a pointer to the class. – ABCplus Nov 05 '14 at 08:41
  • Anyway my suggestion is to read the doc and see the examples too. – ABCplus Nov 05 '14 at 08:42