myClass not declared in this scope - c++

I make a project of music player in Qt based on QWidget class
and using widget.ui form.
I want to check that user will not add two labels with the same text, so I'm trying to add field: QList labelsList;
in my widget.h file.
(every time user add label, than: labelsList.append(label), and before he can add this label program would iterate through labelsList and check if in list exists label with patricular text).
Although "myqlabel.h" is included, compiler says that 'myQLabel' was not declared in this scope... I don't know why. Kind of weird for me, but maybe I lack/forget some basic knowledge... ;/
Thanks for help!
Code (just needed fragments) below:
widget.h file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "myqlabel.h"
#include <QList>
#include <QFormLayout>
#include <QSqlDatabase>
#include <QtSql>
#include <QMediaPlayer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
// to check if label with input text already exists:
// HERE OUR BAD FIELD:
QList<myQLabel*> labelsList;
private:
Ui::Widget *ui;
QMediaPlayer player;
qint64 duration;
};
#endif // WIDGET_H
myqlabel.h file:
#ifndef MYQLABEL_H
#define MYQLABEL_H
#include <QLabel>
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QMouseEvent>
class myQLabel : public QLabel {
Q_OBJECT
public:
myQLabel(QString& text, QFormLayout* parent = 0) : QLabel(text){
setAcceptDrops(true);
position = amount;
this->parent = parent;
labelsList.append(this);
}
};
#endif // MYQLABEL_H

You have a circular dependency in your includes. This means that one of the headers is not seen depending on the order in which they are first included (due to the include guards).
Remove #include "myqlabel.h" from widget.h and add it to widget.cpp. Then forward declare myQLabel by adding
class myQLabel;
to widget.h.

Related

QT: Error [expected ")" before "*" token and MainWindow does not name a type]

I got two errors in Qt, I wish you can help me with this. This is my first time I post something, please me help. This is the code:
#include <QMainWindow>
#include "mainwindow.h"
#include <QThread>
#include <QtCore>
#include "ui_mainwindow.h"
class HiloPrincipal : public QThread
{
Q_OBJECT
public:
explicit HiloPrincipal( MainWindow* parent = 0); // First error
MainWindow * _parent; // Second error
void run(); // Sacar Botella y poner botella.
signals:
public slots:
};
#endif // HILOS_H
I almost sure you have created include cycle.
Fix it like this:
#ifndef HILOS_H // this was missing! Probably when you did copy paste to question
#define HILOS_H
#include <QMainWindow>
// remove line: #include "mainwindow.h"
#include <QThread>
#include <QtCore>
// remove line: #include "ui_mainwindow.h"
// forward declaration
class MainWindow;
class HiloPrincipal : public QThread
{
Q_OBJECT
public:
explicit HiloPrincipal( MainWindow* parent = 0); // First error
MainWindow * _parent; // Second error
void run(); // Sacar Botella y poner botella.
signals:
public slots:
};
#endif // HILOS_H
Than include this headers files mainwindow.h in HiloPrincipal.cpp. This should solve build issue, but not actual problem.
Note problem is caused by invalid design of classes. Your HiloPrincipal thread shouldn't have any knowledge about MainWindow.
Also you're doing that wrong - this is invalid use of QThread.

Refactoring Qt Quick Cpp class into header and .cpp

I have a Cpp Class which I want to refactor into a header and .cpp file. No problem normally, but when I try to do this Qt Quick one I can't get it to compile. It's fine if I put all of it in the header file, but otherwise I get various different errors depending on how I try to do it. Is there a proper way. I think it has to do with the Q_INVOKABLE bit, but not sure.
Here is my code...
#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H
#include <QDateTime>
#include <QObject>
class ApplicationData : public QObject
{
Q_OBJECT
public:
ApplicationData(){}
Q_INVOKABLE QDateTime getCurrentDateTime() const{
return QDateTime::currentDateTime();
}
};
#endif // APPLICATIONDATA_H
Thanks for any pointers.
This compiles but I'm not sure why it does or why it didn't:
//header file
#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H
#include <QDateTime>
#include <QObject>
class ApplicationData : public QObject
{
Q_OBJECT
public:
ApplicationData(); //constructor
Q_INVOKABLE QDateTime getCurrentDateTime() const; //function
};
#endif // APPLICATIONDATA_H
//.cpp file
#include "applicationdata.h"
#include <QDateTime>
#include <QObject>
ApplicationData::ApplicationData(){} //constructor implementation
QDateTime ApplicationData::getCurrentDateTime() const{ //function implementation
return QDateTime::currentDateTime();
}

How to solve expected class-name before '{' in QT c++

