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. [SOLVED] Close signal send to other class

[SOLVED] Close signal send to other class

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k 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.
  • Z Offline
    Z Offline
    ZSWASZ
    wrote on last edited by
    #1

    Hello. I have two classes, QMainWindow and QDialog. Each class has separate GUI.
    In main function I first call QMainWindow (with gui), then after some operations I want to close this gui (this->close()) , and send signal to 2nd class to open. Is this possible?

    I was trying sth like that:

    main.cpp
    @ class1 w;
    w.show();
    if(!w.isVisible())
    {
    class2 u;
    u.show();
    }@

    but it's not working :(

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisberlin
      wrote on last edited by
      #2

      There are mainly two problems here:

      • the way the code is written, u is an automatic variable local to the if() block, so in the example it immediately goes out of scope after the u.show() call and is therefore freed.
        Use a dynamic instance instead which will remain existant:
        class2* u = new class2;
        u->show();
      • w.show() is does not result in a modal window, instead it makes the window visible and then returns immediately. The visibility check in the if statement, which is executed next, therefore always evaluates to true. In order to create u as desired (ie when w gets closed), declare a slot in class1 and connect it to the close() signal. Execute the creation code in the slot implementation.
      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        You can try with this code. I'm assuming that you are writing your own classes which are inherited from QMainWindow and QDialog. There is no close signal when the mainwindow is closed. You need to implement your own signal for mainwindow.

        @class MainWindow : public QMainWindow{
        signals :
        void iamClosed();
        public slots :
        void closeEvent() {
        emit iamClosed();
        }
        }

        main() {
        MainWindow m;
        Dialog d;
        connect(&m,SIGNAL(iamClosed()),&d,SLOT(show()));
        }@

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

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZSWASZ
          wrote on last edited by
          #4

          @chrisberlin, I forgot that it's non-modal window, thanks ;) I tried sth like your way, and it works too, i.e make slot in class1, which creates 2nd GUI and close itself, it works too.

          @Dheerendra, thanks, I didn't know that signals can be declared in classes :) I must read more documentations...
          I will use your way, I think that's more 'elegant'.

          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