no matching function call for QAbstractItemModel derived class - c++

I am getting this error :
/MyApp/company.cpp:3: error: no matching function for call to ‘DepList::DepList(Company*, Company*)’
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
^
/MyApp/company.h:14: In file included from ../../../../MyApp/company.h:14:0,
/MyApp/company.cpp:1: from ../../../../MyApp/company.cpp:1:
/MyApp/deplist.h:34: candidate: DepList::DepList(QAbstractItemModel*, Company*)
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
/MyApp/deplist.h:34: note: no known conversion for argument 1 from ‘Company*’ to ‘QAbstractItemModel*’
^
when compiling the constructor code for Company class:
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
{
// .... some intialization stuff here, unrelated to the error
}
and it only happens with classes where the parent is QAbstractItemModel. With other classes , parents of QObject I am using initialization lists without errors. The error appeared when I added initialization lists.
this is how search_results is declared inside Company class:
class Company : public QObject {
...
DepList search_results;
...
}
and it's constructor is:
DepList::DepList(QAbstractItemModel *parent,Company* p_company) : QAbstractItemModel(parent)
{
company=p_company;
}
What is the problem here , and why it only happens with QAbstractItemModel?? QAbstractItemModel is also a child of QObject and I have no problems with initialization lists in QObject classes. I had a thought of casting , but would it be safe ?
Company is a QObject-derived:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
But DepList is a QAbstractItemModel-derived:
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}

Related

