So I have implemented a thread class in my program that I'm trying to run from my mainwindow, but it is not being recognized. I will post below the thread header and file, as well as the main window header and file.
CONTEXT: The thread contains a loop that goes through the files in a directory and sends a signal to print the title to the mainwindow.
directoryprinter.h (Thread header)
#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
Thread class definitions
#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();
}
mainwindow.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
mainwindow.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();
}
The program exits with: exited with code -1073741819
However, I believe the issue is with the line I commented above. I am unsure where to begin debugging this, I have a few ideas, but they'd mostly be guesses, and I do not have much experience using threads.
Thanks in advance. et me know if there is any further information required.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(Name,SIGNAL(SendSignal(QString)),this,SLOT(Print(QString)));
}
In MainWindow
's constructor, you're connecting a signal from an object that hasn't been initialized (you don't initialize Name
until on_pushButton_released()
is called). To fix this, create Name
in the constructor:
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();
}
From the comment below, there is another problem here - namely, MainWindow::Print
is not defined as a slot
(using the slots:
macro). It should be:
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
};