Qt C++ - Can't add slots - c++

When I add slots to my script it will no longer build.
inkpuppet.obj:-1: error: LNK2005: "private: void __cdecl InkPuppet::on_aboutButton_clicked(void)" (?on_aboutButton_clicked#InkPuppet##AEAAXXZ) already defined in main.obj
and
debug\InkPuppet.exe:-1: error: LNK1169: one or more multiply defined symbols found
Here is the code:
inkpuppet.h - commenting out void on_aboutButton_clicked(); and the function at the end will make it run.
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCore>
namespace Ui {
class InkPuppet;
}
class InkPuppet : public QWidget
{
Q_OBJECT
public:
explicit InkPuppet(QWidget *parent = 0);
~InkPuppet();
private:
Ui::InkPuppet *ui;
private slots:
void on_aboutButton_clicked();
};
#endif // WIDGET_H
void InkPuppet::on_aboutButton_clicked()
{
}
inkpuppet.cpp
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
InkPuppet::InkPuppet(QWidget *parent) :
QWidget(parent),
ui(new Ui::InkPuppet)
{
ui->setupUi(this);
//connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), ui->timeSlider, SLOT(setRange(int,int)));
}
InkPuppet::~InkPuppet()
{
delete ui;
}
main.cpp
#include "inkpuppet.h"
#include "aboutdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
InkPuppet w;
w.show();
return a.exec();
}
aboutdialog.h
#ifndef ABOUTDIALOG_H
#define ABOUTDIALOG_H
#include <QDialog>
namespace Ui {
class AboutDialog;
}
class AboutDialog : public QDialog
{
Q_OBJECT
public:
explicit AboutDialog(QWidget *parent = 0);
~AboutDialog();
private:
Ui::AboutDialog *ui;
};
#endif // ABOUTDIALOG_H
aboutdialog.cpp
#include "aboutdialog.h"
#include "ui_aboutdialog.h"
AboutDialog::AboutDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AboutDialog)
{
ui->setupUi(this);
}
AboutDialog::~AboutDialog()
{
delete ui;
}

You define your void InkPuppet::on_aboutButton_clicked() in your inkpuppet.h. And then you include it in inkpuppet.cpp AND in main.cpp -> one or more multiply defined symbols found.
Put
void InkPuppet::on_aboutButton_clicked()
{
}
in your inkpuppet.cpp file.

If the first file you pasted is a whole, there's a problem with the include guards. The definition is after the guard's end.
#endif // WIDGET_H
void InkPuppet::on_aboutButton_clicked()
{
}
Your definition is right after the #endif which means as soon as in the same translation unit, the header is included twice, you'll get this error. And this happens in your code because inkpuppet.h is included in both main.cpp and in inkpuppet.cpp You should put the implementation code for on_aboutButton_clicked() in your inkpuppet.cpp file.

Related

developing an application that display an image on a label(control) and after click that image the new image will display in other label (control)

