qobject

C++ Qt Reflection with Copy and Assignment

一曲冷凌霜 提交于 2019-12-23 09:05:28
问题 As the QObject documentation and many others explain, a QObject has an identity and thus hides its copy constructor and assignment operator. However, I'm not deriving from QObject for its dynamic properties feature or the signals/slots feature. I only want reflection , or the ability to access Foo::staticMetaObject . class Foo : public QObject { Q_OBJECT Q_ENUMS(Color) public: enum Color { Blue, Red, Pink }; private: Color color; }; Q_DECLARE_METATYPE(Foo::Color) I then can't copy Foo with:

connecting signal/slot across different threads between QObjects

天大地大妈咪最大 提交于 2019-12-23 03:57:13
问题 I wanted to know what is the best practice to connect signal/slots between two QObjects created in the contructor of MainWindow but moved to different threads later...default connections seems not working then when I connect with the option Qt::Directconnection things start working...but sometimes the signal/slot fails...following is my code pattern..please let me know if I need to change my class design... MainWindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), {

connecting signal/slot across different threads between QObjects

流过昼夜 提交于 2019-12-23 03:57:06
问题 I wanted to know what is the best practice to connect signal/slots between two QObjects created in the contructor of MainWindow but moved to different threads later...default connections seems not working then when I connect with the option Qt::Directconnection things start working...but sometimes the signal/slot fails...following is my code pattern..please let me know if I need to change my class design... MainWindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), {

Do QObject derived types need a parent QObject?

淺唱寂寞╮ 提交于 2019-12-22 07:52:27
问题 I am writing some Qt class which is derived from QObject , it looks like: class A : public QObject { Q_OBJECT public: A() : QObject() {} ..... } but in several places I saw, that the QObject derived classes all have a parent, like: class A : public QObject { Q_OBJECT public: A(QObject* parent = 0) : QObject(parent) {} ..... } So the question is: do I need a parent or not? What is the difference if I have one, if I have a default one (0) or I don't have at all? 回答1: As such you don't need a

Do QObject derived types need a parent QObject?

爱⌒轻易说出口 提交于 2019-12-22 07:52:16
问题 I am writing some Qt class which is derived from QObject , it looks like: class A : public QObject { Q_OBJECT public: A() : QObject() {} ..... } but in several places I saw, that the QObject derived classes all have a parent, like: class A : public QObject { Q_OBJECT public: A(QObject* parent = 0) : QObject(parent) {} ..... } So the question is: do I need a parent or not? What is the difference if I have one, if I have a default one (0) or I don't have at all? 回答1: As such you don't need a

QSharedPointer and QObject::deleteLater

旧时模样 提交于 2019-12-22 04:13:07
问题 I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just call QObject::deleteLater on the object, however this isn't possible with a QSharedPointer -managed instance. My workaround is the following: template<typename T> class QSharedPointerContainer : public QObject { QSharedPointer<T> m_pSharedObj;

QSharedPointer and QObject::deleteLater

◇◆丶佛笑我妖孽 提交于 2019-12-22 04:13:04
问题 I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just call QObject::deleteLater on the object, however this isn't possible with a QSharedPointer -managed instance. My workaround is the following: template<typename T> class QSharedPointerContainer : public QObject { QSharedPointer<T> m_pSharedObj;

Add QObject in the combo box of Qt

自闭症网瘾萝莉.ら 提交于 2019-12-22 00:18:04
问题 I have a custom class I created, say MyClass. Now how to add a reference to MyClass's reference as second parameter in the combo box below: this->ui->comboBox->addItem("item-1", ); Purpose is to when item changed even is fired, i want to get that specific class instance of MyClass and process accordingly. 回答1: First you need to use Q_DECLARE_METATYPE(MyClass*), so that the type can be used in QVariant . Then you can add the item like this: this->ui->comboBox->addItem("item-1", QVariant:

Proper way to check QObject derived class type in Qt

心不动则不痛 提交于 2019-12-21 03:47:41
问题 Lets say I have a two classes: class A : public QObject {}; class B : public QObject {}; then I go QObject *a = new A(); QObject *b = new B(); now, how do I make sure that "a" is an instance of class A, and "b" is an instance of class B? currently I do something like this: if (a->inherits(A::staticMetaObject.className())) { ... } else if (a->inherits(A::staticMetaObject.className())) { ... is there a better way? 回答1: You can use qobject_cast<MyClass*>(instance) on QObject derived classes and

How do I copy object in Qt?

情到浓时终转凉″ 提交于 2019-12-17 06:51:36
问题 I'm using Qt and have some real basic problems. I have created my own widget MyTest that have a variable obj . I need to set this variable obj from an object outside of the widget so that the variable is copied not just a pointer to another object. I get an error message and can't figure out how to do this basic stuff. This is the code I'm using: MyTest.h: class MyTest : public QWidget { Q_OBJECT public: void setObj(QObject &inobj); QObject obj; .... } MyTest.cpp: void MyTest::setObj(QObject