Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, July 2007
Re: [Qt4.3] Compile Error on Q_OBJECT in grandchild?


Message 1 in thread

Hi,

> This was hard to track down, because the qobject_cast<> in another file triggered the compile error in the "MyC" header.

What was the complete error message? Didn't it refer to the other file at all?

--
 [ signature omitted ] 

Message 2 in thread

Using sender() in this way is absolutely ok. (and no, you don't need to
do it the ugly reinterpret_cast-way)

again, using Qt 4.3.0 and MSVC 2003 this code compiles fine:

---------------------------
#include <QtGui>
class MyA : public QWidget {
  Q_OBJECT
// ...other stuff
};
class MyB : public MyA {
  Q_OBJECT    // doesn't matter if here or not
// ...other stuff
};
class MyC : public MyB {
  Q_OBJECT   // <--- COMPILE ERROR
// ...other stuff
};
class SomeClass : public QWidget {
	Q_OBJECT
public slots:
	void myslot_doSomething(const QAbstractButton& /*button*/);
};
void SomeClass::myslot_doSomething(const QAbstractButton& /*button*/)
{
	// ...TRIGGERS COMPILE ERROR IN "MyC.hpp" IN 4.3,
	// COMPILES AND RUNS FINE IN 4.2...
	MyC* myc_sender = qobject_cast<MyC*>(sender());
}
---------------------------

i suggest you create a minimal, compilable example that shows the error.

Cheers,
Peter

Charley wrote:
> Ok, strange -- I've found the trigger for the compile error (see
previous
> message below for context):
> 
> void SomeClass::myslot_doSomething(const QAbstractButton& /*button*/)
> {
>   // ...TRIGGERS COMPILE ERROR IN "MyC.hpp" IN 4.3,
>   // COMPILES AND RUNS FINE IN 4.2...
>   MyC* myc_sender = qobject_cast<MyC*>(sender());
>   // ...do stuff
> }
> 
> Now the previous thread makes sense:  Qt4.3 is apparently a bit more
> strict regarding qobject_cast<>.  However, I expected it to work
because
> it's single inheritance from QObject (through QWidget), and I have a
> Q_OBJECT in each header all-the-way down the hierarchy.
> 
> This was hard to track down, because the qobject_cast<> in another
file
> triggered the compile error in the "MyC" header.
> 
> (I understand using "sender()" isn't very graceful, but it seemed to
be
> useful in this case, and I found the code snippet in a Qt example --
is
> this use deprecated in Qt4.3?)
> 
> Looking into QObject.h, it appears the trolls in Qt4.3 use a syntax
closer
> to the following:
> 
>     reinterpret_cast<T>(0)-
> >qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
> 
> What's the appropriate way in Qt4.3 to get the user-type'd "sender()"
> within a slot?
> 
> Thanks!
> 
> --charley

--
 [ signature omitted ]