Was not declared in this scope



  • 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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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


  • Qt Champions 2016

    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.



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


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.