1

I want to create my own custom widget that extends a QFrame, however when I am trying to make the constructor I get an error.

#ifndef CONTROLFRAME_H
#define CONTROLFRAME_H

#include <QObject>
#include <QFrame>
#include <QWidget>
#include <QtGui>

class ControlFrame : public QFrame
{
    Q_OBJECT

public:
    ControlFrame(QWidget *parent = 0);
    ~ControlFrame();

private:
    QWidget *m_parent;
};

#endif // CONTROLFRAME_H

and CPP

#include "controlframe.h"

ControlFrame::ControlFrame(QWidget *parent)
    : QFrame(parent)
{
    m_parent = parent;
}

ControlFrame::~ControlFrame()
{

}

Sadly, I get the following error

Undefined symbols for architecture x86_64:
  "vtable for ControlFrame", referenced from:
      ControlFrame::ControlFrame(QWidget*) in controlframe.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [PFEtest.app/Contents/MacOS/PFEtest] Error 1
18:54:21: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project PFEtest (kit: Desktop Qt 5.4.0 clang 64bit)
When executing step "Make"

What am I doing wrong?

dominicbri7
  • 2,479
  • 4
  • 23
  • 33
  • What build system are you using? Did you add the MOC pass to the compilation? – Matteo Italia Mar 11 '15 at 00:06
  • @MatteoItalia I am using the latest Qt Creator on Mac OS X Yosemite, default install with latest Qt version. Other than that I have no idea what you're asking for (pardon my ignorance) – dominicbri7 Mar 11 '15 at 02:04
  • Try a build clean and rerunning qmake from the Build menu. – phyatt Mar 11 '15 at 06:01
  • Aside from everything, you really don't need `m_parent`. Just use `QObject::parent()`. I think you should read the full `QObject`, `QWidget` and `QFrame` api to get an idea of what's there. Yeah, I know, it's a crazy notion, but you do need to actually read through the whole thing, start to finish. How else will you know of any functionality that might be there? – Kuba hasn't forgotten Monica Mar 11 '15 at 16:19
  • @KubaOber the m_parent comes from an example I read online, but even if I remove it I still get that error. – dominicbri7 Mar 11 '15 at 19:55
  • All i want is to make a very simple class that extends QFrame, everything I try fails – dominicbri7 Mar 11 '15 at 19:55
  • 1
    Correct solution is to re-run qmake, then do a clean build (from Qt Creator build menu for example). If it does not help, make sure both .cpp and .h file are listed in the .pro file for qmake. – hyde Mar 11 '15 at 20:34
  • @hyde not correct. I tried it doesn't work, it is already listed in .pro file. Clean build doesn't fix anything – dominicbri7 Mar 12 '15 at 02:31
  • @dominicbri7 Then probable reason is, you did build in source dir (either from command line or from Qt Creator with shadow build checkbox unchecked). Then you switched to doing shadow builds. In this case, you have stale moc_*.* (and possibly ui_*.h) files in your source dir. Remove them, then try building again. – hyde Mar 12 '15 at 07:08

1 Answers1

-2

I think if you remove the Q_OBJECT from your .h it will be fine. I'm not sure, but because you inherit from QFrame you already have the Q_OBJECT in your object.

simperreault
  • 896
  • 1
  • 7
  • 17
  • 1
    Thank you my friend, you don't deserve like this, you don't deserve for rekt - Pashabiceps – dominicbri7 Mar 11 '15 at 20:29
  • 1
    @dominicbri7 Actually this is wrong. You should only omit `Q_OBJECT` if you can *clearly* state why. Usually it is a bad idea. – hyde Mar 11 '15 at 20:31
  • hyde is right. Generally speaking, anything that derives from `QObject` should have a `Q_OBJECT` macro. Any workarounds are because of not understanding the build system. This **answer is bad advice**. – Kuba hasn't forgotten Monica Mar 11 '15 at 20:47
  • this is the ONLY answer that fixed my problem. I don't have a compilation error anymore. Q_OBJECT macro works in my classes extending QObject, but this class extending QFrame doesn't work with Q_OBJECT, it gives me the compilation error mentioned in the question. – dominicbri7 Mar 12 '15 at 02:29
  • TO me it seems the included classes already have Q_OBJECT and using it a second time messes up the vtable (which is what it seems in the error log). WITH Q_OBJECT my build fails (yes I tried the clean rebuild) WITHOUT Q_OBJECT everything works – dominicbri7 Mar 12 '15 at 02:36