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. Signal and slot related problem?
QtWS25 Last Chance

Signal and slot related problem?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.2k Views
  • 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    Suppose in a single function i want to use the same button as a on off switch like. I mean when once i click the button a new window should open and when i again click the same button the same window should close.
    @connect(button, SIGNAL(clicked()), window, SLOT(open()));
    connect(button, SIGNAL(clicked()), window, SLOT(close()));@
    Here, my problem is that if i will use signal and slot like the code given above the window will open and suddenly it will close because
    both the signals are same so how can i solved it?

    Pratik Agrawal

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you can create a custom slot and use a boolean member to do open/close, depending on the last actio

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xeroblast
        wrote on last edited by
        #3

        i think you could use something like this :

        @
        window::window() {
        connect(button, SIGNAL(clicked()), this, SLOT(open()));
        }
        void window::open() {
        disconnect(button, SIGNAL(clicked()), this, SLOT(open()));
        connect(button, SIGNAL(clicked()), this, SLOT(close()));
        }
        void window::close() {
        disconnect(button, SIGNAL(clicked()), this, SLOT(close()));
        connect(button, SIGNAL(clicked()), this, SLOT(open()));
        }
        @

        not tested yet though...

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          You could do
          @
          connect(button, SIGNAL(clicked()), window, SLOT(toggleOpenClose()));
          @

          with a window class which has something like
          @

          class Window ...
          {
          Q_OBJECT
          ...
          public:
          Window() { is_open = false; }

          void open() { 
             // do opening stuff
             is_open = true;
          }
          
          void close() { 
             // do closing stuff
             is_open = false;
          }
          
          public slots:
          
              void toggleOpenClose()  {
                 if (is_open) 
                     close();
                 else 
                     open();
              }
          
          private:
             bool is_open;
          

          ...
          }
          @

          Edit: which is pretty much what Gerolf suggested above

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pratik041
            wrote on last edited by
            #5

            ok i will try that thank's

            Pratik Agrawal

            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