I am implementing a library management system using Qt C++. I have a Material class which is a QMainwindow and when I click Fiction Section in menu bar Fiction form should be opened which is a QDialogbox. But although I implemented this concept I get the error which is "expected class-name before '{'". Please help to find the error. Thank You in advance.
This is material.h
#ifndef MATERIALS_H
#define MATERIALS_H
#include <QMainWindow>
#include "materialinner.h"
#include "fictionsection.h"
namespace Ui {
class Materials;
}
class Materials : public QMainWindow, public MaterialInner
{
Q_OBJECT
public:
explicit Materials(QWidget *parent = 0);
~Materials();
private slots:
void on_btnAdd_clicked();
void on_btnLoad_clicked();
void on_btnEdit_clicked();
void on_tblMaterial_clicked(const QModelIndex &index);
void on_btnSearch_clicked();
void on_actionClear_triggered();
void createAction();
void on_actionEdit_triggered();
void on_actionDelete_Records_triggered();
void on_actionFiction_section_triggered();
private:
Ui::Materials *ui;
FictionSection *fic;
};
#endif // MATERIALS_H
This is material.cpp
#include "materials.h"
#include "ui_materials.h"
#include <QDebug>
#include <QMessageBox>
Materials::Materials(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Materials)
{
ui->setupUi(this);
// QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}
Materials::~Materials()
{
delete ui;
}
void Materials::on_actionFiction_section_triggered()
{
/* this->hide();
fiction = new FictionSection();
fiction->show();*/
this->hide();
fic = new FictionSection();
fic->show();
}
This is fictionsection.h
#ifndef FICTIONSECTION_H
#define FICTIONSECTION_H
#include <QDialog>
#include "materials.h"
#include "materialinner.h"
namespace Ui {
class FictionSection;
}
class FictionSection : public QDialog, public Materials
**{**
Q_OBJECT
public:
explicit FictionSection(QWidget *parent = 0);
~FictionSection();
private:
Ui::FictionSection *ui;
};
#endif // FICTIONSECTION_H
Error occurs in functionsection.cpp class. And the curly brace where the error occured is bold.
With the following code snippet it gives the error of "request for memeber 'show' is ambiguous"
Material.cpp
#include "materials.h"
#include "ui_materials.h"
#include "fictionsection.h"
#include <QDebug>
#include <QMessageBox>
Materials::Materials(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Materials)
{
ui->setupUi(this);
// QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}
void Materials::on_actionFiction_section_triggered()
{
this->hide();
fiction = new FictionSection();
fiction->show();
}
How to solve this?
You have a circular dependency: materials.h includes fictionsection.h and fictionsection.h includes materials.h.
As your header files has routines to prevent multiple inclusion (#ifndef FICTIONSECTION_H and #ifndef MATERIALS_H, which are good), when material.h includes fictionsection.h, this one includes material.h again but this has absolutely no effect due to your multiple inclusion protection....consequence is that fictionsection.h does not get Materials declaration in the end and refuses to declare FictionSection deriving from it!
You need to use a forward declaration to solve that:
In materials.h, replace:
#include "fictionsection.h"
by
class FictionSection;
And add #include "fictionsection.h" in materials.cpp only.
Forward declaration is a common practice to resolve this problem. But, even without this problem occuring, forward declaration remains a good practice because it will speed up your compilation.

how to use objects of the widgets in QT

In my application I'm having 2 widgets named as widget and form. But if i try to create a pointer object of widget widget in widget form header file , it is giving the error like "Form does not name a type". Refer my used code below:
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "form.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w = new Widget();
w->show();
return a.exec();
}
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
#include "form.h"
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
Form *f ;//i try to create pointer object for Form
};
#endif // WIDGET_H
form.cpp
#include "form.h"
#include "widget.h"
Form::Form(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
form.h
#ifndef FORM_H
#define FORM_H
#include "ui_form.h"
#include "widget.h"
class Form : public QWidget, private Ui::Form
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
};
What I'm doing wrong?
The problem is that widget.h includes form.h, which includes widget.h. The header guards (#ifndef) cause the second include to be skipped.
For declaring a pointer variable in a header a forward declaration will suffice:
SomeClass.h
class Form; // forward declaration
class SomeClass {
public:
SomeClass();
// ...
private:
Form* form; // pointer to Form
};
SomeClass.cpp
SomeClass::SomeClass()
{
form = new Form();
}
You should put a forward declaration of class Form in widget.h instead of #includeing form.h. The problem is that you include form.h, which includes widget.h, which tries to include form.h, but can't because of the include guard. Therefore, in widget.h, class Form is undefined, although it looks to the user to be defined.

expected class-name before '{' token

I'm getting the error "expected class-name before '{' token" in my C++ Qt project. After googling it, it seems like its a problem of circular includes. I have pawn.h that includes piece.h, which includes board.h, which completes the circle by including pawn.h. I've read that this can be fixed with forward declarations, but I've tried forward declaring a few of problem classes, and it doesn't work.
#ifndef PAWN_H
#define PAWN_H
#include "piece.h"
class Pawn : public Piece
{
Q_OBJECT
public:
explicit Pawn(QWidget *parent = 0);
};
#endif // PAWN_H
.
#ifndef PIECE_H
#define PIECE_H
#include <QWidget>
#include "board.h"
class Board;
class Piece : public QWidget
{
Q_OBJECT
public:
explicit Piece(QWidget *parent = 0);
void setPosition(int rank, int file);
QPixmap pixmap;
protected:
void paintEvent(QPaintEvent *);
private:
int rank;
int file;
int x;
int y;
};
#endif // PIECE_H
.
#ifndef BOARD_H
#define BOARD_H
#include <QWidget>
#include <QVector>
#include <QGridLayout>
#include "square.h"
#include "pawn.h"
#include "knight.h"
#include "bishop.h"
#include "queen.h"
#include "king.h"
class Board : public QWidget
{
public:
explicit Board(QWidget *parent = 0);
QVector < QVector<Square *> > sqrVector;
Pawn *pawn[8];
Knight *knight[2];
Bishop *bishop[2];
Queen *queen;
King *king;
private:
QGridLayout *layout;
};
#endif // BOARD_H
Instead of randomly trying things, try changing board.h to include forward declarations for all the pieces:
board.h
class Pawn;
class Knight;
class Bishop;
class Queen;
class King;
And remove the corresponding #include statements. (You'll probably need to put those #include statements in board.cpp, when you decide you need to see inside the various piece classes.)
Your main problem lays in the file: piece.h. Since board is not referenced explicitly in the file whatsoever, the include for it and the forward declaration should be removed. That will break the circle. Additionally, as Greg pointed out, only forward declarations are needed in board.h.