QThread C++ 构造函数的初始化带 QString 参数

发布于 2025-01-11 10:22:09 字数 2184 浏览 0 评论 0原文

是否可以用参数初始化一个新的QThread?我在主窗口中创建了两个线程。现在我想用 QString 初始化线程“mySave”。但这不起作用。

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

我将 mSave = new mySave() 行更改为 mySave = new mySave("HelloWorld") 并将 mySave-Class 的构造函数更改为

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 {};
    ....

但我变成了一个错误!

mainwindow.cpp:57:17:错误:没有匹配的构造函数来初始化“mySave” mysave.h:9:7:注意:候选构造函数(隐式复制构造函数)不可行:第一个参数没有从“const char [6]”到“const mySave”的已知转换 mysave.h:13:14:注意:候选构造函数不可行:第一个参数没有从“const char [6]”到“QObject *”的已知转换

有什么问题......?

没有 QString 参数,一切正常:

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 构造函数

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

我如何在新 QThread 的构造函数中设置 QString 参数....?

谢谢&再见

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2025-01-18 10:22:09

问题解决了...你必须写

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

private:
    QFile file;

最后一个参数必须是QObject!!

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!!

梦醒灬来后我 2025-01-18 10:22:09

查看您的 mySave 构造函数签名...

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

传递的第一个参数必须是 QObject * (或隐式可转换为 QObject * 的内容)并且传递的第二个参数必须是QString。如果您希望仅使用 QString 可以构造 mySave,那么您必须提供合适的构造函数。明显的选择是简单地反转传递参数的顺序...

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

话虽如此,这种性质的类最常用的构造函数调用可能是...

/* 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);

所有这些情况都可以由以下两个构造函数提供...

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

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

或者,如果您愿意,可以将第二个设置为 委托构造函数...

mySave::mySave (QObject *parent)
  : mySave(QString(), parent)
  {}

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)
  {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文