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. Can I emit signal from a public function?
Qt 6.11 is out! See what's new in the release blog

Can I emit signal from a public function?

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 6.9k 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.
  • H Offline
    H Offline
    heatblazer
    wrote on last edited by
    #4

    Oh, I did... OK, that fixed the qtcreator error report. But still leaves me the question - consider this code:
    @
    /* somewherer in a separate main() */
    startTime = endTime = time(NULL);
    int qtstarted = initQtApp(spl, argc, argv);

    while ( 1 ) {
        sleep(1);
        if ( endTime - startTime > (5) ) {
            char buff[512]="";
            spl->showApp();
            sprintf(buff, "%d is time difference\n",
                    endTime - startTime);
            writeToFile(buff, LOGOTHER, "a+");
        }
        else {
            endTime = time(NULL);
        }
    }
    

    /* ------------------------- /
    int initQtApp(SimpleGUI
    spl, int argc, char* argv[]) {
    QApplication* app= new QApplication(argc, argv);
    if ( spl == NULL) spl = new SimpleGUI(app, argc, argv);
    char buff[256]="";
    sprintf(buff,"App created in %p, SimpleGUI in %p\n", app, spl);
    writeToFile(buff, LOGOTHER, "a+");
    return app->exec();
    }
    @
    In SimpleGUI.cpp:
    @
    ...
    void SimpleGUI::showApp() {
    emit showWindow();
    }
    @
    in constructor:
    @
    ...
    QObject::connect(this, SIGNAL(showWindow()),
    window, SLOT(show()) );
    @
    when the time is reached, spl->showApp() does nothing...
    [quote author="SGaist" date="1410948603"]Hi,

    You didn't implement showWindow did you ?[/quote]

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #5

      what is it supposed to do ? What is the implementation of showApp ? That should help to answer your qn.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • H Offline
        H Offline
        heatblazer
        wrote on last edited by
        #6

        I`ve update my previous poll with showApp() implementation and QObject::connect to the window and my app. showApp() emits showWinodw() signal, which is connected to window SLOT(show()). See my prev post.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #7

          @QObject::connect(this, SIGNAL(showWindow()), window, SLOT(show()) );@

          When the signal showWindow().. happens, it should show the 'window' widget. When the signal is emitted what is the status of 'window'. Is it shown already somewhere ?

          In order to check that, it really working, you can try with following.

          QPushButton *button = new QPushButton("Showme");
          QObject::connect(this, SIGNAL(showWindow()), button, SLOT(show()) )

          See what happens. If this is working, you must some logic issue in your program.

          1. There must be some logic issue
          2. 'window' object may have parent and parent is already shown.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          1 Reply Last reply
          0
          • H Offline
            H Offline
            heatblazer
            wrote on last edited by
            #8

            No, Ive never showed the window. The constructor, just connects, signals, prepares buttons, texts, etc. Then does nothing. Ive edited the while(1) loop to wait 2 sec then call spl->showApp() without conditions, and it never shows.
            [quote author="Dheerendra" date="1410955300"]@QObject::connect(this, SIGNAL(showWindow()), window, SLOT(show()) );@

            When the signal showWindow().. happens, it should show the 'window' widget. When the signal is emitted what is the status of 'window'. Is it shown already somewhere ?

            In order to check that, it really working, you can try with following.

            QPushButton *button = new QPushButton("Showme");
            QObject::connect(this, SIGNAL(showWindow()), button, SLOT(show()) )

            See what happens. If this is working, you must some logic issue in your program.

            1. There must be some logic issue
            2. 'window' object may have parent and parent is already shown.

            [/quote]

            1 Reply Last reply
            0
            • H Offline
              H Offline
              heatblazer
              wrote on last edited by
              #9

              As far as I am getting any progress, it appears, that showApp() must appear before app->exec() function, otherwise it does not work. However, this makes writing the app to be called from a UNIX daemon hard. How exactly to handle this exec() problem? I just want my GUI to show in fixed intervals.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                wrote on last edited by
                #10

                Did you try with pushbutton example I gave you ? Did it show pushbutton ? Also can you show me the piece of code where you are creating window object ?

                Dheerendra
                @Community Service
                Certified Qt Specialist
                https://www.pthinks.com

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  heatblazer
                  wrote on last edited by
                  #11

                  Yes, the SIGNAL/SLOT is working, I`ve tried it out. But it appeared that everything must happen in oreder and finishes with app->exec(). Here is my code in the while(1) loop, it works only one time, it shows the app jsut once. Once I press the PRINT TO LOG BUTTON, which is connected to window SLOT(hide()), it never appears
                  @
                  time_t startTime ;
                  time_t endTime ;
                  startTime = endTime = time(NULL);
                  SimpleGUI* spl = NULL;
                  while (1) {
                  sleep(2);
                  if ( endTime - startTime > (2) ) {
                  char buff[512]="";
                  sprintf(buff, "%d is time difference\n",
                  endTime - startTime);
                  writeToFile(buff, LOGOTHER, "a+");
                  int qtstarted = initQtApp(spl, argc, argv);
                  }
                  else {
                  endTime = time(NULL);
                  }
                  }
                  ...
                  int initQtApp(SimpleGUI* sp, int argc, char* argv[]) {
                  QApplication* qapp= new QApplication(argc, argv);
                  if ( sp != NULL ) sp->showApp();
                  else {
                  sp = new SimpleGUI(qapp, argc, argv);
                  sp->showApp();
                  }
                  char buff[256]="";
                  sprintf(buff,"App created in %p, SimpleGUI in %p\n", qapp, sp);
                  writeToFile(buff, LOGOTHER, "a+");
                  return qapp->exec();
                  }
                  @
                  initQtApp simulates a main()
                  And here is the constcution of the SimpleGUI, only the important part:
                  @
                  SimpleGUI::SimpleGUI(QApplication* app, int args, char** argvs)
                  : qapp(app) {
                  isActive = true;
                  window = new QWidget;
                  yes = new QPushButton("STOP DAEMON");
                  no = new QPushButton("PRINT TO LOG FILE");
                  buttonsLay = new QVBoxLayout;
                  textLay = new QVBoxLayout;
                  qvb = new QVBoxLayout;
                  align = new QHBoxLayout;
                  text = new QTextEdit("TEST FOR TEXT AREA");
                  text->setEnabled(false);
                  text->setMaximumHeight(200);
                  text->setMaximumWidth(200);
                  text->setMinimumHeight(200);
                  text->setMinimumWidth(200);

                  window->setMinimumHeight(320);
                  window->setMaximumHeight(320);
                  window->setMinimumWidth(320);
                  window->setMaximumWidth(320);
                  
                  yes->setMinimumWidth(200);
                  yes->setMaximumWidth(200);
                  no->setMinimumWidth(200);
                  no->setMaximumWidth(200);
                  
                  textLay->addWidget(text);
                  buttonsLay->addWidget(yes);
                  buttonsLay->addWidget(no);
                  
                  qvb->addLayout(textLay);
                  qvb->addLayout(buttonsLay);
                  
                  align->addLayout(qvb);
                  window->setLayout(align);
                  
                  SimpleGUI::argc = args;
                  SimpleGUI::argv = argvs;
                  QObject::connect(this, SIGNAL(showWindow()),
                                   window, SLOT(show()));
                  QObject::connect(no, SIGNAL(clicked()),
                                   this, SLOT(callDirwalk()) );
                  QObject::connect(no, SIGNAL(clicked()),
                                   window, SLOT(hide())) ;
                  //[1] ok - stops daemon
                  QObject::connect(yes, SIGNAL(clicked()),
                                   this, SLOT(deactivate()) );
                  

                  }
                  QWidget* SimpleGUI::getWindow() {
                  return window;
                  }

                  void SimpleGUI::callDirwalk() {
                  //thread main()
                  writeToFile("Called dirwalk on separate thread\n", LOGOTHER, "a+");
                  pthread_t t;
                  int res =0;
                  res = pthread_create(&t, NULL, doSomething2, NULL);

                  }

                  void SimpleGUI::activate() {

                  }
                  void SimpleGUI::deactivate() {
                  delete this;
                  }
                  void SimpleGUI::printDiagnostics() {

                  }
                  void SimpleGUI::showApp() {
                  emit this->showWindow();
                  }
                  void SimpleGUI::hideApp() {
                  window->hide();
                  }

                  void SimpleGUI::doSomething() {
                  delete this;
                  QObject::connect(qapp, SIGNAL(aboutToQuit()),
                  qapp, SLOT(quit()) );
                  //

                  }

                  @
                  [quote author="Dheerendra" date="1410958444"]Did you try with pushbutton example I gave you ? Did it show pushbutton ? Also can you show me the piece of code where you are creating window object ?[/quote]

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    heatblazer
                    wrote on last edited by
                    #12

                    Another thing, when I press windows X on the frame, it reappears again, but globs memory as much as I close it from X. Ive tried to make some instances static, but it does nothing at all... Just to tell you that my log file says:
                    @
                    App created in 0x96807d8, SimpleGUI in 0x9783f20

                    App created in 0x96807d8, SimpleGUI in 0x97cf758

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x97e5320

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x97f0d00

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x9800e80

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x98077f8

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x980dcb8

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x9813978

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x981a6f8

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x9824fd0

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x982b618

                    4 is time difference

                    App created in 0x96807d8, SimpleGUI in 0x9831c30

                    App terminated

                    App created in 0x93ad7d8, SimpleGUI in 0x94b0f98

                    App created in 0x9462210, SimpleGUI in 0x94fc8d0

                    5 is time difference

                    App created in 0x94fc6f0, SimpleGUI in 0x951cdb8

                    5 is time difference

                    App created in 0x95138a0, SimpleGUI in 0x951cf78

                    App terminated

                    @
                    If initQtApp() has non-static QApplication variable, if its static Apps address is constant, just SimpleGUI changes. But the previous GUI is not freed properly and task manager reports increase in MB needed for the program ... :(

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      Your code is pretty convoluted for what seems to be a simple task. Can you tell us what exactly would you like to achieve ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        heatblazer
                        wrote on last edited by
                        #14

                        Yes. I am starting a daemon in linux. In fixed intervals, I want it do display an UI in Qt. The UI is responsible to call 3 special functions, all involving an iteration in a list ( as stack ). The 3 functions are Qt independant in pure C. It`s a directory walk, which puts all filenames and dir names in a linked list. Then, depending on the callback, you can either print directories/files, delete them, or crypt them. I tough it will be as simple as that, but ... here I am stuck here.

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          heatblazer
                          wrote on last edited by
                          #15

                          I`ve made this in Java with a new thread which sleeps for N time, then shows the frame, the user click just hides it... Simple. But I want this app to be different. The UI must be versatile, either Qt, either GTK, maybe JUCE++. So I want logics to be separate from the UI. I am saying it because, many would suggest that I have to do it in pure Qt.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            If you want to write a real daemon, then don't put GUI code in it. A daemon doesn't have a GUI.

                            Completely separate both and use e.g. D-Bus or another RPC to communicated with your daemon.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              heatblazer
                              wrote on last edited by
                              #17

                              This is not what I wanted to hear. I can simply put a sleep in GUI constructor or method, then just emit show - hide. Its simple and tested, however, I wanted that kind of behaviour, and almost achieved it, but it appears my old code with recreation of Qt app will be added :( Too bad its not as simple as just show hide.
                              [quote author="SGaist" date="1411025456"]If you want to write a real daemon, then don't put GUI code in it. A daemon doesn't have a GUI.

                              Completely separate both and use e.g. D-Bus or another RPC to communicated with your daemon.[/quote]

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

                                Then don't make it a daemon. Just to be sure we understand each other, a thread running in the background doesn't qualify as daemon.

                                Have a look at QThread's documentation and the worker object part. You can couple that with e.g. a QTimer and then you have your function called at regular interval done in a thread.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  heatblazer
                                  wrote on last edited by
                                  #19

                                  Yes, Ive figured it out... The java thing Ive made... A sleeping thread with show/hide. OK, since I am not the type that will just walk aside Ive came with a solution, just a bit messy but, worked like a charm. Here is the daemon infinite loop: @ qapp = new QApplication(argc, argv); while (1) { do { sleep(1); initQtApp(NULL, argc, argv, 0); /* ok logic */ if ( endTime - startTime > (5) ) { char buff[512]=""; sprintf(buff, "%d is time difference\n", endTime - startTime); writeToFile(buff, LOGOTHER, "a+"); } else { endTime = time(NULL); } } while ( qapp->exec() == 0); } @ Now... qapp is global static variable. initQtApp() has local SimpleGUI pointer that is recreated everytime. Ive figured out that app->exec() will behave for the SimpleGUI associated with. So my log file says:
                                  @
                                  App created in 0x95207d8, SimpleGUI in 0x962f0d8
                                  8 is time difference
                                  -1 is isRunning now
                                  App terminated
                                  App created in 0x95207d8, SimpleGUI in 0x9677820
                                  8 is time difference
                                  Called dirwalk on separate thread
                                  Called a separate thread foo()
                                  CREATED:2014:09:18:14:28:46
                                  App created in 0x95207d8, SimpleGUI in 0x967a000
                                  8 is time difference
                                  Called dirwalk on separate thread
                                  Called a separate thread foo()
                                  @
                                  Pressing the STOP DAEMON just calls a SLOT that simply deletes window, textframe and the 2 buttons, nothing much, also reports some variables for the daemon shared with get/set functions, since they are static. This loop is not final one, but somehow manages to create a new GUI in every second, then wait, and when I destroy it, it just recreates it. TODO is to connect the right signals and slots to the coresponding buttons, and one question actually - can I disable the X button close function? It behaves right now, since close destroys the window, and daemon recreates it but still...
                                  [quote author="SGaist" date="1411033968"]Then don't make it a daemon. Just to be sure we understand each other, a thread running in the background doesn't qualify as daemon.

                                  Have a look at QThread's documentation and the worker object part. You can couple that with e.g. a QTimer and then you have your function called at regular interval done in a thread.[/quote]

                                  1 Reply Last reply
                                  0

                                  • Login

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