QThread base class not appearing - qthread

I've installed Qt 5.7 on Windows (8.1). I'm not getting QThread option (as a base class) while I try to add a new class to a project? What could be the reason?

If you're using Wizard to create your class, that's normal: just select, i.e., QObject, create your class and in the sources replace QObject with QThread as base class. Don't forget to include QThread class previously (#include <QThread>)

Related

QT QML + C++ MVVM

As far as I know in QT QML is not possible to instantiate a C++ class in QML Component, unless it is a QQuickItem.
I would not like to put all ViewModels on ViewEngine context because it is a very bad pratice create all classes in memory without using.
My question is: How can I instantiate a C++ ViewModel ou Services/API from a single QML component without usnig ViewEngine Context.
Do ViewModels have to be QQuickItem type?
C++ objects do not need to be QQuickItems, they need to be QObjects. You just need to register your class with the QQmlEngine, like this:
qmlRegisterType<MyObject>("my.component.library", 1, 0, "MyObject");
Then in your qml files, you can instantiate that class like this:
import my.component.library 1.0
MyObject {
...
}

How to make a class in Qt both scriptable and serializable?

I'm trying to write a class with two basic characteristics:
It needs to be scriptable - the class contains a number of properties and methods decorated with Q_INVOKABLE that are exposed to scripts.
It needs to be serializable so that it can be registered with qRegisterMetaTypeStreamOperators() for storing in QVariants.
As far as I can tell, I need to derive from QObject in order to make the class scriptable. However, in order to register the class with qRegisterMetaTypeStreamOperators(), it seems like the class needs to have a default copy constructor - something a QObject-derived class cannot have.
Is there any way to achieve both goals?
You can have scriptable objects not derived from QObject but it's more work. It's discussed here

Qt: bring base class method into slots in derived class

I have a class which derives from a QWidget and a model class. Based on my reading, I can't have the model class to derive from QObject.
class PageWidget : public QWidget,
public MyModelClass
{
...
};
the model class MyModelClass already have method to set properties such as setWidth(bool). It seems that I can't use those method directly as slots. If I declare:
QObject::connect(button, SIGNAl(dataChanged(bool)), this, SLOT(setWidth(bool)));
Qt complains at runtime that no slot setWidth(bool). I have to manually add each method into PageWidget, which simply calls the same method of MyModelClass.
Is there any way to bring base methods into slots without redeclaring every method?
QMetaObject (the Qt part that allow you to use slots) and multiple inheritance do not mix. You solution of creating "pass through" slots is a way of solving it. However, if you do not need to do the multiple inheritance, I wouldn't do it. Just from the look of things, it seems weird for a PageWidget to derive from both a Widget and a Model. It probably makes more sense to have it contain the model instead.

How to set properties to an object in a scene in Qt

I have created an Scene in Qt using QGraphicsScene. And I have placed many objects in the scene like car , bus , etc using QGraphicsPixmapItem. Now I want to assign some properties to
these objects that I placed so that i can make them run. How do I do that . For example I want to associate each object with my custom class objects . Can anyone help me . I also noticed setData function which associate key to value . Can this be used if so how . What's the standard way of doing this.
If its possible to and you want to use Qt's property system, inherit from QObject as well as the QGraphicsItem. Make sure to also define the Q_OBJECT macro.
class CustomPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
...

Is it important to declare the namespace with QT_BEGIN_NAMESPACE .. QT_END_NAMESPACE

What is the effect of QT_BEGIN_NAMESPACE?
QT_BEGIN_NAMESPACE
class QLineEdit;
QT_END_NAMESPACE
If I don't use the class declaration like that, will any problem occur?
QT_BEGIN_NAMESPACE and QT_END_NAMESPACE are preprocessor macros and they are defined in QtGlobal as:
#define QT_BEGIN_NAMESPACE namespace QT_NAMESPACE {
#define QT_END_NAMESPACE }
But: those two macros only have any effect when Qt was configured and built using the -qtnamespace option, which allows you to build Qt inside a user-defined namespace.
So: if you don't use (or plan on using) a Qt library which was built this way (per default, the Qt library resides in the global namespace), omitting the QT_BEGIN_NAMESPACE and QT_END_NAMESPACE won't lead to any problems.
if you do not use them, it may crash in certain scenarios.
The Qt-in-Namespace feature serves as a tool to handle certain
scenarios involving multiple configurations of Qt more gracefully.
E.g. before Qt-in-Namespace a Qt 3 based application in Linux would
immediately segfault as soon as it tries to dlopen() a shared object
that is linked to Qt 4. With the Qt in namespace feature not only can
the crash be prevented, but under certain circumstances this
combination might actually "work".
source

Resources