1

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

主窗口.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

工人.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

主窗口.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);
}

工人.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);
        }
    }
}

主文件

#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 &)':试图引用已删除的函数

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

4

1 回答 1

2

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

QStringListModel heremodel(QStringListModel*);

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

将信号签名更改为...

void heremodel(QStringListModel*);
于 2020-07-23T11:59:17.107 回答