B-Tank88
B-Tank88

Reputation: 7

Trouble implementing connect() with signals and slots

While doing some threading tutorials I got carried away and decided to make a gui which will show me the effect of multiple threads writing to one variable and using mutex.

The app uses mainwindow.ui menu to create a new instance of the threaddialog class every time which then runs it's own thread, displaying it's count loop on labels. Before I get onto doing the loop and having a mutex 'toggle', I am trying to connect the count update between the mainwindow and threaddialog so mainwindow can show the global count updating.

I can't get the connect() right, I am trying to pass it a pointer to the new threaddialog I just made before it, as that will be signalling the count, and the signal itself. Then for slot I use the this pointer to send the address of MainWindow, as that is where the slot is located, and the slot name itself.

As it stands, the connect() line gives me this error for both signal and slot parameters.

C:\Users\btank\Documents\Qt Projects\QThreadClasses\mainwindow.cpp:46: error: C3867: 'ThreadDialog::gCountUpdate': non-standard syntax; use '&' to create a pointer to member

I have read the whole page on Qt signals and slots official docs to try and understand what I'm doing wrong but no luck and need help. I don't believe I'm doing anything wrong regarding sending those pointers to connect().

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGridLayout>
#include <threaddialog.h>
#include <QLinkedList>
#include <QDebug>

namespace Ui {

class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    QGridLayout *layout = new QGridLayout();
    void NewThread();
    ~MainWindow();

private slots:
    void on_actionNew_Thread_triggered();
    void on_actionDelete_Thread_triggered();

public slots:
    void setGCount(int gCount);

private:
    QLinkedList<ThreadDialog *> list;
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <threaddialog.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //Clear memory list before using
    list.clear();

    qDebug() << list.count();

    // Set layout in widget
    QWidget *window = new QWidget();
    window->setLayout(layout);

    // Add widget to main window central widget
    setCentralWidget(window);
}

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

void MainWindow::on_actionNew_Thread_triggered()
{
    NewThread();
}

void MainWindow::NewThread()
{
    ThreadDialog *newThread = new ThreadDialog(list.count());
    qDebug() << list.count();
    connect(newThread, newThread->gCountUpdate, this, this->setGCount);
    newThread->run();

    list << newThread;
    layout->addWidget(newThread);
}


void MainWindow::on_actionDelete_Thread_triggered()
{
    layout->removeWidget(list.last());
    delete list.last();
    list.removeLast();
    qDebug() << list.count();
}

void MainWindow::setGCount(int gcount)
{
    ui->lblGCount->setText(QString::number(gcount));
}

threaddialog.h

#ifndef THREADDIALOG_H
#define THREADDIALOG_H

#include <QDialog>
#include <QThread>

namespace Ui {
class ThreadDialog;
}

class ThreadDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ThreadDialog(int count, QWidget *parent = 0);
    void run();
    ~ThreadDialog();

signals:
    void gCountUpdate(int uCount);

private:
    Ui::ThreadDialog *ui;
};

#endif // THREADDIALOG_H

threaddialog.cpp

#include "threaddialog.h"
#include "ui_threaddialog.h"
#include "mainwindow.h"

ThreadDialog::ThreadDialog(int count, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ThreadDialog)
{
    ui->setupUi(this);

    // Setup UI
    ui->lblTCount->setText(QString("Thread %1").arg(count));
}

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

void ThreadDialog::run()
{
    for(int i = 0; i < 100; i++)
    {
        qDebug() << (QString("Thread loop %1").arg(i));
        ui->lblTNum->setText(QString("Thread %1").arg(i));
        emit this->gCountUpdate(i);
        QThread::sleep(100);
    }
}

Upvotes: 1

Views: 142

Answers (1)

Simon Warta
Simon Warta

Reputation: 11408

This is how it should look like:

connect(newThread, &ThreadDialog::gCountUpdate
        this, &MainWindow::setGCount);

You need to get a pointer to the method, not call the method.

Upvotes: 0

Related Questions