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