How to solve expected class-name before '{' in QT c++ - 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.

Related

Downloading txt file with qtnetwork

I am trying to download a txt file from a url in QT but i can't seem to make it work.
I am following this guide https://wiki.qt.io/Download_Data_from_URL. I implemented the filedownloader class exactly like it's made in the guide, but when i try to use it like specified in the guide I cannot make it work. I created a slot to be called when the download is finished, but if i try to call the downloader inside like the guide it says it is an undeclared identifier.
Does anyone know how to correctly implement this code?
this is the .cpp of my code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <filedownloader.h>
#include <iostream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl emiurl( "url of my file");
// call to the downloader class.In the guide it's made differently, but it doesn't work
FileDownloader emiload(emiurl,this);
//this connect links the end of the download with the textwriter slot
QObject::connect(&emiload, SIGNAL (downloaded()), this, SLOT (textwriter()));
}
>MainWindow::~MainWindow()
{
delete ui;
}
//slot needed to create the txt file from the downloaded one
void MainWindow::textwriter()
{
QByteArray emibyte;
emibyte=emiload->downloadedData(); //this line gives me error
QFile emifile("emi.txt");
emifile.open(QIODevice::WriteOnly);
std::cout << emibyte.size() << std::endl;
QDataStream out(&emifile);
out << emibyte;
std::cout << emifile.size() << std::endl;
}
Now here's the .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "filedownloader.h"
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void textwriter();
};
#endif // MAINWINDOW_H
To make the undeclared identifier go away and successfully compile, you need to add FileDownloader to the class' declaration. This way, it will be known throughout the class.
I chose to go with the approach that's usual in Qt, to declare a pointer to FileDownloader.
#pragma once // <--- this is supported by virtually any compiler today
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class FileDownloader; // <-- forward declaration is enough, but you can also #include "filedownloader.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
Ui::MainWindow* ui = nullptr;
FileDownloader* emiload = nullptr; // <--- the important line!
private slots:
void textwriter();
};
And then instantiate and call emiload in the constructor:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create an instance of FileDownloader with "new".
emiload = new FileDownloader(QUrl("url of my file"), this);
// using member pointer connection available since Qt5
connect(emiload, &FileDownloader::downloaded, this, &MainWindow::textwriter);
}

C++ Qt4.8 :: Pass Object to another Class - member access into incomplete type error

I am new in C++ Qt and struggling with the correct use of forward declarations and #include.
What I want to do:
I have a Qt Gui (Class Ui::Gui) where we can set values.
I want to save these values in Gui Class variables.
As soon as a button (Generate Xml) is clicked, I want to pass the object
'ui' to the XmlGeneratorClass, So i can use the values to generate a Xml.
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include <QDebug>
#include "xmlgeneratorqobject.h"
namespace Ui {
class Gui;
}
class Gui : public QMainWindow
{
Q_OBJECT
public:
explicit Gui(QWidget *parent = nullptr);
~Gui();
qint8 testvalue = 1;
signals:
void transmitToXmlGen(Ui::Gui*);
private slots:
void on_pushButtonGenerateXml_clicked();
private:
Ui::Gui *ui;
XmlGeneratorQObject *xmlgenerator = new XmlGeneratorQObject();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
#include "ui_gui.h"
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
connect(this,SIGNAL(transmitToXmlGen(Ui::Gui*)),xmlgenerator,SLOT(receiveFromGui(Ui::Gui*)));
}
Gui::~Gui()
{
delete ui;
}
void Gui::on_pushButtonGenerateXml_clicked()
{
emit transmitToXmlGen(ui);
}
xmlgeneratorqobject.h
#ifndef XMLGENERATORQOBJECT_H
#define XMLGENERATORQOBJECT_H
#include <QObject>
#include <QDebug>
namespace Ui {
class XmlGeneratorQObject;
class Gui;
}
class XmlGeneratorQObject : public QObject {
Q_OBJECT
public:
explicit XmlGeneratorQObject(QObject * parent = nullptr);
private slots:
void receiveFromGui(Ui::Gui*);
};
#endif // XMLGENERATORQOBJECT_H
xmlgeneratorqobject.cpp
#include "xmlgeneratorqobject.h"
XmlGeneratorQObject::XmlGeneratorQObject(QObject *parent){}
void XmlGeneratorQObject::receiveFromGui(Ui::Gui* objectFromGui)
{
qDebug() << objectFromGui->testvalue; // ERROR member access into incomplete type 'Ui::Gui'
}
Expected result:
Access to public variables from passed gui-object should be possible
Actual result:
member access into incomplete type 'Ui::Gui'
Can you please help me learn forward declaration / include?
Is my approach in general okay?
Your xmlgeneratorqobject.cpp needs the line
#include "ui_gui.h"
This gives it the details of the ui widgets. This file is generated by the Qt build system.

Qt compiler error: 'emit' was not declared in this scope