How to use the creating class` methods from the created class?

I have a question:
I have a class userinterface that has a class MoveSeries. From MoveSeries I want to have access to the methods of my class userinterface. In this example I want to have access to the method get_MoveCurve_Delta() of userinterface. How do I get access to the creating class (userinterface) from the created class (MoveSeries ? I tried the Signal-Slot-Approach but since I have to use several methods of userinterface several times this makes lots of signal-slots...
here is my code:
Userinterface.h:
class UserInterface : public QMainWindow
{
Q_OBJECT
public:
UserInterface(QWidget *parent = 0, Qt::WFlags flags = 0);
~UserInterface();
...
private:
double MoveCurve_Delta;
MoveSeries *MOVE_SERIES ;
public:
void set_MoveCurve_Delta( double val) { MoveCurve_Delta = val;}
double get_MoveCurve_Delta() { return MoveCurve_Delta ;}
}
Userinterface.cpp:
UserInterface::UserInterface(QWidget *parent, Qt::WFlags flags) :
QMainWindow(parent, flags)
{
ui.setupUi(this);
...
MOVE_SERIES = new MoveSeries( this);
}
MoveSeries.h:
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(QWidget *parent = 0);
~MoveSeries();
...
MoveSeries.cpp:
MoveSeries::MoveSeries(QWidget *parent) :
QDialog(parent),ui(new Ui::MoveSeries)
{
ui->setupUi(this);
this->parent = parent;
parent->set-MoveSeries_Delta_Val();
}
Rather than assume that the parent QWidget in MoveSeries is UserInterface, you can also require that it is.
MoveSeries.h:
class UserInterface; // only need a forward declaration
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(UserInterface *parent = 0);
~MoveSeries();
...
UserInterface * uiparent;
}
MoveSeries.cpp:
#include "Userinterface.h" // include the header where it is required
MoveSeries::MoveSeries(UserInterface *parent) :
QDialog(parent), ui(new Ui::MoveSeries), uiparent(parent)
{
ui->setupUi(this);
uiparent->set-MoveSeries_Delta_Val();
}
It looks like you want to cast the parent to the class you want:
static_cast<UserInterface *>(parent)->get_MoveCurve_Delta();
Bear in mind that this could be dangerous as it makes an assumption about the type of the parent.
If you want only UserInterface be the parent of MoveSeries, say so:
explicit MoveSeries(UserInterface *parent = 0);
If you want any widget to be able to act as the parent, you cannot access UserInterface methods because the parent does not necessarily have them.

Qt property outside of base class

Im using a QML frontend for my C++ App which worked fine so far. However, I planned to tidy up my code and split functions into smaller classes
At first, my Property decleration looked like this:
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude MEMBER baroAltitude NOTIFY pressureChanged)
public:
explicit mainBoard(QObject *parent = 0);
void start();
private:
double baroAltitude = 0;
signals:
void pressureChanged();
};
Now, I do have this external class, with my getter method.
#include "pressuresensor.h"
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ pressureSensors.getBaroAltitude NOTIFY pressureSensors.pressureChanged)
public:
explicit mainBoard(QObject *parent = 0);
void start();
private:
pressureSensor pressureSensors;
};
But now, all I get is:
mainboard.h:25: Parse error at "pressureSensors"
error: [moc_mainboard.cpp] Error 1
Is there a better, or correct (because its working :D ) way for it?
thanks!
Q_PROPERTY does not support getters/setters methods which are not part of the class in question.
If you really want to keep the pressureSensor class you have to provide getters/setters in the mainBoard class and forward the calls.
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ getBaroAltitude)
public:
double getBaroAltitude() const {
return pressureSensors.getBaroAlitude();
}
private:
pressureSensor pressureSensors;
};

Inaccessible conversion in QObject

Here is my sample code:
class hoho : public QObject
{
Q_OBJECT
public:
hoho()
{
httpFetch = new HttpFetch(QUrl("http://www.google.com/"));
connect(httpFetch, SIGNAL(Fetched()), this, SLOT(PrintData(QByteArray)));
}
void PrintData(QByteArray http)
{
qDebug()<<http;
}
HttpFetch *httpFetch;
};
When I try to compile this, following error pops up
1>main.cpp(15): error C2243: 'type cast' : conversion from 'HttpFetch *' to 'const QObject *' exists, but is inaccessible
This error comes as the class is derived from QObject (which is necessary for signal and slot mechanism).
Can anyone tell me how to fix this?
You probably did not derive HttpFetch publicly, but privately from QObject. So just change
class HttpFetch : QObject { // ...
to
class HttpFetch : public QObject { // ...
and it should work.
If your design requires to make the inheritance non-public (I had this requirement because I inherited from a QWidget for a multithreading purpose and didn't want to expose all functions to the user), you can do this:
class FilesQueueQList : protected QWidget
{
Q_OBJECT
public:
using QWidget::QObject; //This is the solution!
//...
}
Now the members of QWidget are private/protected, but QObject is accessible as public.
Did you forget the Q_OBJECT macro in your class HttpFetch ?
Can I see your class HttpFetch ?

Can't cope with expected identifier

I'm making a tetris. Well, my glass (QtGlass.h) creates a figure.
I would like to use a parameter here to specify which shape the figure should
take.
Could you suggest me why parameters cause this error:
QtGlass.h:29:23: error: expected identifier before 'L'
QtGlass.h:29:23: error: expected ',' or '...' before 'L'
I've shown in the comments below where this error occurs.
By the way, if I uncomment the lines which signigy a parameterless variant,
it works.
**Figure.h**
class Figure : public QObject {
Q_OBJECT
...
public:
Figure(char Shape);
//Figure();
...
};
**Figure.cpp**
Figure::Figure(char Shape) {
//Figure::Figure() {
previous_shape = 1;
colour = RED;
...
}
**QtGlass.h**
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure('L'); //QtGlass.h:29:23: error: expected identifier before 'L' QtGlass.h:29:23: error: expected ',' or '...' before 'L'
//Figure the_figure;
...
};
Edded later
When I use this:
class QtGlass : public QFrame {
Q_OBJECT
QtGlass() : the_figure('L') {}
I get this:
QtGlass.cpp:164:50: error: no matching function for call to 'Figure::Figure()'
QtGlass.cpp:164:50: note: candidates are:
Figure.h:38:5: note: Figure::Figure(char)
Figure.h:38:5: note: candidate expects 1 argument, 0 provided
Figure.h:20:7: note: Figure::Figure(const Figure&)
Figure.h:20:7: note: candidate expects 1 argument, 0 provided
QtGlass.cpp
QtGlass::QtGlass(QWidget *parent) : QFrame(parent) {
key_pressed = false;
coord_x = 5;
coord_y = 5;
arrow_n = 0;
highest_line = 21;
this->initialize_glass();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(moveDownByTimer()));
timer->start(1000);
}
You can't initialize a member object using that syntax. If your compiler supports C++11's uniform initialization syntax or in-class initialization of member variables you can do this:
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure{'L'};
// or
Figure the_figure = 'L'; // works because Figure(char) is not explicit
...
};
Otherwise you need to initialize the object within QtGlass' constructor initializer list
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure;
...
};
// in QtGlass.cpp
QtGlass::QtGlass(QWidget *parent)
: QFrame(parent)
, the_figure('L')
{}
You are trying to create an instance of Figure in the definition of QtGlass which is not allowed. You have to instantiate the_figure in the constructor of QtGlass:
class QtGlass : public QFrame {
Q_OBJECT
QtGlass() {
the_figure = Figure('L');
};
...
protected:
Figure the_figure;
...
};

g++ Parse error at ":"

g++ is reporting a parse error with the code below:
class Sy_timeLineDelegateScene : public QGraphicsScene
{
Q_OBJECT
public:
Sy_timeLineDelegateScene( Sy_animPropertyTimeLine* timeline,
Sy_animClock* clock,
QObject* parent = nullptr );
virtual ~Sy_timeLineDelegateScene() {}
protected slots: // Parse error at ":"
typedef QMap< Sy::Frame, Sy_timeLineDelegateKey* > DelegateTimeLine;
...
My class is derived from QObject and I have declared the Q_OBJECT macro before the error, but if I comment out the slots part, it compiles fine. I have used Qt for years and never seen this before, it must be something stupid, but I can't see what's causing the problem.
The "slots" and "signals" sections in a class definition should only contain functions; neither types nor member variables.
You should move the typedef in a public, protected or private section:
class Sy_timeLineDelegateScene : public QGraphicsScene
{
Q_OBJECT
public:
Sy_timeLineDelegateScene( Sy_animPropertyTimeLine* timeline,
Sy_animClock* clock,
QObject* parent = nullptr );
virtual ~Sy_timeLineDelegateScene() {}
typedef QMap< Sy::Frame, Sy_timeLineDelegateKey* > DelegateTimeLine;
protected slots:
...