Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top