c++ - Qt 调试 : I am implementing a Qt thread class that is not being recognized when used, 我做错了什么?

标签 c++ qt debugging qthread

所以我在我的程序中实现了一个线程类,我试图从我的主窗口运行它,但它没有被识别。我将在下面发布线程头和文件,以及主窗口头和文件。

上下文:该线程包含一个循环,该循环遍历目录中的文件并发送信号以将标题打印到主窗口。

directoryprinter.h(线程头)

#ifndef DIRECTORYPRINTER_H
#define DIRECTORYPRINTER_H

#include <QThread>
#include <QtCore/QDir>
#include <QtCore/QDirIterator>

class DirectoryPrinter : public QThread
{
    Q_OBJECT
public:
    explicit DirectoryPrinter(QObject *parent = 0);
    void DirectoryParser();
    void run();

signals:
    void SendSignal(QString);

private:
    QDirIterator * it;    
};

#endif // DIRECTORYPRINTER_H

线程类定义

#include "directoryprinter.h"

DirectoryPrinter::DirectoryPrinter(QObject *parent) :
    QThread(parent)
{
    it = new QDirIterator ("C:Users/Andrew/",QDirIterator::Subdirectories);
}

void DirectoryPrinter::DirectoryParser()
{
    while (it->hasNext())
    {
        QString String = it->next();
        SendSignal(String);
    }
}

void DirectoryPrinter::run()
{
    this->DirectoryParser();
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <iostream>
#include "directoryprinter.h"

//UNINITIALIZED POINTERS??

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void Print(QString String);
    ~MainWindow();

private slots:
    void on_pushButton_released();

private:
    Ui::MainWindow *ui;
    DirectoryPrinter *Name; //THIS LINE IS NOT RECOGNIZED
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(Name,SIGNAL(SendSignal(QString)),this,SLOT(Print(QString)));
    }

void MainWindow::Print(QString String)
{
    ui->textBrowser->setText(String);
}

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

void MainWindow::on_pushButton_released()
{
    Name = new DirectoryPrinter();
    Name->start();
}

程序退出:退出代码为-1073741819

但是,我认为问题出在我上面评论的那一行。我不确定从哪里开始调试这个,我有一些想法,但它们大多是猜测,而且我没有太多使用线程的经验。

提前致谢。让我知道是否需要任何进一步的信息。

最佳答案

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(Name,SIGNAL(SendSignal(QString)),this,SLOT(Print(QString)));
}

MainWindow 的构造函数中,您正在连接来自尚未初始化的对象的信号(在 on_pushButton_released 之前不会初始化 Name () 被调用)。要解决此问题,请在构造函数中创建 Name:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Name = new DirectoryPrinter(this);//this is the parent; takes care of cleanup
    connect(Name,SIGNAL(SendSignal(QString)),this,SLOT(Print(QString)));
}

void MainWindow::on_pushButton_released()
{
    //Name = new DirectoryPrinter(); not needed here anymore
    Name->start();
}

从下面的评论来看,这里还有另一个问题 - 即 MainWindow::Print 没有定义为 slot (使用 slots:宏)。应该是:

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    //void Print(QString String); // <-- not here...
    ~MainWindow();

public slots:
    void Print(QString String); // <-- here instead

// ...other class-definition stuff
};

关于c++ - Qt 调试 : I am implementing a Qt thread class that is not being recognized when used, 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10957319/

相关文章:

以 ~ 开头的 C++ 路径

c++ - 在按钮点击时控制 qt 中的循环

linux - 寻找一个不错的 linux 程序调试器

c++ - 根据运行时数据选择函数重载?

c++ - GTKMM:Gtk TreeView 单元格中的 GtkPopOver

c++ - boost 范围 weak_ptr

c++ - QAbstractItemModel:为什么在将大量项目插入模型时发出 dataChanged 和插入行信号如此缓慢?

c++ - 如何在 QMenu 中为 QActions 指定助记符(&符号快捷方式)?

c# - VS调试器为什么不停止在初始化变量上?

javascript - Python Bokeh CustomJS : Debugging a JavaScript callback for the Taping-Tool