Refactoring Qt Quick Cpp class into header and .cpp - c++

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();
}

Related

missing ';' before '*' after instance declaration

I've encountered an error which I cannot seem to find a solution to anywhere else.
The error occurs when I'm trying to declare an instance of the "EncodeWindow" class.The compiler is giving errors C2143,C4430 and C2238. I am simply trying to give the "MainWindow" class an instance of "EncodeWindow".
File mainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QLabel>
#include "Image.h"
#include "encodewindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
/*Check whether a given file path is valid*/
bool CheckFilePath(QString);
/*Sets UI widgets*/
void setOriginalImage_Label(const char*);
void setEncodedImage_Label(const char*);
void setDebug_TextBox(QString);
/*Saves all information about current encoding/decoding*/
void saveFile();
private slots:
void on_Encode_Button_clicked();
void on_Reset_Button_clicked();
void on_Save_Button_clicked();
void on_AddEncodeImage_Debug_Button_clicked();
void on_AddImage_Button_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
EncodeWindow *CurrentEncodeWindow = new EncodeWindow; //ERROR HERE!! C2143
int fileNumber = 0;
};
#endif // MAINWINDOW_H
File encodeWindow.h:
#ifndef ENCODEWINDOW_H
#define ENCODEWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
namespace Ui {
class EncodeWindow;
}
class EncodeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit EncodeWindow(QWidget *parent = 0);
~EncodeWindow();
/*Get filepaths from 'result' object (For access to filepaths without 'encode' Window)*/
const char* getOriginalFilePath();
const char* getEncodeFilePath();
const char* getSaveFilePath();
bool checkUI(const char*,const char*,const char*,bool*);
bool CheckFilePath(const char*);
std::string readInText(const char*);
private slots:
void on_RedChannel_CheckBox_clicked(bool);
void on_GreenChannel_CheckBox_clicked(bool);
void on_BlueChannel_CheckBox_clicked(bool);
void on_AlphaChannel_CheckBox_clicked(bool);
void on_BitDepth_Slider_sliderMoved(int);
void on_BitDepth_SpinBox_valueChanged(int);
void on_Encode_Button_clicked();
private:
Ui::EncodeWindow *ui;
Encoded result; //Enocoded object (child of Image class)
};
#endif // ENCODEWINDOW_H
Any help would be great. The code was done in Qt for an image steganography project.
Thanks.
You have circular includes. "mainwindow.h" includes "encodewindow.h" and "encodewindow.h" includes "mainwindow.h". The include guards stop the cycle, but the class definitions for whichever header is included first won't be complete in the second include file, which will result in your errors.
In the snippet above, there's nothing that I see in encodewindow.h that uses anything in mainwindow.h, so just remove the include of mainwindow.h from encodewindow.h.

myClass not declared in this scope

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.

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.

Qt and C++ class giving me an error

what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"

C++: Problems with having a containing class call a function from a contained class

I believe this might be an issue of #including or forward declaring, rather than an issue of my syntax, since I'm currently getting errors of "invalid use of incomplete type 'struct MainWindow'", and "forward declaration of 'struct MainWindow' when I attempt to build the following in Qt Creator (Qt 4.7.4):
MYCLASS.H
#ifndef MYCLASS_H
#define MYCLASS_H
class MainWindow;
class MyClass
{
public:
MyClass(MainWindow * parent);
void callParentFunction();
private:
MainWindow *myPointer;
};
#endif // MYCLASS_H
MYCLASS.CPP
#include "myclass.h"
MyClass::MyClass(MainWindow *parent) : myPointer(parent)
{
}
void MyClass::callParentFunction()
{
myPointer->setSpinBoxValue(500);
}
MAINWINDOW.H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDoubleSpinBox>
#include "myClass.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void setSpinBoxValue(double x);
private:
QDoubleSpinBox *mySpinBox;
MyClass *myObject;
};
#endif // MAINWINDOW_H
MAINWINDOW.CPP
#include "mainwindow.h"
MainWindow::MainWindow()
{
mySpinBox = new QDoubleSpinBox;
setCentralWidget(mySpinBox);
myObject = new MyClass(this);
myObject->callParentFunction();
}
void MainWindow::setSpinBoxValue(double x)
{
mySpinBox->setValue(x);
}
I'd appreciate any ideas. Thanks!
You need to include mainwindow.h in myclass.cpp after myclass.h. In myclass.cpp you call a method of the MyClass (inside MyClass::callParentFunction), but at that point the compiler still only has the forward-declaration of MainWindow.