I have an error that i can't include my header file in more than one cpp even though i have guard headers.
when removing the include of DatabaseManager from main the ccode builds just fine
here is the header file :
#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H
#include <QSqlDatabase>
#include <QSqlQuery>
class DatabaseManager
{
private:
QSqlDatabase PatternLibrary;
QSqlQuery query;
public:
DatabaseManager();
};
#endif
here is the .cpp:
#include "DatabaseManager.h"
#include <QSqlError>
#include <QDebug>
DatabaseManager::DatabaseManager()
{
}
and here is the main :
#include "DatabaseManager.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DatabaseManager x;
MainWindow w;
w.show();
return a.exec();
}
giving these errors :
/Code/DB_RangePattern-build-desktop-Qt_4_8_1_in_PATH_System_Debug/../DB_RangePattern/main.cpp:6: error: first defined here
collect2: ld returned 1 exit status
You've only posted one line of a larger error, but I can hazard a guess at what the problem is. You seem to be unsure of whether your class is DataBaseManager or DatabaseManager (note the change in capital B).
Also, if your header file is with the rest of your source files, make sure you're doing #include "DatabaseManager.h" (not using < and >).
I am pretty sure QSqlDatabase uses/include QSqlError because it has a defined public function
QSqlError lastError () const
and redefinition will come from your including QSqlError
Related
I encountered an error and I didn't find any solution (even over the internet)
I created aQt app to receive data using a TCP protocol and plot them using QcustomPlot.
I have the following files:
mainwindow.h :
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
#include <QVector>
#include <iostream>
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
// ...
private:
struct addrinfo _hints;
struct addrinfo* _result = NULL;
// ...
};
mainwindow.cpp :
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#include "mainwindow.h"
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//...
}
and the main.cpp file:
#include "mainwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I have the following error:
'MainWindow::hints' uses undefined struct 'addrinfo' (compiling source file main.cpp).
I don't understand why I have this error, because I tested the same program in a classic consol app following the microsoft tutorial and it was working.
I believe it comes from the includes, but I still dont have a clue about which one causes that.
You need to #include something in your mainwindow.h that defines struct addrinfo, because your MainWindow class has a member variable of that type. At the moment you include all the socket stuff only in your *.cpp file.
You use #define WIN32_LEAN_AND_MEAN that prevents including many dependent header files and makes you include required header files explicitly. As for addrinfo you have to #include <ws2def.h>.
I have this two class in c++
GUI.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() {
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w.show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
// TODO Auto-generated destructor stub
}
GUI.h
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mainwindow.h"
#ifndef GUI_H_
#define GUI_H_
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow w;
};
#endif
But when i run this program i have the error:
QWidget: Must construct a QApplication before a QPaintDevice
How can I declare MainWindow w in gui.h in such a way that I don't receive this error
You can't (well, you can, but you shouldn't). The MainWindon declaration is right where it should be. The problem is that you attempt to create a GUI object before you create the QApplication.
Why not create the QApplication where you create the GUI object, just before it?
I would have made w a pointer used a forward declaration for MainWindow and removed all the includes (including the 2 includes for mainwindow.h) from GUI.h. Then like the answer from Sebastian says construct the QApplication first.
AL_GUI.h
#ifndef GUI_H_
#define GUI_H_
class MainWindow;
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow* w;
};
gui.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() : w(NULL)
{
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w = new MainWindow;
w->show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
delete w;
}
I've just started programming in Qt framework. Following is a very simple program:
#include <QtCore/QCoreApplication>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass() {}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass *c = new MyClass();
return a.exec();
}
But I receive following error when I try to compile & run it:
In function MyClass:
undefined reference to vtable for MyClass
But when I remove the QObject macro everything works fine. Please note that the class is defined in the same file as the main function.
I'm using Qt version 4.7, running on Win 7.
What is causing this issue?
Update: I get the same error when I define my class in a separate header file. mytimer.h:
#ifndef MYTIMER_H
#define MYTIMER_H
#include <QtCore>
class MyTimer : public QObject
{
Q_OBJECT
public:
QTimer *timer;
MyTimer();
public slots:
void DisplayMessage();
};
#endif // MYTIMER_H
mytimer.cpp:
#include "mytimer.h"
#include <QtCore>
MyTimer::MyTimer()
{
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(DisplayMessage()));
timer->start(1000);
}
void MyTimer::DisplayMessage()
{
qDebug() << "timed out";
}
And this is the main.cpp:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include "mytimer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyTimer *mt = new MyTimer();
return a.exec();
}
You need to compile it using qmake, which is going to create mock methods for your custom QObject class. See here about more on generating moc files.
Since your example doesn't contain header files, it is not parsed, and no moc files are generated. You need to declare MyClass in a separate header file, and run moc generation tool.
When you are using QT Creator you should cleanup up your project and execute qmake, in the build menu.
Whenever u apply some changes first clean your project, then run qmake, and the finally build your project...
I am coding a project. I started to getting errors after I tried to make the static build. I made some changes, which I can't remember. But, I am sure that if this stub can be cleared, then the main project also can be get cleared.
This is header file.
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QFileDialog>
#include <QLabel>
#include <QMouseEvent>
#include <QObject>
#include <QPaintEvent>
class MyLabel:public QWidget
{
private:
QPixmap default_Pixmap;
QPixmap pixmap;
QFileDialog * fileDialog;
Q_OBJECT
public:
MyLabel();
void setPixmap(QPixmap pixmap);
void setDefault();
protected:
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent * event);
signals:
void file_Selected(QString fileName);
private slots:
void file_Got_Selected(QString fileName);
};
#endif // MYLABEL_H
Here is the source file
#include "MyLabel.h"
#include "MyMessageBox.h"
#include <QFileDialog>
#include <QPainter>
MyLabel::MyLabel():QWidget()
{
default_Pixmap = QPixmap("select.gif").scaled(250,100);
this->fileDialog=new QFileDialog(this);
fileDialog->setNameFilter("Image Files (*.BMP *.GIF *.JPG *.JPEG *.PNG *.PBM *.PGM *.PPM *.XBM *.XPM)");
connect(fileDialog,SIGNAL(fileSelected(QString)),this,SIGNAL(file_Selected(QString)));
connect(fileDialog,SIGNAL(fileSelected(QString)),this,SLOT(file_Got_Selected(QString)));
}
void MyLabel::setPixmap(QPixmap pixmap)
{
this->pixmap = pixmap;
}
void MyLabel::setDefault()
{
this->pixmap = default_Pixmap;
}
void MyLabel::mousePressEvent(QMouseEvent *event)
{
fileDialog->show();
//QString file_Name = file_Dialog.getOpenFileName();
}
void MyLabel::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
painter.drawPixmap(0,0,width(),height(),pixmap.scaled(QSize(width(),height())));
}
void MyLabel::file_Got_Selected(QString fileName)
{
this->pixmap = QPixmap(fileName);
}
#include "myLabel.moc"
And this is the main file
#include <QLabel>
#include <QPixmap>
#include <QtGui/QApplication>
#include "myLabel.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyLabel mm;
//mm.setPixmap(QPixmap("dummy.jpg"));
//mm.setPixmap(QPixmap());
mm.setDefault();
mm.show();
return a.exec();
}
I created the moc file using qt command prompt and the command
moc myLabel.h -o myLabel.moc
After this I tried to compile the project through Qt-Editor. But I got multi definition error as follows,
debug/moc_myLabel.o:d:/TempInstallationFolder/Qt/Dynamic/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `MyLabel::metaObject() const'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:57: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:62: multiple definition of `MyLabel::qt_metacast(char const*)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:62: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:70: multiple definition of `MyLabel::qt_metacall(QMetaObject::Call, int, void**)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:70: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:87: multiple definition of `MyLabel::file_Selected(QString)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:87: first defined here
debug/moc_myLabel.o:moc_myLabel.cpp:(.data+0x0): multiple definition of `MyLabel::staticMetaObject'
debug/myLabel.o:myLabel.cpp:(.data+0x0): first defined here
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\CalendarNew.exe] Error 1
mingw32-make: * [debug] Error 2
The process "D:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project CalendarNew (target: Desktop)
When executing build step 'Make'
Anybody please hep me out of this problem.
Try to remove line include "MyLabel.moc" form you source file. You don't need to include it into cpp file.
I'm trying to write an application using QNetworkManager. I have simplified the code down to the problem. The following code hangs, and I have no idea why:
main.cpp:
#include <QApplication>
#include "post.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
post("http://google.com/search", "q=test");
return app.exec();
}
post.h:
#ifndef _H_POST
#define _H_POST
#include <QNetworkAccessManager>
#include <QNetworkRequest>
class post : public QObject {
Q_OBJECT
public:
post(QString URL, QString data);
public slots:
void postFinished(QNetworkReply* reply);
protected:
QNetworkAccessManager *connection;
};
#endif
post.cpp:
#include <QApplication>
#include <QUrl>
#include "post.h"
post::post(QString URL, QString data) {
connection = new QNetworkAccessManager(this);
connect(connection, SIGNAL(finished(QNetworkReply*)), this, SLOT(postFinished(QNetworkReply*)));
connection->post(QNetworkRequest(QUrl(URL)), data.toAscii());
}
void post::postFinished(QNetworkReply*) {
qApp->exit(0);
}
Some Googling shows it may be because I have everything on one thread, but I have no idea how to change that in Qt... none of the network examples show this.
I just tried it with the same results. The problem is that you are creating the post object by only calling the constructor. Since you are not specifying an object it is getting destroyed right away (to check this create a destructor and see when it gets called.)
try:
post p("http://google.com/search","q=test");
Then your slot gets called.