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 connect customWiget Signals and Slots

How connect customWiget Signals and Slots

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.6k 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.
  • L Offline
    L Offline
    LCorona
    wrote on last edited by LCorona
    #1

    My app has many custom widgets each has their own signals, but I need one of these customwidget control the other other ones, the tutorials always use qt creator with included widgets, when I use the Edit signal /slots editor, in the end the IDE seeks the signal function of the customwidget in the mainwindow, when this signal is defined in the own file of the customwidget, I dont know then of connect the customwidgets.

    I use the customwiget in my project only by including the header in the mainwindow header file.

    Honestly I dont understand very well of do this connections, the tutorials are ambiguos. Only the command connect is nedded?, of work in union with the ui Edit signal /slots editor, or both do the same??

    In QT Creator while run the appa this message appear, Warnings(???)

    QObject::connect: No such signal QCustomPlot::rangeChanged() in ./ui_mainwindow.h:439
    QObject::connect: (sender name: 'rule')
    QObject::connect: (receiver name: 'customPlot1')

    Where rangeChanged is the name of Signal f QCustomPlot widget send

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      When using Designer, to connect signals to slots its
      often use an auto connect feature.

      When you add a slot a function is added that is in the format
      On_WidgetName_SignalName()

      When we run the app, inside setupUI the function
      QMetaObject::connectSlotsByName(MainWindow);
      is called.
      It will (try) find a widget and a matching slot and auto connect them.

      However, this can easy break if you rename the widget or the slot function name.

      For that reason, its recommended to simply always connect the widget manually.

      for that, you only need to use the connect command.
      and it works too with custom widgets like custom plot.

      L 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        When using Designer, to connect signals to slots its
        often use an auto connect feature.

        When you add a slot a function is added that is in the format
        On_WidgetName_SignalName()

        When we run the app, inside setupUI the function
        QMetaObject::connectSlotsByName(MainWindow);
        is called.
        It will (try) find a widget and a matching slot and auto connect them.

        However, this can easy break if you rename the widget or the slot function name.

        For that reason, its recommended to simply always connect the widget manually.

        for that, you only need to use the connect command.
        and it works too with custom widgets like custom plot.

        L Offline
        L Offline
        LCorona
        wrote on last edited by
        #3

        @mrjj
        Hi!!, Ok, I will made and test in that way.

        mrjjM 1 Reply Last reply
        1
        • L LCorona

          @mrjj
          Hi!!, Ok, I will made and test in that way.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LCorona
          Hi
          There is 2 syntaxes.
          Its a good idea to practice the new one as it has benefits.
          https://wiki.qt.io/New_Signal_Slot_Syntax

          Its normally not very hard
          it's normally something like

          connect(ui->customplotName, &QCustomPlot::SignalName, this, &QMainWindow::slotName );

          the & is important. Make sure it does not help you and add () to signalname / slot name

          Also make sure the slot has the same parameters as the signal has.

          L 1 Reply Last reply
          1
          • mrjjM mrjj

            @LCorona
            Hi
            There is 2 syntaxes.
            Its a good idea to practice the new one as it has benefits.
            https://wiki.qt.io/New_Signal_Slot_Syntax

            Its normally not very hard
            it's normally something like

            connect(ui->customplotName, &QCustomPlot::SignalName, this, &QMainWindow::slotName );

            the & is important. Make sure it does not help you and add () to signalname / slot name

            Also make sure the slot has the same parameters as the signal has.

            L Offline
            L Offline
            LCorona
            wrote on last edited by
            #5

            @mrjj
            Excellent!!, works really fine in both syntaxes, thank you, problem resolved.

            In the end the solution was very easy, only one line of code nedded.

            mrjjM 1 Reply Last reply
            0
            • L LCorona

              @mrjj
              Excellent!!, works really fine in both syntaxes, thank you, problem resolved.

              In the end the solution was very easy, only one line of code nedded.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @LCorona
              Super.
              The main difference between the SIGNAL() and SLOT() macros and the
              new syntax is when errors are detected.
              The macros will accept anything when you compile and first fail when app runs mostly
              by you discovering that nothing happens.

              The new syntax, however, gets help from the compiler and will catch errors at compile time.

              For a big app, its very helpful as if you rename some slot or similar, you get told at once by the compiler.

              Also the new syntax allows to use lambdas which can be very handy.
              A lambda is just a nameless in place function.
              Like

              QObject::connect(timer, &QTimer::timeout, [this](){
                      ....
                  });
              

              the part between the {} is like a slot but you dont need to define in .h etc.

              This can sometimes be very handy :)

              L 1 Reply Last reply
              1
              • mrjjM mrjj

                @LCorona
                Super.
                The main difference between the SIGNAL() and SLOT() macros and the
                new syntax is when errors are detected.
                The macros will accept anything when you compile and first fail when app runs mostly
                by you discovering that nothing happens.

                The new syntax, however, gets help from the compiler and will catch errors at compile time.

                For a big app, its very helpful as if you rename some slot or similar, you get told at once by the compiler.

                Also the new syntax allows to use lambdas which can be very handy.
                A lambda is just a nameless in place function.
                Like

                QObject::connect(timer, &QTimer::timeout, [this](){
                        ....
                    });
                

                the part between the {} is like a slot but you dont need to define in .h etc.

                This can sometimes be very handy :)

                L Offline
                L Offline
                LCorona
                wrote on last edited by
                #7

                @mrjj
                Ok, understood. Thanks.

                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