0

I'm trying to update gui label with an other thread information (QString).

class MainWindow : public QMainWindow
{
Q_OBJECT

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

public Q_SLOTS:
    void sl_appendInfo(QString p_text);

private:
    Ui::MainWindow *ui;
    QFuture<void> m_thread;
    QFuture<void> m_engine;
    engine* m_object;
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    m_object = new engine();

    qRegisterMetaType<QString>();
    bool success = connect(this->m_object, SIGNAL(engine::sig_appendInfo(QString)), this, SLOT(sl_appendInfo(QString)), Qt::QueuedConnection);

    if(!success)
    {
        qDebug("success failed");
    }

    m_engine = QtConcurrent::run(this->m_object, &engine::eventLoop);
}

//slot declaration in mainwindow.cpp
void MainWindow::sl_appendInfo(QString p_text)
{
    ui->label->setText(p_text.toLocal8Bit().constData());
}


class engine : public QObject
{

    Q_OBJECT

public:
    engine();
    ~engine();
    void eventLoop();

Q_SIGNALS:
    void sig_exitengine(void);
    void sig_appendInfo(QString p_text);
};

void engine::eventLoop()
{
    int state = false;

    while(true)
    {
        state = getNextEvent(m_event);

        if (state == true)
        {
                sig_appendInfo("information for gui: we handled a new event !");
        state=false;
        }
        QThread::msleep(1000);
    }
}

Now I use this link : My signal / slot connection does not work to build my own code but it didn't work, the connection failed... Can I have some help please?

Thank you

thuga
  • 12,601
  • 42
  • 52
smashup3d
  • 3
  • 2
  • "the connection failed...". You need to give a bit more information. Did you see any warning messages at the console for example? Note: if you are using `Qt5` you could move to the new [syntax](https://wiki.qt.io/New_Signal_Slot_Syntax). – G.M. Apr 23 '17 at 09:15
  • Also, I can't recall if the old signal/slot syntax allows you to use the scoping operator so try removing `IEE_engineReader::` from the `SIGNAL` invocations. – G.M. Apr 23 '17 at 09:31
  • Thank you for your help ! I tried the new method and it didn't work : connect(that, &ThatObject::mySignal, this, &ThisObject::mySlot)); and about the connection failed I have any message at the console – smashup3d Apr 23 '17 at 09:36
  • Please edit your question to include a [MCVE]. – G.M. Apr 23 '17 at 09:41
  • Ok, give me two minutes – smashup3d Apr 23 '17 at 09:42
  • Is it ok for you now? – smashup3d Apr 23 '17 at 10:03
  • G.M. I fix the problem thanks to your suggestion thank you, I search about the new synthax and it works ! I just edit my qusetion so that you can see what change I did. – smashup3d Apr 23 '17 at 10:24
  • Don't edit the question to show only the working code. Makes the question useless. If you have a solution, you can post it as an answer. – thuga Apr 24 '17 at 07:13

1 Answers1

0

Your connect syntax is wrong. You shouldn't include the class name in the SIGNAL macro. If you use the old syntax, it should be:

bool success = connect(m_object, SIGNAL(sig_appendInfo(QString)), this, SLOT(sl_appendInfo(QString)), Qt::QueuedConnection);

Or if you want to use the new syntax:

bool success = connect(m_object, &engine::sig_appendInfo, this, &MainWindow::sl_appendInfo, Qt::QueuedConnection);
thuga
  • 12,601
  • 42
  • 52