Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to emit signal inside constructor
Forum Updated to NodeBB v4.3 + New Features

how to emit signal inside constructor

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 502 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by
    #1

    How to emit signal inside constructor?
    I have class that try to find files and analyse its. After last file done my class should emit mysignal(). If there is no any files I want too emit mysignal() in constructor...
    This is wrong way

    MyClass::MyClass(QObject *parent) : QObject(parent){
        emit mysignal(); //Emitting inside constructor probably has no effect [clazy-incorrect-emit]
    }
    

    I have idea. Using of QTimer::singleShot() is a good practice or dirty hack?

    MyClass::MyClass(QObject *parent) : QObject(parent){
        QTimer::singleShot(0, this, [&]{ emit finished(); });
    }
    
    JonBJ 1 Reply Last reply
    0
    • D DungeonLords

      How to emit signal inside constructor?
      I have class that try to find files and analyse its. After last file done my class should emit mysignal(). If there is no any files I want too emit mysignal() in constructor...
      This is wrong way

      MyClass::MyClass(QObject *parent) : QObject(parent){
          emit mysignal(); //Emitting inside constructor probably has no effect [clazy-incorrect-emit]
      }
      

      I have idea. Using of QTimer::singleShot() is a good practice or dirty hack?

      MyClass::MyClass(QObject *parent) : QObject(parent){
          QTimer::singleShot(0, this, [&]{ emit finished(); });
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @DungeonLords
      As https://stackoverflow.com/questions/41220300/why-emit-signal-in-constructor-doesnt-work says, you won't have connected the signal while still inside the constructor so how should that work?

      You can use your "timer delay" to work around it if you want. Won't be emitted till next time event loop is hit.

      I have class that try to find files and analyse its.

      Maybe that's too much work to put in a constructor? You could avoid problems and timer if you move the finding/analyzing out of constructor and into some callable method?

      1 Reply Last reply
      3
      • D DungeonLords has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved