QStringListModel * 作为信号的参数给我错误“...试图引用已删除的函数”?

QStringListModel * as a parameter of a signal giving me the error "...attempting to reference deleted function"?

我写了这个小测试程序,所以我更好地理解了 QT 的信号和槽,以及多线程。

mainwindow.h

#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.h

#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

mainwindow.cpp

#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.h

#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();
}

但是当我 运行 调试时它给我这个错误: 错误:C2280:'QStringListModel &QStringListModel::operator =(const QStringListModel &)':试图引用已删除的函数

我是新来的,所以请不要太苛刻,如果您发现与问题无关的错误,请告诉我。预先感谢您回答我的问题!

您的 heremodel 信号被声明为 return 一个 QStringListModel 按值...

QStringListModel heremodel(QStringListModel*);

因此 QObject 的错误是不可复制的。

将信号签名更改为...

void heremodel(QStringListModel*);