Вопрос

I have a Qt project made by Qt creator. I let the creator itself generate a private slots function fx. on_pushbutton_clicked(). This function is declared in header, the function itself is in the cpp file created by the Qt creator. When I move the function from cpp file generated by Qt creator to another cpp file (it is added in the project, it has the same includes as the generated cpp. When I try to compile it, I get lnk2019 error. Is there any way to have slots functions in different files? I am using VC compiler. Okay, here is extract from the code. (it is quite long) gui.h

#ifndef GUI_H
#define GUI_H

#include <QMainWindow>
#include "buffer.h"
#include <string>
#include <map>
#include <math.h>
#include <sstream>
#include <stdlib.h>
#include "qcustomplot.h"
#include <limits>
#include <time.h>
#include <random>


namespace Ui {
class GUI;
}

class GUI : public QMainWindow
{
    Q_OBJECT

public:
    explicit GUI(QWidget *parent = 0);
    ~GUI();
private slots:

    void on_setall_clicked();

    void on_integrace_button_clicked();
    void on_elipsy_button_clicked();
    void on_grafy_button_clicked();

private:
    Ui::GUI *ui;



};
#endif // GUI_H

gui.cpp

#include "gui.h"
#include "ui_gui.h"

double drand(double from, double to){
    double f = (double)rand()/RAND_MAX;
    return from + f * (to - from);
}

GUI::GUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GUI)
{
    ui->setupUi(this);
    srand(time(0));
}

GUI::~GUI()
{
    delete ui;
}
void GUI::on_setall_clicked(){...};
void GUI::on_grafy_button_clicked(){...};
void GUI::on_integrace_button_clicked(){...};

elipsy.cpp

#include "gui.h"
void GUI::on_elipsy_button_clicked(){...};

GUI.pro

#-------------------------------------------------
#
# Project created by QtCreator 2013-03-27T09:01:31
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport

TARGET = GUI
TEMPLATE = app


SOURCES += main.cpp\
        gui.cpp \
    solve_rpn.cpp \
    shunting_yard.cpp \
    qcustomplot.cpp \
    elipsy.cpp \
    grafy.cpp \
    integrace.cpp

HEADERS  += gui.h \
    buffer.h \
    qcustomplot.h

FORMS    += gui.ui

And the error code it gives me when i try to compile with the function elipsy_button_clicked() in file other than gui.cpp

moc_gui.obj:-1: Chyba:LNK2019: unresolved external symbol "private: void __cdecl GUI::on_elipsy_button_clicked(void)" (?on_elipsy_button_clicked@GUI@@AEAAXXZ) referenced in function "private: static void __cdecl GUI::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@GUI@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)
debug\GUI.exe:-1: Chyba:LNK1120: 1 unresolved externals

Well, in case you need the entire sourcecode, I uploaded it http://seed.gweana.eu/public/GUI.7z

FIXED: The file was ignored by the project, running qmake again solved the issue. Many thanks for the answers :)

Это было полезно?

Решение

One of the problems is that you exported the slot methods to elipsy.cpp where on line 14 you try to use: ui ... which is defined in ui_gui.h included only in gui.cpp, but forward declared in gui.h (which you include of course in elipsy.cpp) so this should give you a compilation error. Solution: include ui_gui.h in elipsy.cpp. If it doesn't give you a compilation error try to rebuild the application.

Secondly, your drand function is defined in gui.cpp but not in any header file (easily fixable, modify gui.h)...

After fixing these two issues the compilation was ok for me.

So, a few recommendations:

  1. Leave the things as they are when comes to Qt ... you will just mess up your head when moving things around.
  2. have a separate "module" for utilities, such as drand

(PS: Nice app :) )

Другие советы

Generally slots are declared in a header file like,in my Counter.h :

    #include <QObject>

    class Counter : public QObject
   {
     Q_OBJECT

    public:
     Counter() { m_value = 0; }

     int value() const { return m_value; }

   public slots:
    void setValue(int value);
 ....

Now in Counter.cpp(and it has to include Counter.h), i will define this function like any other normal function.

So in this case everything will work correctly.Does that answers your question?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top