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. Multiple windows signal slot problem

Multiple windows signal slot problem

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 2.8k 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.
  • G Offline
    G Offline
    Godknows
    wrote on last edited by
    #1

    Hi guys,

    happy new year!

    I am a very new bird with Qt and am writing a small project with 2 windows: a preference window and a bargraph window. In preference window you can change the configurations like color, size usw., and the bar in the bargraph window should be changed correspongdingly.
    Now I am focus on the color change function. I added a color choose button on the preference window and with clicking it the slot function selectBarColor will be called and a QColorDialog can be opened. At the end of this funtion I tried to send the signal like this:
    @connect(&d, SIGNAL(colorSelected(const QColor&)), &bargraph, SLOT(setColor(const QColor&)));@
    with d is the QColorDialog object and bargraph is a Bargraph object.
    And I wrote the slot setColor in Bargraph like this:
    @void Bargraph::setColor(const QColor & color)
    {
    barColor = color;
    update();
    }@

    But it didn't work, it seems the setColor slot didn't get the signal.

    What could be the problems here?

    Thanks a lot!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      connect only connects the signal and slots. You need to use the emit keyword to send the signal.

      if the signal name is 'colorSelected' you should send the signal using

      emit colorSelected(color)

      Once you do this above slot will be called.

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

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

        Hi and welcome to devnet,

        Since "d" looks like it's created on the stack, are you sure it exists long enough to actually send the signal ?

        Just to correct Dheerendra, you can also connect signals to signals. It's called signal forwarding. Not necessarily useful in this precise case but might be in the future.

        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
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Yes, Signal to Signal connect also can be used. What I meant in the above post was that, connect does not emit signal. It only connects. If he is assuming that it automatically emits signal it will be potential issue.

          I think 'd' and 'bargraph' as stack variable are good observation from Galst. I feel this could be potential problem in your case. Make them as heap variables.

          d = new classD
          bargraph = new BarGraph.

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Godknows
            wrote on last edited by
            #5

            [quote author="Dheerendra" date="1389781569"]Yes, Signal to Signal connect also can be used. What I meant in the above post was that, connect does not emit signal. It only connects. If he is assuming that it automatically emits signal it will be potential issue.

            I think 'd' and 'bargraph' as stack variable are good observation from Galst. I feel this could be potential problem in your case. Make them as heap variables.

            d = new classD
            bargraph = new BarGraph.

            [/quote]

            Thanks a lot Dheerendra.

            I checked the use of "emit" and wote
            @emit d.colorSelected(bar_color);@

            But colorSelected(const QColor &) is a protected signal of QColorDialog. So it won't work if I wrote it in this way?
            Could you please give me some further suggestions?

            Many thanks!

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

              You can't emit a signal from another class

              QColorDialog will emit the signal when needed

              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
              • G Offline
                G Offline
                Godknows
                wrote on last edited by
                #7

                [quote author="SGaist" date="1389779303"]Hi and welcome to devnet,

                Since "d" looks like it's created on the stack, are you sure it exists long enough to actually send the signal ?

                Just to correct Dheerendra, you can also connect signals to signals. It's called signal forwarding. Not necessarily useful in this precise case but might be in the future.

                [/quote]
                Thank you very much SGaist!

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Godknows
                  wrote on last edited by
                  #8

                  [quote author="SGaist" date="1389789056"]You can't emit a signal from another class

                  QColorDialog will emit the signal when needed[/quote]

                  Thanks a lot SGaist, it is because the "stack", I changed the position of the "connect" and it works now.

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

                    What do you mean by: "changed the position of the connect" ?

                    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
                    • G Offline
                      G Offline
                      Godknows
                      wrote on last edited by
                      #10

                      [quote author="SGaist" date="1389876303"]What do you mean by: "changed the position of the connect" ?[/quote]

                      Just right after the declaration of d

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

                        Looks like you might be doing something unusual with your code, can you post it ?

                        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

                        • Login

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