Questions tagged [qobject]

QObject is a Qt class which serves as a base class for all Qt objects.

QObject is the center of Qt object model. QObjects are organized as trees, where one objects can become parents to others, taking ownership for them, and being responsible for freeing heir children's resources on deletion.

Each QObject has a name, which can be accessed. It also provides a way to search in, or to iterate through it's children. Objects can also receive and filter events.

When deriving a class from QObject, one must remember to include a special Q_OBJECT macro in a private section of the class, which is a mandatory for every class that uses signals and slots, or any other features of Qt's meta-object system.

A QObject instance is said to have a thread affinity, or that it lives in a certain thread. When a QObject receives a queued signal or a posted event, the slot or event handler will run in the thread that the object lives in.

Official documentation of QObject can be found here.

410 questions
6
votes
3 answers

Qt, CMake, Visual Studio and Q_OBJECT in cpp files

I'm developing a large project using Qt 4.6, CMake 2.8 and Visual Studio 2008 for the Windows platform. As far the build system goes, it's all standard stuff: I'm using CMake's QT4_WRAP_CPP macro to generate moc files from header files, which are…
François Beaune
  • 4,270
  • 7
  • 41
  • 65
6
votes
2 answers

Inheriting constructor from QObject based class

I have a class called MiscData that inherits QObject and has a member variable (a model). And then bunch of other classes that inherit MiscData and reimplement its virtual function to populate the model. So it looks like this: class MiscData :…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
6
votes
1 answer

Why QObject needs to be the first in case of multiple inheritance

According to http://qt-project.org/doc/qt-4.8/moc.html#multiple-inheritance-requires-qobject-to-be-first the QObject must be the first in the base classes when using multiple inheritance. Is this because of some limitation in the moc tool or C++…
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
6
votes
1 answer

Checking Q_OBJECT macro with static_assert

I have some interesting need in showing a compilation error if the declaration of the type I was given doesn't contain the Q_OBJECT macro. I found one bad way to do it. Actually it repeats the idea of Qt developers to do the same…
dvvrd
  • 1,679
  • 11
  • 20
6
votes
3 answers

Get objectname (as seen from Qt Designer) from QWidget?

I want to disable all but a selected set of widgets in my Qt application. What I am trying to do is to iterate all children of mainWindow using findChildren and disable all the resulting widgets except 'myTable' using setEnabled(false).…
fortytwo
  • 491
  • 1
  • 5
  • 16
6
votes
5 answers

Order of QObject children (strategy question)

For one of my projects I have a tree of QObject derived objects, which utilize QObject's parent/child functionality to build the tree. This is very useful, since I make use of signals and slots, use Qt's guarded pointers and expect parent objects to…
BastiBen
  • 19,679
  • 11
  • 56
  • 86
6
votes
2 answers

QML: Get QObject parent

QML introduces a separate ownership (QObject) tree from the visual tree (QtQuick scene graph). parent returns the visual parent. children returns the visual children. data returns the QObject children. But how do I access the QObject parent? FYI,…
cmannett85
  • 21,725
  • 8
  • 76
  • 119
6
votes
1 answer

Any chance to use non QObject classes with QML

As of Exposing Attributes of C++ Types to QML classes used with QML have to be QObjects. Any chance I can use non QObjects s (aka POCO, not derived from QObject, but registered with Qt metasystem)? If not, is there a simple generic wrapping system…
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
6
votes
1 answer

Properties undefined only when accessed through script

I'm running into some strange behaviour where a property can be accessed directly through QObject's property function, but not through JavaScript: #include #include #include #include class Item…
Mitch
  • 23,716
  • 9
  • 83
  • 122
6
votes
0 answers

How big is QObject?

I was curious how big QObject actually is, including the typical private data each instance creates dynamically. I couldn't get a sizeof for those, because of the way they are implemented. EDIT: NOTE that I am asking about the size of a typical…
dtech
  • 47,916
  • 17
  • 112
  • 190
6
votes
2 answers

In Qt, how do I use Q_OBJECT slots and signals with multiple inheritance?

I looked through the related questions and couldn't find anything that addresses exactly what I was talking about, so let me describe. I have a class, let's say foo that needs to have its own slots and signals, but also needs to inherit from…
San Jacinto
  • 8,774
  • 5
  • 43
  • 58
6
votes
2 answers

QObject based class has a queued connection to itself

I was digging into some source code I am working on. I found a peculiar statement that someone had coded. The source code is a GUI application with a QML GUI and uses QT 4.7.x. The snippet below belongs to core application logic. // connect…
Vikas Bhargava
  • 691
  • 1
  • 9
  • 22
5
votes
1 answer

Why can't I set a QObject parent in a class of which QObject is only an indirect base?

I have a class BatchItem that inherits QObject, plus several classes that inherit from BatchItem: #ifndef BATCHITEM_H #define BATCHITEM_H #include class BatchItem : public QObject { Q_OBJECT public: virtual void start() = 0; …
steps
  • 618
  • 1
  • 7
  • 18
5
votes
1 answer

Qt: Q_PROPERTY with pointer and forward declaration for QtScript access

Problem I am making a project using Q_OBJECT and Q_PROPERTY to access some objects from scripts. I have two problems: making classes that use forward declarations scriptable returning a property as pointer Explanations 1. Why forward…
opatut
  • 6,708
  • 5
  • 32
  • 37
5
votes
1 answer

Is `moveToThread(nullptr)` a valid way to pull a QObject within the destination thread from its source thread?

Suppose if an object obj belongs to a QThread T1. Ideally being in Qhread T2's function, obj can't be 'pulled' from T1 to T2. This is mentioned in moveToThread() documentation: Warning: This function is not thread-safe; the current thread must be…
iammilind
  • 68,093
  • 33
  • 169
  • 336