QThread C++ 构造函数的初始化带 QString 参数
是否可以用参数初始化一个新的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了...你必须写
最后一个参数必须是QObject!!
The problem was solved...you have to write
The last parameter has to be the QObject!!
查看您的
mySave
构造函数签名...传递的第一个参数必须是
QObject *
(或隐式可转换为QObject *
的内容)并且传递的第二个参数必须是QString
。如果您希望仅使用QString
可以构造mySave
,那么您必须提供合适的构造函数。明显的选择是简单地反转传递参数的顺序...话虽如此,这种性质的类最常用的构造函数调用可能是...
所有这些情况都可以由以下两个构造函数提供...
或者,如果您愿意,可以将第二个设置为 委托构造函数...
Look at your
mySave
constructor signature...The first parameter passed must be a
QObject *
(or something implicitly convertible to aQObject *
) and the second parameter passed must be aQString
. If you wantmySave
to be constructible using aQString
only then you must provide a suitable constructor. The obvious option would be to simply reverse the order of the passed parameters...Having said that, the most commonly used constructor calls for classes of this nature would probably be...
All of these cases can be provided by the following two constructors...
Or, if you prefer, make the second a delegating constructor...