c++ - QStringListModel *作为信号的参数,给我错误 “…attempting to reference deleted function”吗?

标签 c++ multithreading qt qstringlistmodel

我编写了这个小型测试程序,以便更好地了解QT的Signals&Slots和Multithreading。
主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include <QStringListModel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    Ui::MainWindow *ui;
public slots:
    void change_stats(QString &msg);
    void insert_list(QStringListModel *model);
private:
};
#endif // MAINWINDOW_H
worker
#ifndef WORKER_H
#define WORKER_H
#include "mainwindow.h"

#include <QObject>
#include <QString>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(MainWindow &mw);

signals:
    QString statsmsg(QString);
    QStringListModel heremodel(QStringListModel*);
public slots:
    void wip_list();
    void wip_status();
private:
    MainWindow *w;
};

#endif // WORKER_H
主窗口
#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

void MainWindow::change_stats(QString &msg)
{
    ui->statusbar->showMessage(msg);
}

void MainWindow::insert_list(QStringListModel *model)
{
    ui->listView->setSelectionMode(QListView::SingleSelection);
    ui->listView->setModel(model);
}
worker
#include "worker.h"
#include <QThread>
#include <QStringListModel>
#include <vector>
#include <chrono>
#include "ui_mainwindow.h"
#include <sstream>

Worker::Worker(MainWindow &mw)
{
    w = &mw;
    QThread *thread = new QThread;
    this->moveToThread(thread);
    connect(thread, SIGNAL(started()), this, SLOT(wip_status));
    connect(w->ui->pushButton, SIGNAL(clicked()), this, SLOT(wip_list()));
    connect(this, SIGNAL(heremodel(QStringListModel*)), w, SLOT(insert_list(QStringListModel*)));
    connect(this, SIGNAL(statsmsg(QString)), w, SLOT(change_stats(QString)));
    thread->start();
}

void Worker::wip_list(){

    QStringListModel *model = new QStringListModel();
    QStringList list;
    std::string row = "row nr.";
    for(int i = 1; i <= 10; i++){
        std::stringstream rows;
        rows << row << i;
        list << QString::fromStdString(rows.str());
    }
    model->setStringList(list);
    emit heremodel(model);
}

void Worker::wip_status(){
    std::vector<QString> connecting = {"Connecting", "Connecting.", "Connecting..", "Connecting..."};
    QString msg = "Connecting";
    std::chrono::milliseconds interval = std::chrono::milliseconds(300);
    while(1){
        for(int i = 0; i <= 3; i++){
            msg = connecting[i];
            emit statsmsg(msg);
            std::this_thread::sleep_for(interval);
        }
    }
}
main.cpp
#include "worker.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    Worker worker(w);
    w.show();
    return a.exec();
}
但是,当我运行Debug时,它给了我这个错误:
错误:C2280:'QStringListModel&QStringListModel::operator =(const QStringListModel&)':尝试引用已删除的函数
我是新来的,所以请不要太苛刻,如果您发现与问题无关的错误,也请告诉我。预先感谢您回答我的问题!

最佳答案

您的heremodel信号被声明为按值返回QStringListModel ...

QStringListModel heremodel(QStringListModel*);
因此,作为QObject的错误是不可复制的。
将信号签名更改为...
void heremodel(QStringListModel*);

关于c++ - QStringListModel *作为信号的参数,给我错误 “…attempting to reference deleted function”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63053588/

相关文章:

c++ - 旋转矩阵的奇怪行为

iphone - 我可以在iPhone OS中使用POSIX线程吗?

c++ - 如何从 QTextEdit 中检索单个字符?

qt - 动态更改样式表

c# - System.Timers.Timer.Enabled 属性线程安全吗,是否可以从计时器的 Elapsed 事件访问它?

c++ - QUdpSocket - 数据报被接收两次,为什么?

c++ - 从 int 到 float 的转换是否比 static_cast<float> 更优雅?

c++ - 使用 boost::filesystem 将文件路径从 Windows 转换为 Linux,然后再转换回来

c++ - 具有可变数组大小的模板类 : initialize with array reference or brace-enclosed initializer list

java - 第二次加载时进度条失败