Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用信号和插槽实现connect()_C++_Multithreading_Qt_Qthread - Fatal编程技术网

C++ 无法使用信号和插槽实现connect()

C++ 无法使用信号和插槽实现connect(),c++,multithreading,qt,qthread,C++,Multithreading,Qt,Qthread,在做一些线程教程时,我很激动,决定制作一个gui,向我展示多个线程写入一个变量并使用互斥锁的效果 应用程序使用mainwindow.ui菜单每次创建threaddialog类的新实例,然后运行自己的线程,在标签上显示其计数循环。在我开始循环并使用互斥体“切换”之前,我正在尝试在mainwindow和threaddialog之间连接计数更新,以便mainwindow可以显示全局计数更新 我无法正确获取connect(),我正在尝试向它传递一个指向我刚才在它前面创建的新threaddialog的指针

在做一些线程教程时,我很激动,决定制作一个gui,向我展示多个线程写入一个变量并使用互斥锁的效果

应用程序使用mainwindow.ui菜单每次创建threaddialog类的新实例,然后运行自己的线程,在标签上显示其计数循环。在我开始循环并使用互斥体“切换”之前,我正在尝试在mainwindow和threaddialog之间连接计数更新,以便mainwindow可以显示全局计数更新

我无法正确获取connect(),我正在尝试向它传递一个指向我刚才在它前面创建的新threaddialog的指针,因为这将发送计数信号,以及信号本身。然后对于插槽,我使用this指针发送主窗口的地址,因为这是插槽所在的位置,以及插槽名称本身

现在,connect()行给出了信号和插槽参数的这个错误

C:\Users\btank\Documents\Qt Projects\QThreadClasses\mainwindow.cpp:46: error: C3867: 'ThreadDialog::gCountUpdate': non-standard syntax; use '&' to create a pointer to member
我已经阅读了Qt信号和插槽官方文档的整个页面,试图理解我做错了什么,但运气不佳,需要帮助。我不认为我在将这些指针发送到connect()方面做错了什么

主窗口

#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
\ifndef主窗口
#定义主窗口
#包括
#包括
#包括
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
QGridLayout*布局=新的QGridLayout();
void NewThread();
~main窗口();
专用插槽:
操作新线程时无效已触发();
在_actionDelete_Thread_triggered()上无效;
公众时段:
无效设置计数(int gCount);
私人:
QLinkedList列表;
Ui::MainWindow*Ui;
};
#endif//main窗口
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));
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
//使用前清除内存列表
list.clear();
qDebug()设置布局(布局);
//将小部件添加到主窗口中心小部件
setCentralWidget(窗口);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void main window::on_actionNew_Thread_triggered()
{
NewThread();
}
void main窗口::NewThread()
{
ThreadDialog*newThread=newthreaddialog(list.count());
qDebug()gCountUpdate,this,this->setGCount);
newThread->run();
列表addWidget(newThread);
}
void main window::on_actionDelete_Thread_triggered()
{
layout->removeWidget(list.last());
删除list.last();
list.removeLast();
qDebug()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
\ifndef THREADDIALOG\u H
#定义线程对话框
#包括
#包括
名称空间用户界面{
类线程对话框;
}
类线程对话框:公共QDialog
{
Q_对象
公众:
显式线程对话框(int计数,QWidget*parent=0);
无效运行();
~ThreadDialog();
信号:
作废gCountUpdate(int uCount);
私人:
Ui::ThreadDialog*Ui;
};
#endif//THREADDIALOG\u 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);
    }
}
#包括“threaddialog.h”
#包括“ui_threaddialog.h”
#包括“mainwindow.h”
ThreadDialog::ThreadDialog(整数计数,QWidget*父项):
QDialog(父级),
ui(新ui::ThreadDialog)
{
用户界面->设置用户界面(此);
//设置界面
ui->lblTCount->setText(QString(“线程%1”).arg(计数));
}
ThreadDialog::~ThreadDialog()
{
删除用户界面;
}
void ThreadDialog::run()
{
对于(int i=0;i<100;i++)
{
qDebug()lblTNum->setText(QString(“线程%1”).arg(i));
发出此->gCountUpdate(i);
QThread::sleep(100);
}
}

它应该是这样的:

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

您需要获取指向该方法的指针,而不是调用该方法。

它应该是这样的:

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

您需要获取指向该方法的指针,而不是调用该方法。

&newThread->gCountUpdate&newThread->gCountUpdate这个术语对我来说是新的,我对它进行了更多的研究。一开始我很困惑,因为line&ThreadDialog::gCountUpdate似乎只是自己指定了对type/member的引用,而没有指定任何对象,这是我使用它需要知道的。我想这就是参数1和3在connect()中的作用。这个术语对我来说是新的,对它进行了更多的研究。一开始我很困惑,因为line&ThreadDialog::gCountUpdate似乎只是自己指定了对type/member的引用,而没有指定任何对象,这是我使用它需要知道的。我想这就是参数1和3在connect()中的作用。