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. Using connect on a custom QSpinBox
Forum Updated to NodeBB v4.3 + New Features

Using connect on a custom QSpinBox

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 729 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.
  • D Offline
    D Offline
    Dummie1138
    wrote on 17 Jan 2023, 11:39 last edited by
    #1

    Hi. I have the following code, meant to create a custom spin box widget that will round the input value to the nearest multiple of 3 every time the value is changed.

    Header:

    #ifndef SPINBOXTHREE_H
    #define SPINBOXTHREE_H
    
    #include <QObject>
    #include <QSpinBox>
    
    class spinBoxThree : public QSpinBox
    {
        Q_OBJECT
    public:
        explicit spinBoxThree(QWidget *parent = nullptr);
    
    private slots:
        int setThree();
    
    };
    
    #endif // SPINBOXTHREE_H
    
    

    cpp:

    #include "spinboxthree.h"
    
    spinBoxThree::spinBoxThree(QWidget *parent) : QSpinBox(parent){
        connect(this, &QSpinBox::valueChanged, this, [=](){setThree();});
    }
    
    int spinBoxThree::setThree(){
        int input = value();
        if (input % 3 != 0){
            input = input - (input % 3);
        }
        setValue(input);
        return input;
    }
    

    I am receiving a "no matching member function" error for the line connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); . I am aware that there are issues with using connect on objects not inheriting QObject, but as I need this object to be a QSpinBox, I am wondering whether there are methods I can use to connect the signals and slots within a non-QObject-defined custom class. Please let me know if more information is required.

    J 1 Reply Last reply 17 Jan 2023, 11:47
    0
    • D Dummie1138
      17 Jan 2023, 12:04

      @jsulm The build does not succeed. The error messages are:
      spinboxthree.cpp:7: error: no matching member function for call to 'connect'
      spinboxthree.cpp:7: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'

      spinboxthree.cpp: In constructor 'spinBoxThree::spinBoxThree(QWidget*)':
      spinboxthree.cpp:7:77: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'
      connect(this, &QSpinBox::valueChanged, this, ={setThree();});
      ^

      Placing QObject:: in front of connect, like so: QObject::connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); resulted in the same error.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Jan 2023, 12:15 last edited by
      #6

      @Dummie1138 said in Using connect on a custom QSpinBox:

      <unresolved overloaded function type>

      That explains the issue: this signal is overloaded, so you will have to do what is shown in documentation: https://doc.qt.io/qt-5/qspinbox.html#valueChanged

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 17 Jan 2023, 12:21
      3
      • D Dummie1138
        17 Jan 2023, 11:39

        Hi. I have the following code, meant to create a custom spin box widget that will round the input value to the nearest multiple of 3 every time the value is changed.

        Header:

        #ifndef SPINBOXTHREE_H
        #define SPINBOXTHREE_H
        
        #include <QObject>
        #include <QSpinBox>
        
        class spinBoxThree : public QSpinBox
        {
            Q_OBJECT
        public:
            explicit spinBoxThree(QWidget *parent = nullptr);
        
        private slots:
            int setThree();
        
        };
        
        #endif // SPINBOXTHREE_H
        
        

        cpp:

        #include "spinboxthree.h"
        
        spinBoxThree::spinBoxThree(QWidget *parent) : QSpinBox(parent){
            connect(this, &QSpinBox::valueChanged, this, [=](){setThree();});
        }
        
        int spinBoxThree::setThree(){
            int input = value();
            if (input % 3 != 0){
                input = input - (input % 3);
            }
            setValue(input);
            return input;
        }
        

        I am receiving a "no matching member function" error for the line connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); . I am aware that there are issues with using connect on objects not inheriting QObject, but as I need this object to be a QSpinBox, I am wondering whether there are methods I can use to connect the signals and slots within a non-QObject-defined custom class. Please let me know if more information is required.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 17 Jan 2023, 11:47 last edited by
        #2

        @Dummie1138 said in Using connect on a custom QSpinBox:

        I am aware that there are issues with using connect on objects not inheriting QObject

        Not sure what you mean. spinBoxThree is a QSpinBox, which is a QWidget, which is a QObject.
        Please post the whole error message.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply 17 Jan 2023, 11:54
        0
        • J jsulm
          17 Jan 2023, 11:47

          @Dummie1138 said in Using connect on a custom QSpinBox:

          I am aware that there are issues with using connect on objects not inheriting QObject

          Not sure what you mean. spinBoxThree is a QSpinBox, which is a QWidget, which is a QObject.
          Please post the whole error message.

          D Offline
          D Offline
          Dummie1138
          wrote on 17 Jan 2023, 11:54 last edited by Dummie1138
          #3

          @jsulm Thank you for your patience. Here is the error message.

          36089b8f-cb33-4269-9b44-c412aebe823d-image.png

          Where I got the understanding of QObject errors from: https://stackoverflow.com/questions/24410130/no-matching-function-for-qobjectconnect

          J 1 Reply Last reply 17 Jan 2023, 11:56
          0
          • D Dummie1138
            17 Jan 2023, 11:54

            @jsulm Thank you for your patience. Here is the error message.

            36089b8f-cb33-4269-9b44-c412aebe823d-image.png

            Where I got the understanding of QObject errors from: https://stackoverflow.com/questions/24410130/no-matching-function-for-qobjectconnect

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 17 Jan 2023, 11:56 last edited by
            #4

            @Dummie1138 It seems to come from the code model.
            Does the actual build succeed?
            You can also put QObject:: in front of connect().

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply 17 Jan 2023, 12:04
            0
            • J jsulm
              17 Jan 2023, 11:56

              @Dummie1138 It seems to come from the code model.
              Does the actual build succeed?
              You can also put QObject:: in front of connect().

              D Offline
              D Offline
              Dummie1138
              wrote on 17 Jan 2023, 12:04 last edited by
              #5

              @jsulm The build does not succeed. The error messages are:
              spinboxthree.cpp:7: error: no matching member function for call to 'connect'
              spinboxthree.cpp:7: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'

              spinboxthree.cpp: In constructor 'spinBoxThree::spinBoxThree(QWidget*)':
              spinboxthree.cpp:7:77: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'
              connect(this, &QSpinBox::valueChanged, this, ={setThree();});
              ^

              Placing QObject:: in front of connect, like so: QObject::connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); resulted in the same error.

              J 1 Reply Last reply 17 Jan 2023, 12:15
              0
              • D Dummie1138
                17 Jan 2023, 12:04

                @jsulm The build does not succeed. The error messages are:
                spinboxthree.cpp:7: error: no matching member function for call to 'connect'
                spinboxthree.cpp:7: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'

                spinboxthree.cpp: In constructor 'spinBoxThree::spinBoxThree(QWidget*)':
                spinboxthree.cpp:7:77: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'
                connect(this, &QSpinBox::valueChanged, this, ={setThree();});
                ^

                Placing QObject:: in front of connect, like so: QObject::connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); resulted in the same error.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 17 Jan 2023, 12:15 last edited by
                #6

                @Dummie1138 said in Using connect on a custom QSpinBox:

                <unresolved overloaded function type>

                That explains the issue: this signal is overloaded, so you will have to do what is shown in documentation: https://doc.qt.io/qt-5/qspinbox.html#valueChanged

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply 17 Jan 2023, 12:21
                3
                • J jsulm
                  17 Jan 2023, 12:15

                  @Dummie1138 said in Using connect on a custom QSpinBox:

                  <unresolved overloaded function type>

                  That explains the issue: this signal is overloaded, so you will have to do what is shown in documentation: https://doc.qt.io/qt-5/qspinbox.html#valueChanged

                  D Offline
                  D Offline
                  Dummie1138
                  wrote on 17 Jan 2023, 12:21 last edited by
                  #7

                  @jsulm Thank you very much. I have previously neglected the detailed error messages for being messy, today is a good day to learn that they actually contain useful information.

                  1 Reply Last reply
                  0

                  1/7

                  17 Jan 2023, 11:39

                  • Login

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