i am working on QT C++,developing an application that display an image on a label(control) and after click that image the new image will display in other label (control). i create a new project: application->QtWidget Application. then i select class Qdialog. in project file are
- my_qlabel_mouseEvent.pro
and in header folder are
- dialog.h
- my_qlabel.h
and then source folder has files
1: dialog.cpp
2:main.cpp
3: my_qlabel.cpp
and then in form folder has
1: dialog.cpp.
when i add the new class my_qlabel with base class QLabel, type information: QWidget after creating this the setPixmap does not work. its show error the error is that "in constructor 'Dialog::Dialog(QWidget*)' class my_qlabel has no member named setPixmap" please solve this error or reffer me any site or other sources.
**my_qlabel.cpp**
#include "my_qlabel.h"
#include <QPixmap>
my_qlabel::my_qlabel(QWidget *parent) : QWidget(parent)
{
}
void my_qlabel::mouseMoveEvent(QMouseEvent *)
{
emit Mouse_Pos();
}
void my_qlabel::mousePressEvent(QMouseEvent *)
{
emit Mouse_Pressed();
}
void my_qlabel::leaveEvent(QEvent *)
{
emit Mouse_Left();
}
**main.cpp**
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
**dialog.cpp**
#include "dialog.h"
#include "ui_dialog.h"
#include "my_qlabel.h"
#include <QPixmap>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QPixmap pix("E:/osld/andgate.png");
ui->lblMouse->setPixmap(pix);
connect(ui->lblMouse, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
connect(ui->lblMouse, SIGNAL(Mouse_Pos()), this, SLOT(Mouse_current_pos()));
connect(ui->lblMouse, SIGNAL(Mouse_Left()), this, SLOT(Mouse_left()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::Mouse_current_pos()
{
ui->lblMouse_Current_Event->setText("Mouse Moving");
}
void Dialog::Mouse_Pressed()
{
ui->lblMouse_Current_Event->setText("Mouse Pressed");
}
void Dialog::Mouse_left()
{
ui->lblMouse_Current_Event->setText("Mouse Left");
}
**dialog.h**
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void Mouse_current_pos();
void Mouse_Pressed();
void Mouse_left();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
**my_qlabel.h**
#ifndef MY_QLABEL_H
#define MY_QLABEL_H
#include <QWidget>
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>
#include <QPixmap>
class my_qlabel : public QWidget
{
Q_OBJECT
public:
explicit my_qlabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void leaveEvent(QEvent *);
signals:
void Mouse_Pressed();
void Mouse_Pos();
void Mouse_Left();
public slots:
};
#endif // MY_QLABEL_H

Error while declaring QVector of class type

Getting error while declaring QVector of class type in Qt.
Error :"incomplete type is not allowed"
I didn't understand what causes this error.
if i #include "storage.h" in main.cpp and declare a Qvector of class storeage it works fine but when i do the same in waveform class it reports an error.
I've tried forward declaring storage class in waveform class but still getting the same error.
Any Help??
main.cpp
#include "test_subject_02.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TEST_SUBJECT_02 w;
w.show();
return a.exec();
}
test_subject_02.h
#ifndef TEST_SUBJECT_02_H
#define TEST_SUBJECT_02_H
#include <QtGui/QMainWindow>
#include "ui_test_subject_02.h"
#include"waveform.h"
#include "storage.h"
class TEST_SUBJECT_02 : public QMainWindow
{
Q_OBJECT
public:
TEST_SUBJECT_02(QWidget *parent = 0, Qt::WFlags flags = 0);
waveform *wv;
~TEST_SUBJECT_02();
private:
Ui::TEST_SUBJECT_02Class ui;
};
#endif // TEST_SUBJECT_02_H
test_subject_02.cpp
#include "test_subject_02.h"
TEST_SUBJECT_02::TEST_SUBJECT_02(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QVector<storage> ser; //works fine here
wv->readfile("e:/testing2/result/3/abc.cur");
}
TEST_SUBJECT_02::~TEST_SUBJECT_02()
{
}
waveform.h
#ifndef WAVEFORM_H
#define WAVEFORM_H
#include "storage.h"
#include <QObject>
class waveform : public QObject
{
Q_OBJECT
public:
waveform(QObject *parent=0);
void readfile(QString);
QVector <storage> myvector ; // incomplete type is not allowed
~waveform();
private:
};
#endif // WAVEFORM_H
waveform.cpp
#include "waveform.h"
waveform::waveform(QObject *parent)
: QObject(parent)
{
}
void waveform::readfile(QString file)
{
QVector<storage> sw; //again error incomplete type is not allowed
}
waveform::~waveform()
{
}
storage.h
#ifndef STORAGE_H
#define STORAGE_H
#include <QObject>
class storage : public QObject
{
Q_OBJECT
public:
storage(QObject *parent);
storage();
storage(QString,QString);
~storage();
private:
QString x;
QString y;
};
#endif // STORAGE_H
storage.cpp
#include "storage.h"
storage::storage(QObject *parent)
: QObject(parent)
{
}
storage::storage(QString xcord,QString ycord)
{
x=xcord;
y=ycord;
}
storage::storage()
{
}
storage::~storage()
{
}
You need to explicitly include QVector in necessary file (waveform.h i believe), since QT uses a lot of forward declarations, while they appear as correct classes in IDE, but they won't compile, if proper definition is not included in file.

Access Qt ui from another class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am trying to make a class in Qt that will change things on my MainWindow.
This is my code so far:
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
message_function.h (My Class header)
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
namespace Ui { class MainWindow; }
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp (My Class .cpp)
#include "message_function.h"
void message_function::Print(Ui::MainWindow *ui)
{
ui->label->setText("This is a test");
}
When i buld my project i get this error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall message_function::Print(class Ui::MainWindow *)" (?Print#message_function##QAEXPAVMainWindow#Ui###Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QAE#PAVQWidget###Z)
I don't know if i am doing this correctly or if i am doing something wrong.
Can somebody help me get this to work?
Sailordi, the error is in the way you're including the header files.
I did some modifications that should fix it. However I'd like to tell you that what you're doing is not a good programming practice.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
message_function.h
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp
#include "message_function.h"
#include "ui_mainwindow.h"
void message_function::Print(Ui::MainWindow* ui)
{
ui->label->setText("This is a test");
}
You shouldn't really be trying to access the MainWindow UI from another class, a better solution is to create a signal in your class that you want to make the change from and a slot in MainWindow that executes the change.

C2143: syntax error : missing ';' before '*'

Following bit of code is throwing error. I have no idea why. Can anyone shed some light?
All codes are on different files.
#ifndef MAINSESSION_H
#define MAINSESSION_H
#include "sessionsuper.h"
#include "mainwindow.h"
class MainSession : public SessionSuper
{
public:
MainSession();
private:
};
#include "mainsession.h"
MainSession::MainSession()
{
}
#endif // MAINSESSION_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mainsession.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MainSession *ms; //Error here
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ms=new MainSession(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
#ifndef SESSIONSUPER_H
#define SESSIONSUPER_H
class SessionSuper
{
public:
SessionSuper();
};
#endif // SESSIONSUPER_H
#include "sessionsuper.h"
SessionSuper::SessionSuper()
{
}
Error:
d:\qtsrc\untitled4\mainwindow.h:20: error: C2143: syntax error :
missing ';' before '*'
d:\qtsrc\untitled4\mainwindow.h:20: error: C4430: missing type
specifier - int assumed. Note: C++ does not support default-int
d:\qtsrc\untitled4\mainwindow.h:20: error: C4430: missing type
specifier - int assumed. Note: C++ does not support default-int
Am using Qt+msvc10.0 compiler.
Update:-
#ifndef MAINSESSION_H
#define MAINSESSION_H
#include "sessionsuper.h"
#include "mainwindow.h"
class MainSession : public SessionSuper
{
public:
MainSession(MainWindow*);
private:
MainWindow *mw;
};
#endif // MAINSESSION_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mainsession.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MainSession *ms;
};
#endif // MAINWINDOW_H
#ifndef SESSIONSUPER_H
#define SESSIONSUPER_H
class SessionSuper
{
public:
SessionSuper();
};
#endif // SESSIONSUPER_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainsession.h"
MainSession::MainSession(MainWindow mss)
{
mw=mss;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ms=new MainSession(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "sessionsuper.h"
SessionSuper::SessionSuper()
{
}
Errors:- a lot more but of same type
You have circular include, forward declaration MainSession type to break the current circula include issue.
In MainWindow.h
//#include "mainsession.h" comment out this line
class MainSession; // add forward declaration
class MainWindow : public QMainWindow
{
//...
MainSession *ms; //Error here.
};
I have checked your code like this:
class MainWindow
{
public:
explicit MainWindow();
~MainWindow();
private:
Ui::MainWindow *ui;
MainSession *ms; //My error also here <- see this
};
See here in my code where MainSession is missing and I got same error in the line. Hope it will help. MainSession definition may missing because of file missing, file not included, scope problem (another namespace) etc. Please check these. namespace Ui (different) probably the problem.
Problem Is Solved by using the observer Model.
A Complete Demo Here
Add a Comment If you want the working code of the above code.
Cheers!!!

C++ Qt - strange behavior when Class T's attribute has parent is T

Not sure this issue about Qt or C++ in general, I'm just a newbie for both of these!
I got a simple Qt app, with a MainWindow and Hello class like below:
hello.h
#ifndef HELLO_H
#define HELLO_H
#include <QWidget>
#include "mainwindow.h"
class Hello : public QWidget
{
Q_OBJECT
public:
explicit Hello(MainWindow *parent = 0);
signals:
public slots:
};
#endif // HELLO_H
heloo.cpp
#include "hello.h"
Hello::Hello(MainWindow *parent) :
QWidget(parent)
{
//nothing here yet
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "hello.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Hello* hi;
};
#endif // MAINWINDOW_H
mainwindows.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
hi = new Hello(this);
}
MainWindow::~MainWindow()
{
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and here is the error when I build my project:
from ../untitled1/main.cpp:2: ../untitled1/hello.h:11: error: expected
‘)’ before ‘*’ token
and the line cause the error is:
explicit Hello(MainWindow *parent = 0);
Can you help me to resolve the issue!
Thanks you!
You have circular inclusion of header files in "hello.h" and "maindwindow.h". There is no need to include these files in the header file as you are just using a pointer. A simple forward declaration such as class MainWindow; in "hello.h" is sufficient.