Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Was not declared in this scope

Was not declared in this scope

Scheduled Pinned Locked Moved Unsolved General and Desktop
scopedeclaration
8 Posts 5 Posters 13.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,
    I have a question…

       QFile inputFile(":/Table.htm");
       inputFile.open(QIODevice::ReadOnly);
       QTextStream in(&inputFile);
       QString line = in.readAll();
       inputFile.close();
           w->append(line);
    

    w was not declared in this scope. I know that.

    The Code is written in Start.cpp

    I declared w in Mainwindow.h

    How can i use the w from Mainwindow.h in Start.cpp?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Sounds like you are trying to access internal details of a class from another one which is a bad idea.

      Add a function to MainWindow to modify w but don't try to access it from another class like that.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • yuvaramY Offline
        yuvaramY Offline
        yuvaram
        wrote on last edited by
        #3

        Hi @HenrikSt
        This sample code may help you.
        widget.cpp

        #include "widget.h"
        widget::widget(QObject *parent) : QObject(parent)
        {
        }
        void widget:: setValueX(int val){
        xValue = val;
        qDebug()<<Q_FUNC_INFO<<"Xval ::"<<xValue<<endl;
        }
        int widget:: getValueX(){
        qDebug()<<Q_FUNC_INFO<<"Xval ::"<<xValue<<endl;
        return xValue;
        }

        start.h

        #include "widget.h"
        class start : public QWidget
        {
        Q_OBJECT
        public:
        explicit start(QWidget parent = 0);
        void SetWidgetpointer(QObject
        );

        signals:

        public slots:
        private:
        widget *wgtobj;
        };

        start.cpp

        #include "start.h"
        start::start(QWidget parent) : QWidget(parent)
        {}
        void start:: SetWidgetpointer(QObject
        obj){
        wgtobj = qobject_cast<widget*>(obj);
        qDebug()<<Q_FUNC_INFO<<"> valueX ::"<<wgtobj->getValueX()<<endl;
        }

        mainwindow.cpp

        #include "mainwindow.h"
        #include "start.h"

        MainWindow::MainWindow(QWidget *parent)
        : QWidget(parent)
        {
        widget w;
        w.setValueX(100);
        start strtObj;
        strtObj.SetWidgetpointer(&w);

        }

        Execute the above code, you will get clear idea of implementation.

        Yuvaram Aligeti
        Embedded Qt Developer
        : )

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @yuvaram That's really not a clean implementation. You're creating an unneeded tight coupling that will only result in a maintenance nightmare.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          yuvaramY 1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            w is your textedit ?
            Would be easier just to make that function in mainwin.
            Do you mean something like this?

            start.cpp
            void InsertResTable(QString &line) {
               QFile inputFile(":/Table.htm");
                inputFile.open(QIODevice::ReadOnly);
               QTextStream in(&inputFile);
               line = in.readAll();
               inputFile.close();       
            }
            
            in start.h
            void InsertResTable(QString &line);
            
            in maindow.
            #include "start.h"
            ... when u click?
            QString line;
            InsertResTable(line);
            w->append(line);
            

            disclaimer: not tested.

            1 Reply Last reply
            1
            • SGaistS SGaist

              @yuvaram That's really not a clean implementation. You're creating an unneeded tight coupling that will only result in a maintenance nightmare.

              yuvaramY Offline
              yuvaramY Offline
              yuvaram
              wrote on last edited by
              #6

              Hi @SGaist
              Can you please share right way of implementation in this scenario.
              Any other way of implementation.
              Thank you.

              Yuvaram Aligeti
              Embedded Qt Developer
              : )

              jsulmJ 1 Reply Last reply
              0
              • yuvaramY yuvaram

                Hi @SGaist
                Can you please share right way of implementation in this scenario.
                Any other way of implementation.
                Thank you.

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

                @yuvaram Just use signals/slots to exchange data between classes and to trigger actions. Do NOT access internal details (like member variables) of a UI class from another one.

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

                yuvaramY 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @yuvaram Just use signals/slots to exchange data between classes and to trigger actions. Do NOT access internal details (like member variables) of a UI class from another one.

                  yuvaramY Offline
                  yuvaramY Offline
                  yuvaram
                  wrote on last edited by
                  #8

                  @jsulm Yes, i agree with you. But as per above requriment i gave that sample.

                  Yuvaram Aligeti
                  Embedded Qt Developer
                  : )

                  1 Reply Last reply
                  0

                  • Login

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