Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to use the elements that is not belong in your class
Forum Updated to NodeBB v4.3 + New Features

how to use the elements that is not belong in your class

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by victor wang
    #1

    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!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      V 2 Replies Last reply
      1
      • dheerendraD dheerendra

        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 ?

        V Offline
        V Offline
        victor wang
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • dheerendraD dheerendra

          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 ?

          V Offline
          V Offline
          victor wang
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by dheerendra
            #5

            When are u assigning this string value ? Just check how static variable work in c++. It should help.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            V 1 Reply Last reply
            1
            • dheerendraD dheerendra

              When are u assigning this string value ? Just check how static variable work in c++. It should help.

              V Offline
              V Offline
              victor wang
              wrote on last edited by
              #6

              @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.

              jsulmJ 2 Replies Last reply
              0
              • V victor wang

                @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.

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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).

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • V victor wang

                  @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.

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @victor-wang I moved this thread to "C++ gurus" forum as your question is not related to Qt but to C++.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @victor-wang I moved this thread to "C++ gurus" forum as your question is not related to Qt but to C++.

                    V Offline
                    V Offline
                    victor wang
                    wrote on last edited by
                    #9

                    @jsulm
                    Thx a lot!
                    I have solve the problem.
                    I add this program.

                    DIDOThread::DIDOThread(QObject *parent) : QThread(parent)
                    {
                        p = (MainWindow *)parent;
                    }
                    
                    

                    It work for me in my DIDOThread class!

                    jsulmJ 1 Reply Last reply
                    0
                    • V victor wang

                      @jsulm
                      Thx a lot!
                      I have solve the problem.
                      I add this program.

                      DIDOThread::DIDOThread(QObject *parent) : QThread(parent)
                      {
                          p = (MainWindow *)parent;
                      }
                      
                      

                      It work for me in my DIDOThread class!

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      V 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @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.

                        V Offline
                        V Offline
                        victor wang
                        wrote on last edited by
                        #11

                        @jsulm
                        You mean i can add this program in my DIDOThread.h file?

                        jsulmJ 1 Reply Last reply
                        0
                        • V victor wang

                          @jsulm
                          You mean i can add this program in my DIDOThread.h file?

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @victor-wang Yes, you just add "const QString &cannum" to your constructor in DIDOThread as I shown.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved