2

そのため、メインウィンドウから実行しようとしているプログラムにスレッド クラスを実装しましたが、認識されません。スレッド ヘッダーとファイル、およびメイン ウィンドウのヘッダーとファイルの下に投稿します。

CONTEXT: スレッドには、ディレクトリ内のファイルを通過し、タイトルをメインウィンドウに出力するためのシグナルを送信するループが含まれています。

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

プログラムは次のように終了します: exited with code -1073741819

ただし、問題は上でコメントした行にあると思います。これのデバッグをどこから始めればよいかわかりません。いくつかのアイデアがありますが、それらはほとんど推測であり、スレッドを使用した経験はあまりありません。

前もって感謝します。さらに情報が必要な場合はお知らせください。

4

1 に答える 1

3
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として定義されていません。そのはず:slotslots:

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
};
于 2012-06-09T00:10:03.723 に答える