Skip to content
  • 0 Votes
    5 Posts
    2k Views
    kshegunovK

    @Dariusz said in Move EVERY QObject to main thread and more...:

    Print every QObject in-app.

    No. You can get all the children for a given root QObject though: object->findChildren<QObject *>()

    Get each object thread

    QObject::thread() for the object's associated thread (i.e. depending on its affinity), QThread::currentThread() for the thread object associated with the current context (and yes, these can differ).

    Move object to the main thread.

    Only from the thread the QObject belongs to, no other. QObject instances can only be "pushed" into another thread from their own thread, but not "pulled" from their thread from another.
    To move the object, you do what you usually do:

    object->moveToThread(QCoreApplication::instance()->thread());
  • 0 Votes
    9 Posts
    986 Views
    SGaistS

    QObject has no event loop, the event loop is in QCoreApplication and siblings.

    You can not just stop the one from you main application. As for QThread you can interrupt the thread but I'm really not sure that's the best way to handle your situation.

    From the looks of it, you seem to have to create a queue of the events received and store these there while in pause mode and then empty that queue on restart if that makes sense.

  • 0 Votes
    8 Posts
    2k Views
    dheerendraD

    @Yash001 You can three classes & inheritance mechanism as you mentioned. I'm keeping aside the topic of why not to inherit from QThread.

  • 0 Votes
    1 Posts
    388 Views
    No one has replied
  • 0 Votes
    5 Posts
    3k Views
    SGaistS

    @oblivioncth said in std::unique_ptr with QObject derived class?:

    Won't let me mark your post as the answer for some reason. Sorry.

    Done for you :)

  • 0 Votes
    2 Posts
    634 Views
    SGaistS

    Hi,

    Why do you have to remove these events ?

  • 0 Votes
    5 Posts
    1k Views
    JoeCFDJ

    @kengineer said in C++ Enums in QML with setContextProperty:

    MyCustomQMLClass myclass;
    view.engine()->rootContext()->setContextProperty("myQmlClassInstance", &myclass)

    you can use context as well. But it is better to define it with pointer.
    auto myclass = new MyCustomQMLClass;
    view.engine()->rootContext()->setContextProperty("myQmlClassInstance", myclass);
    if not with a pointer, myclass is gone if it is set in a func.
    You also need to clear it when it is not needed.

  • 0 Votes
    2 Posts
    603 Views
    devDawgD

    Problem solved. I changed what I was getting with my Q_INVOKABLE method inside of the QML document.

    Instead of doing this for my ListView delegate:

    ListView { . . . model: dataModel.size delegate: Component { Text{ text: dataModel.getTextBodyAt(index) } } }

    I'm now doing this:

    ListView { . . . model: dataModel.size delegate: Component { Text{ text: dataModel.getModuleAt(index).TextBody } } }

    So in essence, instead of getting DataModule's property using a getTextBodyAt(i) method from DataModel, I used getDataModuleAt(i), which gave me direct access to that member's property.

    Hopefully this helps others as much as it helped me.

  • 0 Votes
    8 Posts
    2k Views
    raven-worxR

    @philm001
    the stylesheet declaration should rather look like this i guess:

    w->setStyleSheet(QString("QListView::item { background: rgba(255, 0, 0, %1)}").arg(value.toInt())
  • 0 Votes
    3 Posts
    4k Views
    VRoninV
    void MainWindow::doThat() { // private: int m_numDestroyed; m_numDestroyed = 0; for(QTimer*& singleTim : {timerA ,timerB ,timerC }){ singleTim = new QTimer(this); singleTim ->setInterval(100); singleTim->setSingleShot(true); } const auto checkDestroyed=[=]()->void{ if(++m_numDestroyed>1){ delete numDestroyed; timerC->start(); }; connect(timerA,&QTimer::timeout,this,[=]()->void{ qDebug("slot A"); checkDestroyed(); } }); connect(timerB,&QTimer::timeout,this,[=]()->void{ qDebug("slot B"); checkDestroyed(); } }); connect(timerC,&QTimer::timeout,this,[=]()->void{ qDebug("slot C"); } }); connect(timerA,&QTimer::timeout,timerA,&QTimer::deleteLater); connect(timerB,&QTimer::timeout,timerB,&QTimer::deleteLater); timerA->start(); timerB->start(); }
  • 0 Votes
    14 Posts
    9k Views
    VRoninV

    I heard opposing arguments to that when it comes to moc/rcc/uic. Personally, I use the set(CMAKE_AUTOMOC ON) way

  • 0 Votes
    5 Posts
    4k Views
    C

    Adding the QObject constructor to my class's constructor and adding the pointer-to-member syntax suggested by Kshegunov fixed the issue.

    Thanks guys and good day.

  • 0 Votes
    11 Posts
    8k Views
    D

    @yuvaram

    If so, compile it with Network Download Example, which is officially available from Qt.

  • 0 Votes
    2 Posts
    2k Views
    romsharkovR

    After hours of google, trial and error I'm proud to finally have solved it myself as I am still pretty new to Qt!

    To help people with a similar problem I'll quickly sum up all I had to do to make it work:

    turn the internal QList<Message> into a QList<QSharedPointer<Message>>

    define a QMap<QString, QSharedPointer<Message>> to hold index ids to the individual messages

    when inserting, insert in the indexMap as well

    define a method in the QAbstractListModel derived MessageList which simply returns a QVariantMap

    P.S. Below is the new working version of the example app, in case anyone notices any misconceptions, please let me know

    MessageList.hpp:

    #ifndef MESSAGELIST_HPP #define MESSAGELIST_HPP #include "Message.hpp" #include <QObject> #include <QAbstractListModel> #include <QModelIndex> #include <QList> #include <QHash> #include <QVariant> #include <QMap> #include <QSharedPointer> class MessageList : public QAbstractListModel { Q_OBJECT public: typedef QSharedPointer<Message> MessagePointer; typedef QList<MessagePointer> MessagePointerList; typedef QMap<QString, MessagePointer> IndexMap; enum Roles { IdentifierRole, TitleRole, MessageRole, }; protected: MessagePointerList _list; IndexMap _indexMap; public: MessageList(); int rowCount(const QModelIndex& parent) const; QHash<int, QByteArray> roleNames() const; QVariant data(const QModelIndex& index, int role) const; bool insert( const QList<Message>& messages, int position = 0 ); bool reset(); Q_INVOKABLE QVariantMap get(const QString& identifier) const; const Message& at(int index) const; }; #endif // MESSAGELIST_HPP

    MessageList.cpp:

    #include "MessageList.hpp" #include <QObject> #include <QModelIndex> #include <QVariant> #include <QHash> #include <QByteArray> MessageList::MessageList() : QAbstractListModel(nullptr) { } int MessageList::rowCount(const QModelIndex& parent) const { Q_UNUSED(parent) return _list.size(); } QHash<int, QByteArray> MessageList::roleNames() const { QHash<int, QByteArray> roles; roles[IdentifierRole] = "identifier"; roles[TitleRole] = "title"; roles[MessageRole] = "message"; return roles; } QVariant MessageList::data(const QModelIndex& index, int role) const { if(!index.isValid() || index.row() >= _list.size() || index.row() < 0 ) { return QVariant(); } switch(role) { case IdentifierRole: return _list.at(index.row())->identifier(); break; case TitleRole: return _list.at(index.row())->title(); break; case MessageRole: return _list.at(index.row())->message(); break; default: return QVariant(); } } bool MessageList::insert( const QList<Message>& messages, int position ) { beginInsertRows(QModelIndex(), position, position + messages.size() - 1); for(int row = 0; row < messages.size(); ++row) { IndexMap::const_iterator indexMapItr(_indexMap.constFind(messages.at(row).identifier())); if(indexMapItr == _indexMap.constEnd()) { MessagePointer newMessage(new Message(messages.at(row))); _list.insert(position, newMessage); _indexMap.insert(messages.at(row).identifier(), newMessage); } } endInsertRows(); return true; } bool MessageList::reset() { beginResetModel(); _list.clear(); __indexMap.clear(); endResetModel(); return true; } QVariantMap MessageList::get(const QString& identifier) const { QVariantMap result; IndexMap::const_iterator indexMapItr(_indexMap.constFind(identifier)); if(indexMapItr != _indexMap.constEnd()) { result["identifier"] = QVariant(indexMapItr->data()->identifier()); result["title"] = QVariant(indexMapItr->data()->title()); result["message"] = QVariant(indexMapItr->data()->message()); } return result; } const Message& MessageList::at(int index) const { return *(_list.at(index).data()); }

    main.cpp:

    #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "Message.hpp" #include "MessageList.hpp" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); MessageList messageList; messageList.insert(QList<Message> { Message("a", "first message", "this is a sample text message of the first message"), Message("b", "second message", "another sample text message of the second message"), Message("c", "third message", "yet a third text message sample"), Message("d", "fouth message", "last sample message") }); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("MessageList", &messageList); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }

    main.qml:

    import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Messages") Rectangle { id: menu color: Qt.rgba(0.9, 0.9, 0.9, 1) height: 32 anchors.top: parent.top anchors.left: parent.left anchors.right: parent.right Text { anchors.left: parent.left anchors.margins: 8 anchors.verticalCenter: parent.verticalCenter text: "at id 'b' there is: " + MessageList.get("b").title } } ListView { model: MessageList anchors.top: menu.bottom anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom clip: true delegate: Rectangle { height: 32 Column { anchors.fill: parent anchors.margins: 8 spacing: 2 Text { text: title + " (" + identifier + ")" font.bold: true } Text { text: message } } } } }
  • 0 Votes
    15 Posts
    14k Views
    DongD

    @kshegunov
    There is a reason why I can't use QT Property Binding (It only support predefined Property using Q_PROPERTY script).
    Please take a look at my topic about Auto Binding 2 ways with Dynamic Property
    But Binding is an other topic.

    About "Object's pointers & Memory"
    I also take an advise from a Pro in C++ & memory management.
    His solution is implement a Factory Pattern to Create/Delete Object's pointers
    (also free memory when an instance had no pointer to it.)
    (I think I'll give it a try)

  • 0 Votes
    2 Posts
    1k Views
    kshegunovK

    @Jakob
    Switch the order of the construction of objects. When the parent's destructor runs it'll try to free its children, but since you've created it after the child (hence its destructor will be called first), it will try to delete a stack object.

  • 0 Votes
    5 Posts
    9k Views
    SandersS

    I finally found my issue. My include path did not point to the great folder (the one containing the QObject header)

    To fix it I used the include path flag with the ndk-build command

  • 0 Votes
    10 Posts
    3k Views
    SGaistS

    @kshegunov Thanks :)

    @Basti46 Remove MainWindow from SerialPort, it doesn't belong there. In MainWindow connect all the signals you'll be emitting from SerialPort in order to trigger a GUI update.

  • 0 Votes
    11 Posts
    3k Views
    S

    @jsulm Thanks ! It's clear !

    Yes, sorry for the lack of clarity, but it's as hard to explain for me as it's hard to understand for you :) By the way, thank you for your help, and your patience !

    Have a nice day