I am trying to create a Simple GUI which creates multiple threads and perform some operation at the background while the GUI being responsive all the time. I am using QThreads of QT framework to achieve this but I am facing above said issue. Below is the code.
//Threading.h
This is my threading.h file.
#ifndef THREADING
#define THREADING
#include <QThread>
#include <QObject>
class Threading : public QThread
{
Q_OBJECT
private:
int num;
public:
explicit Threading(QObject * parent = 0);
void run();
void set_num(int);
int get_num();
Q_SIGNALS:
void someSignal(int);
};
//This is threading.cpp file
#include "threading.h"
#include <QtCore>
Threading::Threading(QObject *parent) : QThread(parent)
{
}
void Threading:: run()
{
emit someSignal(get_num());
}
void Threading :: set_num(int num)
{
QMutex mutex;
mutex.lock();
this->num = num;
mutex.unlock();
}
int Threading :: get_num()
{
return num;
}
//Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "threading.h"
typedef unsigned char byte;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Threading *threadPointer;
};
//Mainwindow.cpp
In this file I am starting thread.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "global.h"
#include <QtCore>
#include <QObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui-> setupUi(this);
threadPointer = new Threading(this);
connect(threadPointer,SIGNAL(someSignal(int)),this,SLOT(onSomeSignal()));
}
void MainWindow::on_clicked()
{
threadPointer->set_num(0);
threadPointer->start();
}
I saw some video online which has exactly similar code which is strangely but working fine and mine is not. Does it have to do with version ? Any help would be appreciated.
You can bypass the issue using Q_EMIT in place of emit, or just call the signal as a normal function (emit is optional and is there just for code readability):
void Threading:: run()
{
someSignal(get_num());
}
emit is an empty macro defined in qobjectdefs.h. You should investigate further, and try to understand why it is not defined (e.g. if QT_NO_KEYWORDS is defined somewhere and why).
You may also want to check if a
CONFIG += no_keywords
line exists in your pro file, as explained at the very end of this.

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.

undeclared identifier error with correct include and declaration

I'm creating an application in Qt
i get a compilation error: error C2065: 'callDialog' : undeclared identifier
in my CategoryDialog class
error line:
CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1));
5th last line in CategoryDialog class:
#include "ui_categorydialog_new.h"
#include "calldialog.h"
#include "questionsdialog.h"
#include "../objects/call.h"
#include "../webservice/dataconnector.h"
#include "../webservice/dataconngetter.h"
namespace Ui {
class CategoryDialog;
}
class CategoryDialog : public QDialog
{
Q_OBJECT
public:
explicit CategoryDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CategoryDialog){
ui->setupUi(this);
}
~CategoryDialog()
{
delete ui;
}
private slots:
void on_btn_back_clicked()
{
ui->btn_back->setEnabled(false);
DataConnGetter::getConnector()->setCallAbort(call->getId()); //get errormessage back and show errormessage when nessesary
QStackedWidget* dialogStack = (QStackedWidget*)this->parentWidget();
CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1)); //TODO 005 replace indexes with enums > more clear
callDialog->updateCalls(false);
dialogStack->setCurrentIndex(1);
ui->btn_back->setEnabled(true);
}
the CallDialog class looks like this
#include <QDialog>
#include <QString>
#include <QList>
#include <QSound>
#include <QtDebug>
#include <QStringList>
#include <QPushButton>
#include <QStackedWidget>
#include <QtAlgorithms>
#include <QLabel>
#include <typeinfo>
#include "ui_calldialog.h"
#include "callbutton.h"
#include "categorydialog_new.h"
#include "../settings/consts.h"
#include "../webservice/dataconnector.h"
#include "../webservice/dataconngetter.h"
#include "../settings/programsettings.h"
#include "../webservice/pollingservice.h"
class PollingService;
namespace Ui {
class CallDialog;
}
class CallDialog : public QDialog
{
Q_OBJECT
public:
// explicit CallDialog(QWidget *parent = 0);
// ~CallDialog();
// void initCalls();
// void updateCalls(bool sound);
// void enablePoller();
explicit CallDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CallDialog)
{
ui->setupUi(this);
installEventFilter(this);
notice the correct include in the CategoryDialog
they do include each others (maybe cyclic dependency problem?)
I tried with forward declaring CallDialog. didn't help.
the files are only the .h files with implementation directly inside
EDIT
I bypassed the problem as follows:
i added an abstract class with the function that CallDialog used from CategoryDialog
like this:
#ifndef ABSTRACTDIALOG_H
#define ABSTRACTDIALOG_H
#include <QDialog>
#include "../objects/call.h"
class AbstractDialog : public QDialog
{
Q_OBJECT
public:
explicit AbstractDialog(QWidget *parent = 0) : QDialog(parent){}
void setCall(Call* call){
_call = call;
}
private:
Call* _call;
};
#endif // ABSTRACTDIALOG_H
Forward declaring CallDialog before CategoryDialog won't work since CallDialog member functions are used and the compiler needs to know about them.
Did you try forward declaring CategoryDialog in before CallDialog and remove, I'm guessing, #include "categorydialog_new.h"? Since the all of calldialog.h is not shown, I'm not sure how you are using CategoryDialog in that file.