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 1 May 2016, 18:40 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 May 2016, 21:19 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
      • Y Offline
        Y Offline
        yuvaram
        wrote on 2 May 2016, 14:49 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 May 2016, 20:57 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

          Y 1 Reply Last reply 3 May 2016, 02:07
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 May 2016, 21:08 last edited by mrjj 5 Feb 2016, 21:09
            #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
            • S SGaist
              2 May 2016, 20:57

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

              Y Offline
              Y Offline
              yuvaram
              wrote on 3 May 2016, 02:07 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
              : )

              J 1 Reply Last reply 3 May 2016, 05:30
              0
              • Y yuvaram
                3 May 2016, 02:07

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

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 3 May 2016, 05:30 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

                Y 1 Reply Last reply 3 May 2016, 12:20
                1
                • J jsulm
                  3 May 2016, 05:30

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

                  Y Offline
                  Y Offline
                  yuvaram
                  wrote on 3 May 2016, 12:20 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

                  3/8

                  2 May 2016, 14:49

                  5 unread
                  • Login

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