Home > Net >  Initialization of the constructor of a QThread C with QString parameter
Initialization of the constructor of a QThread C with QString parameter

Time:03-04

is it possible to initialize a new QThread with a parameter?? I created two threads in MainWindow. Now i want initialize the Thread "mySave" with a QString. But it doesn't work.

void MainWindow::on_startButton_clicked()
{
    thread = new QThread();
    mThread = new myThread();
    threadSave = new QThread();
    mSave = new mySave("HelloWorld");
    ....
}

I change the line mSave = new mySave() to mySave = new mySave("HelloWorld") and the constructor of mySave-Class to

mySave::mySave(QObject *parent, QString stringFromMainWindow)
    : QObject{parent}, m_string{stringFromMainWindow}
{
    this->stringMySave = stringFromMainWindow;
}

mySave.h

class mySave : public QObject
{
    Q_OBJECT
public:
    explicit mySave(QObject *parent = nullptr, QString stringFileName = NULL);

private:
    QFile file;
    QString m_string {};
    ....

But i become a fault!

mainwindow.cpp:57:17: error: no matching constructor for initialization of 'mySave' mysave.h:9:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [6]' to 'const mySave' for 1st argument mysave.h:13:14: note: candidate constructor not viable: no known conversion from 'const char [6]' to 'QObject *' for 1st argument

What's wrong...??

Without QString parameter everthing works fine:

void MainWindow::on_startButton_clicked()
{
    thread = new QThread();
    mThread = new myThread();
    threadSave = new QThread();
    mSave = new mySave();

    connect(thread, SIGNAL(finished()), mThread, SLOT(deleteLater()));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    connect(mThread, SIGNAL(emitData(const QByteArray &)), this, SLOT(setEditText(const QByteArray &)));

    mThread->moveToThread(thread);
    thread->start();

    mSave->moveToThread(threadSave);
    threadSave->start();

    QMetaObject::invokeMethod(mThread, "interfaceSerial");

    QMetaObject::invokeMethod(mSave, "startFile", Q_ARG(QString, fileName));

    qDebug() << "Main open " << QThread::currentThread();

    dataTimer.start(0); // Interval 0 means to refresh as fast as possible
}

mySave-Thread constructor

mySave::mySave(QObject *parent)
    : QObject{parent}
{
    qDebug() << "Thread open " << QThread::currentThread();
}

How can i set a QString parameter in the constructor of a new QThread....??

Thanks & Bye bye

CodePudding user response:

The problem was solved...you have to write

class mySave : public QObject
{
    Q_OBJECT
public:
    explicit mySave(QString stringFileName = NULL, QObject *parent = nullptr);

private:
    QFile file;

The last parameter has to be the QObject!!

CodePudding user response:

Look at your mySave constructor signature...

explicit mySave(QObject *parent = nullptr, QString stringFileName = NULL);

The first parameter passed must be a QObject * (or something implicitly convertible to a QObject *) and the second parameter passed must be a QString. If you want mySave to be constructible using a QString only then you must provide a suitable constructor. The obvious option would be to simply reverse the order of the passed parameters...

explicit mySave(QString stringFileName = QString(), QObject *parent = nullptr);

Having said that, the most commonly used constructor calls for classes of this nature would probably be...

/* Default constructor: no text or parent. */
mySave ms1;

/* Text only. */
mySave ms2("Some text...");

/* Parent object only. */
QObject *parent = ...;
mySave ms3(parent);

/* Pass both text and parent object. */
mySave ms4("Some text...", parent);

All of these cases can be provided by the following two constructors...

mySave::mySave (const QString &stringFileName = QString(), QObject *parent = nullptr)
  : QObject(parent)
  , m_string(stringFileName)
  {
    ...
  }

mySave::mySave (QObject *parent)
  : QObject(parent)
  {
    ...
  }

Or, if you prefer, make the second a delegating constructor...

mySave::mySave (QObject *parent)
  : mySave(QString(), parent)
  {}
  •  Tags:  
  • c qt
  • Related