Signal and SLOT C++ with QT

Asked

Viewed 224 times

0

I am developing a software in c++ that captures images from Webcam, before I used Opencv but received an error

Undefined Symbols for Architecture x86_64
Until I opened a topic here and nobody knew how to answer me the error. Quit using Opencv and went to use QT Qcamera. So far so good, the problem is that now I wanted to update with a Thread a Qlabel but when I do the Emit command I get the same error as when I used Opencv. See the code I’m using:

thread. h

#ifndef THREAD_H
#define THREAD_H

#include <QtCore>


class Thread : public QThread {
            private:
                void run();
            signals:
                void MySignal( void );
        };

#endif // THREAD_H



thread cpp.

#include "thread.h"
#include <iostream>
#include <thread>
#include <QtCore>

using namespace std;


void Thread::run(){
    qDebug()<<"From worker thread: "<<currentThreadId();
    emit MySignal();
}



Button that fires the Thread:

void MainWindow::on_pushButton_clicked() {
    qDebug()<<"From main thread: "<<QThread::currentThreadId();
    Thread t;
    QObject::connect( &t, SIGNAL( MySignal() ), this, SLOT( MySlot() ) );
    t.start();
}



Error output:

12:11:55: Running steps for project CameraControl...
12:11:55: Configuration unchanged, skipping qmake step.
12:11:55: Starting: "/usr/bin/make" 
Undefined symbols for architecture x86_64:
"Thread::MySignal()", referenced from:
  Thread::run() in thread.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [CameraControl.app/Contents/MacOS/CameraControl] Error 1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -mmacosx-version-min=10.10.3 -Wl,-rpath,/Applications/QTFiles/5.4/clang_64/lib -o CameraControl.app/Contents/MacOS/CameraControl main.o mainwindow.o thread.o moc_mainwindow.o   -F/Applications/QTFiles/5.4/clang_64/lib -framework QtMultimediaWidgets -framework QtMultimedia -framework QtNetwork -framework QtCore -framework DiskArbitration -framework IOKit -framework QtGui -framework QtWidgets -framework OpenGL -framework AGL 
12:11:55: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project CameraControl (kit: Desktop Qt 5.4.2 clang 64bit)
When executing step "Make"
12:11:55: Elapsed time: 00:00.



File . pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-06-27T21:51:05
#
#-------------------------------------------------

QT += core gui

QT += multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CameraControl
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    thread.cpp

HEADERS  += mainwindow.h \
    thread.h

FORMS    += mainwindow.ui

target.path = $$[QT_INSTALL_EXAMPLES]/multimediawidgets/camera
INSTALLS += target



mainwindow.cpp

void MySlot( void ){
    qDebug() << "slot called";
}



mainwindow. h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qcamera.h>
#include <QCameraImageCapture>
#include <QMediaRecorder>
#include <qcamerainfo.h>

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void setCamera(const QCameraInfo &cameraInfo);
    void updateCaptureMode();
    void processSavedImage(int requestId, QString str);
    void task();

    void on_actionSobre_triggered();

    void on_pushButton_clicked();

signals:
    void signal();

public:
    Ui::MainWindow *ui;
private:


    QCamera *camera;
    QCameraImageCapture *imageCapture;
    QMediaRecorder* mediaRecorder;

    QImageEncoderSettings imageSettings;
    QAudioEncoderSettings audioSettings;
    QVideoEncoderSettings videoSettings;
    QString videoContainerFormat;
    bool isCapturingImage;
    bool applicationExiting;
};

#endif // MAINWINDOW_H
  • I already edited the question by adding . pro

  • Where is the definition of Mysignal?

  • I forgot to put. Already edited.

  • No one knew how to answer is a little lacking in patience, especially over the weekend, where most of the users did not participate so much in the community. The problem is the same as the other question, has nothing to do with Slots or with your code, I do not see the need to have opened a new question, it is now clear that the problem is with the compiler of your Mac. See if my answer helps http://answall.com/a/71609/3635

  • 1

    I only commented on the other problem I got with Qt in the other topic because the error was pretty much the same, but at no point did I say this problem was related to the other. Because the problem I had was with Opencv which was not compiling with Qt, and this is with respect to Thread. I didn’t abandon the other topic, I’m just wondering about my slot question. NOTE: If it was a problem with my compiler, Opencv would not compile via terminal without Qt.

  • Friend the problem is with the compiler and not with the code, the answer I added was for both Openvc and the Mac compiler that seems to be the problem here too, see if my answer helps http://answall.com/a/71609/3635

Show 1 more comment

1 answer

1


You must include the macro Q_OBJECT in the Thread class.

Of documentation:

The Meta-object Compiler, moc, is the program that Handles Qt’s C++ Extensions. The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ file containing the meta-object code for those classes. Among other Things, meta-object code is required for the Signals and slots Mechanism, the run-time type information, and the Dynamic Property system.

Your Thread class would look like this:

#ifndef THREAD_H
#define THREAD_H

#include <QtCore>

class Thread : public QThread 
{
   Q_OBJECT

signals:
   void MySignal( void );

private:
   void run();
};

#endif
  • But Myslot is not to be declared in mainwindow?

  • Give me a few minutes to edit the answer.

  • 1

    That’s right, thank you very much, you don’t know how to help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.