2

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?

mhcuervo
  • 2,610
  • 22
  • 33
Sailordi
  • 73
  • 1
  • 3
  • 8
  • Does your project file (.pro ) include message_function.cpp in the SOURCES += section and message_function.h in the HEADER += section? If you're using the Qt Creator IDE it would normally add these automatically but maybe something went wrong. – Tod May 15 '14 at 06:26

2 Answers2

4

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");
}
mhcuervo
  • 2,610
  • 22
  • 33
  • tried the changes you suggested but i still get the same error message,and i would like you to explain why this would not bee a good programming practice – Sailordi May 15 '14 at 05:14
  • I've compiled the code with clang on Mac and with mingw on Windows and the program compiles and run as expected. – mhcuervo May 15 '14 at 05:51
  • Which C++ compiler are you using? – mhcuervo May 15 '14 at 05:52
  • im using mingw and i am programming in windows – Sailordi May 15 '14 at 06:10
  • In reference to good programming practices, I would guess that @mhcuervo is talking about the global variable MeFu. Any reason you can't declare that inside the MainWindow constructor as a local variable, or if it's needed more than once, make it a member of MainWindow? Those would both be better approaches. – Tod May 15 '14 at 06:34
  • mhcuervo Got it to work now after re-installing Qt, I apparently had a bad install and that caused the error.@Nicholas Smith I would use signals and slots if I did not ned to re-use a lot of the code in many slots.Tod I would make my desired functions a member of MainWindow, but it would get messy having all of my code there. I like to have all of the functionality of my program split up into classes. – Sailordi May 15 '14 at 08:55
2

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.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55