使用信号和槽实现 connect() 时遇到问题
Trouble implementing connect() with signals and slots
在做一些线程教程时,我被带走了,决定制作一个 gui,它会向我展示多个线程写入一个变量并使用互斥锁的效果。
该应用程序使用 mainwindow.ui 菜单创建线程对话框的新实例 class 每次运行它自己的线程,在标签上显示它的计数循环。在我开始执行循环并拥有互斥锁 'toggle' 之前,我试图连接主窗口和线程对话框之间的计数更新,以便主窗口可以显示全局计数更新。
我无法正确连接 connect(),我正试图将一个指针传递给它,指向我刚刚在它之前创建的新线程对话框,因为它将发出计数信号和信号本身。然后对于插槽,我使用 this 指针发送 MainWindow 的地址,因为这是插槽所在的位置,以及插槽名称本身。
就目前而言,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() 方面做错了什么。
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);
}
}
它应该是这样的:
connect(newThread, &ThreadDialog::gCountUpdate
this, &MainWindow::setGCount);
您需要获取指向该方法的指针,而不是调用该方法。
在做一些线程教程时,我被带走了,决定制作一个 gui,它会向我展示多个线程写入一个变量并使用互斥锁的效果。
该应用程序使用 mainwindow.ui 菜单创建线程对话框的新实例 class 每次运行它自己的线程,在标签上显示它的计数循环。在我开始执行循环并拥有互斥锁 'toggle' 之前,我试图连接主窗口和线程对话框之间的计数更新,以便主窗口可以显示全局计数更新。
我无法正确连接 connect(),我正试图将一个指针传递给它,指向我刚刚在它之前创建的新线程对话框,因为它将发出计数信号和信号本身。然后对于插槽,我使用 this 指针发送 MainWindow 的地址,因为这是插槽所在的位置,以及插槽名称本身。
就目前而言,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() 方面做错了什么。
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);
}
}
它应该是这样的:
connect(newThread, &ThreadDialog::gCountUpdate
this, &MainWindow::setGCount);
您需要获取指向该方法的指针,而不是调用该方法。