gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:35 25 4
gpt4 key购买 nike

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

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

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 的构造函数中,您正在连接来自尚未初始化的对象的信号(您不会初始化 Name 直到 on_pushButton_released () 被调用)。要解决此问题,请在构造函数中创建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com