I am trying to access the member of a class declared in another class as an attribute.
The project class has a workArea that is also a class defined in the project.h.
I instanciate a project in the main, and then I send it to the mainWindow through its constructor. Once in the mainWindow constructor I try to access project->lat that works fine, but project->workArea->latInf crashes.
Any help will be highly appreciated.
project.h
#ifndef PROJECT_H
#define PROJECT_H
#include <QObject>
class WorkArea{
public://attributes
int latInf = 30;
public://methods
WorkArea()//Default constructor
{
}
~WorkArea();
};
class Project : public QObject
{
Q_OBJECT
public: //attributes
int lat = 20;
WorkArea* workArea;
public: //methods
explicit Project(QObject *parent = nullptr);
signals:
public slots:
};
#endif // PROJECT_H
main.cpp
#include "ui/mainwindow.h"
#include <QApplication>
#include "project.h"
int main(int argc, char *argv[])
{
Project* pj;
QApplication app(argc, argv);
MainWindow w( 0 , pj);
w.show();
return app.exec();
}
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent, Project *project) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
try
{
qDebug() << "Project latInf" << project->lat << "\n"; // works fine
qDebug()<< "Project lowLeft: " << project->workArea->latInf << "\n" ; // crashes
}
catch(std::exception &ex)
{
qDebug() << ex.what() ;
}
catch (...)
{
}
}
That was it. Thanks #eyllanesc. I forgot to create workArea. I though just instanciting it as an attribute in Project was fine, but I had to fill that memory space in the constructor of project.
project.h
#ifndef PROJECT_H
#define PROJECT_H
#include <QObject>
class WorkArea{
public://attributes
int latInf = 30;
public://methods
WorkArea()//Default constructor
{
}
~WorkArea();
};
class Project : public QObject
{
Q_OBJECT
public: //attributes
int lat = 20;
WorkArea* workArea;
public: //methods
explicit Project(QObject *parent = nullptr){
workArea = new WorkArea;
}
signals:
public slots:
};
#endif // PROJECT_H
Related
Getting error while declaring QVector of class type in Qt.
Error :"incomplete type is not allowed"
I didn't understand what causes this error.
if i #include "storage.h" in main.cpp and declare a Qvector of class storeage it works fine but when i do the same in waveform class it reports an error.
I've tried forward declaring storage class in waveform class but still getting the same error.
Any Help??
main.cpp
#include "test_subject_02.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TEST_SUBJECT_02 w;
w.show();
return a.exec();
}
test_subject_02.h
#ifndef TEST_SUBJECT_02_H
#define TEST_SUBJECT_02_H
#include <QtGui/QMainWindow>
#include "ui_test_subject_02.h"
#include"waveform.h"
#include "storage.h"
class TEST_SUBJECT_02 : public QMainWindow
{
Q_OBJECT
public:
TEST_SUBJECT_02(QWidget *parent = 0, Qt::WFlags flags = 0);
waveform *wv;
~TEST_SUBJECT_02();
private:
Ui::TEST_SUBJECT_02Class ui;
};
#endif // TEST_SUBJECT_02_H
test_subject_02.cpp
#include "test_subject_02.h"
TEST_SUBJECT_02::TEST_SUBJECT_02(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QVector<storage> ser; //works fine here
wv->readfile("e:/testing2/result/3/abc.cur");
}
TEST_SUBJECT_02::~TEST_SUBJECT_02()
{
}
waveform.h
#ifndef WAVEFORM_H
#define WAVEFORM_H
#include "storage.h"
#include <QObject>
class waveform : public QObject
{
Q_OBJECT
public:
waveform(QObject *parent=0);
void readfile(QString);
QVector <storage> myvector ; // incomplete type is not allowed
~waveform();
private:
};
#endif // WAVEFORM_H
waveform.cpp
#include "waveform.h"
waveform::waveform(QObject *parent)
: QObject(parent)
{
}
void waveform::readfile(QString file)
{
QVector<storage> sw; //again error incomplete type is not allowed
}
waveform::~waveform()
{
}
storage.h
#ifndef STORAGE_H
#define STORAGE_H
#include <QObject>
class storage : public QObject
{
Q_OBJECT
public:
storage(QObject *parent);
storage();
storage(QString,QString);
~storage();
private:
QString x;
QString y;
};
#endif // STORAGE_H
storage.cpp
#include "storage.h"
storage::storage(QObject *parent)
: QObject(parent)
{
}
storage::storage(QString xcord,QString ycord)
{
x=xcord;
y=ycord;
}
storage::storage()
{
}
storage::~storage()
{
}
You need to explicitly include QVector in necessary file (waveform.h i believe), since QT uses a lot of forward declarations, while they appear as correct classes in IDE, but they won't compile, if proper definition is not included in file.
I cannot access staticmetaobject and I dont know why. I would need some help.
Here is the code
The two errors are:
staticMetaObject is not a member of MainWIndow*
I feel like it has something to do with the list, but I'm not sure.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "form.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Form<MainWindow*>* form;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/*qDebug() << MainWindow::staticMetaObject.className();
if (QString(MainWindow::staticMetaObject.className()) == QString("MainWindow")) {
qDebug() << "test";
}*/
form = new Form<MainWindow*>(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
form->myFunc();
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QObject>
#include <QDebug>
class FormBase : public QObject
{
Q_OBJECT
public:
FormBase() {}
};
template <typename T>
class Form : public FormBase, public QList<T>
{
public:
Form(T a)
{
QList<T>::append(a);
}
void myFunc()
{
qDebug() << T::staticMetaObject.className();
}
};
#endif // FORM_H
You are getting you types confused.
You want T to be MainWindow so that you can do
T::staticMetaObject.className()
That means you want a QList<T*>. You derive from that so you can just call
append(a);
The following code compiles fine:
class FormBase : public QObject
{
Q_OBJECT
public:
FormBase() {}
};
template <typename T>
class Form : public FormBase, public QList<T*>
{
public:
Form( T* a )
{
append( a );
}
void myFunc()
{
qDebug() << T::staticMetaObject.className();
}
};
class MainWindow:
public QMainWindow
{
MainWindow()
{
form = new Form<MainWindow>( this );
}
FormBase* form;
};
I've created an SSCE to better explain this.
In a class, I have a variable, a struct, and a declaration of the struct. Within that struct is a constructor, a variable, a struct and a declaration of that struct. And inside THAT struct is a constructor.
So is goes Mother > Daughter > GDaughter aka class > struct > struct
mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include <QSplitter>
class Mother : public QSplitter
{
Q_OBJECT
public:
Mother(int);
int age;
struct Daughter
{
Daughter();
int height1;
struct GDaughter
{
GDaughter();
};
GDaughter *kate;
};
Daughter *tina;
};
#endif // MOTHER_H
Now let's take a look at the constructors/source. Here lies my issue.
mother.cpp
#include "mother.h"
#include <QDebug>
Mother::Mother(int a)
{
age = a;
tina = new Daughter();
}
Mother::Daughter::Daughter()
{
qDebug() << age; //Not going to work... I get it. Daughter isnt a derived class
height1 = 10;
kate = new GDaughter();
}
Mother::Daughter::GDaughter::GDaughter()
{
qDebug() << height1; //Why not? The GDaughter instance is a member of Daughter!
}
Both qDebug() lines throw is not a type name, static, or enumerator.
The goal is to create "child" structs dynamically. So a parent struct might have 0 child structs, 1 child struct, or even 100 child structs. That is why I am using structs instead of derived classes. This setup looks like it would work except for the problem where "parent" variables can't be accessed.
I will include the other files anyway:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mother.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mom = new Mother(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mother.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Mother *mom;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
If I am misunderstanding how to go about making something like this then let me know.
Thanks for your time.
AFTER ANSWER
In the mother.h I added parent pointers:
struct Daughter
{
Daughter(Mother *p); //Here
int height1;
struct GDaughter
{
GDaughter(Daughter *p); //And here
};
GDaughter *kate;
};
and in mother.cpp I filled in the needed code:
Mother::Mother(int a)
{
age = a;
tina = new Daughter(this); //Here
}
Mother::Daughter::Daughter(Mother *m) //Here
{
qDebug() << m->age; //Here
height1 = 10;
kate = new GDaughter(this); //Here
}
Mother::Daughter::GDaughter::GDaughter(Daughter *d) //Here
{
qDebug() << d->height1; //Here
}
keep pointer(std::weak_ptr if you use smart pointers, for example) to parent class object in nested object.
explicit MainWindow(QWidget *parent = 0);
look at Qt code here, you pass pointer to the parent object in constructor,
if parent is nullptr that means MainWindow has no parent object, Also you can keep
pointer to MainWindow parent object and then static_cast it to exact class
#include <iostream>
class A {
class B{
public:
B(A *p):parent(p) { parent->hello(); }
private:
A *parent;
};
public:
A() { b = new B(this); }
void hello() { std::cout << "hello world" << std::endl; }
private:
B *b;
};
In my application im having three widgets named as "Widget" , "one" and "two" . i tried to create the objects of the widget in main function as pass it as an argument to another widgets . it compiles successfully but application crashed before running , refer my code below and guide me,
//main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "one.h"
#include "two.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w;
One *one;
Two *two;
w->GetObject(one,two);
one->GetObject(w,two);
two->GetObject(one,w);
w->show();
return a.exec();
}
//widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void Widget::GetObject(One *onee, Two *twoo)
{
one=onee;
two=twoo;
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
class One;
class Two;
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
One *one;
Two *two;
void GetObject(One *onee, Two *twoo);
};
#endif // WIDGET_H
//one.cpp
#include "one.h"
One::One(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void One::GetObject(Widget *widgett, Two *twoo)
{
widget = widgett;
two = twoo;
}
void One::on_pushButton_clicked()
{
widget->show();
}
//one.h
#ifndef ONE_H
#define ONE_H
#include "ui_one.h"
class Widget;
class Two;
class One : public QWidget, private Ui::One
{
Q_OBJECT
public:
explicit One(QWidget *parent = 0);
Widget *widget;
Two *two;
void GetObject(Widget *widgett, Two *twoo);
private slots:
void on_pushButton_clicked();
};
#endif // ONE_H
i'm trying to make a simple currency converter and I have some problems with my class designed to make the calculations.
I want to create an object of this class in my MainWindow to use the total variable for displaying the result in a widget on my appi form.
Member functions aren't all here yet, but the problem I have is that i get a
'Calculator' : no appropriate default constructor available error in mainwindow.cpp
While I do know what that means, I don't know how to avoid (solve) the issue because i kind of need the reference on the map to get the echange rates needed.
.cpp
#include <QObject>
#include <QString>
#include <QDebug>
#include <ui_mainwindow.h>
class Calculator
{
public:
explicit Calculator(QMap<QString,double> ¤cy_map);
void multiply(double x, double y);
void getValues(QString strFrom, QString strTo);
private:
double total, firstCurr, secondCurr;
QMap<QString,double> ↦
};
.h
#include "calculator.h"
Calculator::Calculator(QMap<QString,double> ¤cy_map):map(currency_map)
{
total = 0;
firstCurr = 0;
secondCurr= 0;
}
void Calculator::getValues(QString strFrom, QString strTo)
{
QMapIterator<QString, double> i(map);
while(i.hasNext())
{
if(i.key() == strFrom)
firstCurr=i.value();
if(i.key() == strTo)
secondCurr = i.value();
}
}
void Calculator::multiply(double x, double y)
{
total = x * y;
}
and now im trying to create an object of that class in my MainWindow class:
#include <QMainWindow>
#include <ui_mainwindow.h>
#include "calculator.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QMap<QString,double> ¤cy_map, QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
parser currency_parser;
Calculator calc; // error
};
MainWindow.h
MainWindow::MainWindow(QMap<QString, double> ¤cy_map, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->from_Combox->addItems(currency_parser.currency_list);
ui->to_Combox->addItems(currency_parser.currency_list);
}
the idea behind was that i have a Map in my main in which i save all my data needed and pass the map around to classes that use it.
i'll post my main even tho im not sure if it's needed
#include "downloader.h"
#include "mainwindow.h"
#include "parser.h"
#include "calculator.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMap<QString,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
MainWindow w(currency_map);
w.show();
return a.exec();
};
The error occurs, because you don't specify the appropriate constructor for class Calculator in the MainWindow constructors member initializer list (the compiler want's to use the default constructor then):
MainWindow::MainWindow(QMap<QString, double> ¤cy_map, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, calc(currency_map) // <<<<<<<<<<<<<<
{
// ...
}