-
Hi, I'm using qt5.5 on my computer.
This is part of my code.void CandumpThread::run() { QString info; bool ret=false; bool readyread; QProcess *processwhile=new QProcess(); processwhile->setProcessChannelMode(QProcess::MergedChannels); processwhile->start("sh",QStringList()<<"-c"<<"candump "+MainWindow::cannum); ret=processwhile->waitForStarted(); if(!ret) { qDebug("finished error"); } while(!stopped) { readyread=processwhile->waitForReadyRead(-1); if(readyread==true) { emit done(QString(processwhile->readAllStandardOutput())); } } delete processwhile; }
And this will cause the error for me said
error: invalid use of non-static data member 'MainWindow::cannum' processwhile->start("sh",QStringList()<<"-c"<<"candump "+MainWindow::cannum);
I declared cannum in the MainWindow.h as a QString.
how can i using the element that is not declared in the CandumpThread?
Please help! -
This question is nothing to do with qt. please check on how to use static class variables in c++. Declare cannum variable as static in main window class and use it. Why do u want to do this ?
-
@dheerendra
I want to using the specific String that i will give it in the MainWindow and used it in my CandumThread class.Is this will cause any problem?
-
@dheerendra
Do you have any suggestion to do it better?
cause i don't want cannum to be the static String cause i will need to change the value in the MainWindow if i need. -
When are u assigning this string value ? Just check how static variable work in c++. It should help.
-
@dheerendra
I will assigned it in the MainWindow.
And i will want to used it at CandumpThread.i change ti into static const and still not work.
-
@victor-wang If MainWindow is using CandumpThread then just pass that string from MainWindow to CandumpThread constructor.
Don't make cannum static! Your CandumpThread class should not know anything about MainWindow (it would be bad design if it would know). -
@victor-wang I moved this thread to "C++ gurus" forum as your question is not related to Qt but to C++.
-
@victor-wang Bad design...
You should instead pass the string as parameter:DIDOThread::DIDOThread(const QString &cannum, QObject *parent) : QThread(parent) { }
And if you still want to do it like you shown then you should at least use C++ style cast not C.
-
@victor-wang Yes, you just add "const QString &cannum" to your constructor in DIDOThread as I shown.