PDA

View Full Version : Failed to compiled QT4 with Q_OBJECT when include header file



teleplan
26th August 2015, 08:52
Hi, I have spend 1 week try to resolve the problem but still failed. Would like to seek you guy help on below error.

I had build a simple form with feature for signal/slots. So Q_OBJECT is a must. In my header file myQtApp.h, I try to include header file "scsicmd.h" & "atacmd.h" (link to static library) it failed to compiled. Below is error message and code and project file. Hope somebody can guide me. Thanks you very much.

root@udoobuntu: /home/ubuntu/firstGUI $ qmake
root@udoobuntu: /home/ubuntu/firstGUI $ make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mk
specs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o myqtapp.o myqtapp.cpp
In file included from myqtapp.cpp:7:0:
myqtapp.h:20:5: error: expected nested-name-specifier before numeric constant
myqtapp.h:20:5: error: expected '>' before numeric constant
myqtapp.h:20:5: error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]
myqtapp.h:20:5: error: expected ',' or '...' before numeric constant
myqtapp.h: In member function 'void myQtApp::qt_check_for_QOBJECT_macro(int) const':
myqtapp.h:20:5: error: '_q_argument' was not declared in this scope
myqtapp.cpp: At global scope:
myqtapp.cpp:11:1: warning: unused parameter 'parent' [-Wunused-parameter]
make: *** [myqtapp.o] Error 1
root@udoobuntu: /home/ubuntu/firstGUI $


myqtapp.h code:

#ifndef MYQTAPP_H
#define MYQTAPP_H

#include "scsicmd.h" //SCSI Command Library (static) <--- error failed to compiled
#include "atacmd.h" //ATA Command Library (static) <--- error failed to compiled
#include "ui_myqtapp.h"

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fstream>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <scsi/sg.h>

class myQtApp : public QWidget, private Ui::myQtAppDLG
{
Q_OBJECT //<--- all error point to this line 20 !!!

public:
myQtApp(QWidget *parent = 0);
~myQtApp();
sg_io_hdr_t io_hdr; //SCSI Generic Structure
SCSICmd *SCSICommand;
ATACmd *ATACommand;
int sg_dev;


public slots:
void getPath();
void doSomething();
void clear();
void about();

void bufferdump(char *d,unsigned int);
void ByteSwapAll(unsigned char *buffer, int size);
DWORD dwordReverse (DWORD dword);
WORD wordReverse (WORD word);
void openlib();
void openDevice(int dev=0);


protected:
u_char dataBuffer[512*512];
u_char senseBuffer[64];
u_char errorBuffer[64];
DWORD tx_len;
char tmp[10];
char model[22];
char vendor[8];
char fw[8];
char serial[20];
BYTE byReadCap [8];
UINT blocksize;
UINT dwMaxLBA;
UINT capacity;
};


#endif


myqtapp.pro file:

HEADERS = atacmd.h scsicmd.h myqtapp.h
SOURCES = myqtapp.cpp main.cpp
FORMS = myqtapp.ui
LIBS = /usr/local/lib/libATACmd.a /usr/local/lib/libSCSICmd.a

# install
target.path = myqtapp
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS *.pro
sources.path = .
INSTALLS += target sources

d_stranz
26th August 2015, 21:37
Your class is derived from QWidget. Where is your #include <QWidget> (which will at some point include the header where Q_OBJECT is defined)?

teleplan
27th August 2015, 02:32
Hi, Thanks for your reply. This program originally is working good without any problem. I already included QTGui in myqtapp.cpp file.
The problem occur once I add in the static library with 2 include header file [scsicmd.h] & [atacmd.h] then it failed to compiled and all error point to Q_OBJECT line.

In myqtapp.cpp which I not posted here, I already include
#include <QtGui>
#include "myqtapp.h"

anda_skoa
27th August 2015, 20:07
What if you just separate UI and processing into to classes?
A sensible thing to do in any case and could totally avoid the problem you are seeing.

Cheers,
_

d_stranz
28th August 2015, 00:22
Possibly the problem is the private inheritance of Ui::myQtAppDLG. Try changing it to public or protected. The Q_OBJECT macro inserts some Qt meta-object source code into the class definition, and maybe this is incompatible with private inheritance of the UI class.

And take anda_skoa's suggestion for separating UI and processing code into different classes. It is never a good design to mix the two up. Makes for bad spaghetti.

teleplan
28th August 2015, 09:14
What if you just separate UI and processing into to classes?
A sensible thing to do in any case and could totally avoid the problem you are seeing.

Cheers,
_


Hi anda_skoa & d_stranz

I new with QT GUI, I using QT designer to generate a form. Then I use normal editor to write a source file. Original I use a sample code from internet which is running well. Once I add in my code [Not touch on any QT code] then it compiled error.

I not sure how to separate the UI, can you provide me guideline/example based on my above sample code? I had stuck with this many weeks.. really hope can settle it soon. bcoz backend I still need to do lots low level hardisk drive code.

I just need a simple GUI with few button, the button will send some hard disk command to retrieve some information. :-)

Thanks again.


Possibly the problem is the private inheritance of Ui::myQtAppDLG. Try changing it to public or protected. The Q_OBJECT macro inserts some Qt meta-object source code into the class definition, and maybe this is incompatible with private inheritance of the UI class.

And take anda_skoa's suggestion for separating UI and processing code into different classes. It is never a good design to mix the two up. Makes for bad spaghetti.

I had try this, unfortunately still same error.

anda_skoa
28th August 2015, 16:43
I not sure how to separate the UI, can you provide me guideline/example based on my above sample code?

You put all the non-UI code into a separate class and let the widget hold an instance of that class as a pointer.
That way you do not need any of these other includes in the header that has the Q_OBJECT.

Cheers,
_

teleplan
3rd September 2015, 02:11
You put all the non-UI code into a separate class and let the widget hold an instance of that class as a pointer.
That way you do not need any of these other includes in the header that has the Q_OBJECT.

Cheers,
_

Hi anda_skoa,

Yes, it compiled now, I separate the non-UI code into another class.
Really not understand, why my previous QT3 can compiled without any problem which include non-UI code.

Anyway thanks for help...