Reputation: 347
How can i emit signal from another class? In my implementation shown below I've got "unresolved external symbol error" when I try to emit signal in SerialPort::open method. Code, head file of main window:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
std::shared_ptr<SerialPort> serialPort;
private slots:
void labelchange();
private:
Ui::MainWindow *ui;
};
and the cpp file:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
serialPort(std::shared_ptr<SerialPort>(new SerialPort))
{
ui->setupUi(this);
connect(serialPort.get(), SIGNAL(opened()),this,SLOT(labelchange()));
}
void MainWindow::labelchange()
{
ui->testinLabel->setText("signal connected to slot");
}
and the other class method when i try emit signal: head file:
class SerialPort : public QObject
{
public:
SerialPort();
void open()
signals:
void serial_opened();
}
and cpp file:
void SerialPort::open()
{
emit serial_opened();
}
Upvotes: 0
Views: 905
Reputation: 2231
SerialPort shall contain Q_OBJECT marco like this:
class SerialPort : public QObject
{
Q_OBJECT
public:
SerialPort();
void open()
signals:
void serial_opened();
}
Also please check your .pro file and check whether you added your SerialPort.h under HEADERS section.
Upvotes: 1
Reputation: 16907
This is just normal signal emitting from a class. Not "from another" class.
You are missing the Q_OBJECT
macro, QObject do not really work with out it:
class SerialPort : public QObject
{
Q_OBJECT
public:
SerialPort();
void open()
signals:
void serial_opened();
}
And you need to have the file processed by moc
. (happens automatically, if the files are listed in the .pro file)
Upvotes: 1