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 to connect signal to this member function using the new method?

How to connect signal to this member function using the new method?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 911 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.
  • T Offline
    T Offline
    Tapsa
    wrote on last edited by
    #1

    I based my code on this example:

    void someFunction();
    QPushButton *button = new QPushButton;
    QObject::connect(button, &QPushButton::clicked, someFunction);
    

    In a header file I have:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        ...
    private:
        void select_filter(int);
        QComboBox *unit_filters;
    };
    

    Then in a source file I have:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ...
        unit_filters = new QComboBox;
        QObject::connect(unit_filters, &QComboBox::currentIndexChanged, select_filter);
        // or this
        QObject::connect(unit_filters, &QComboBox::currentIndexChanged, [this]{select_filter(0);});
    }
        
    void MainWindow::select_filter(int)
    {
        ...
    }
    

    How can I make it compile? As you can see, I did what the documentation told me.
    I am getting 'no matching function' and 'unresolved overload' etc.
    I am using Qt Creator 3.6.0 based on Qt 5.5.1.

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, you get an error because there are 2 different currentIndexChanged() signals, so you have to tell the compiler which one you want read more here

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JordanHarris
        wrote on last edited by JordanHarris
        #3

        Well the example you show is using a free function, not a member function. Perhaps that's why the first connect() doesn't work. In the second, you are missing the empty argument list "()" in the lambda. I believe this should work:

        QObject::connect(unit_filters, &QComboBox::currentIndexChanged, this,  &MainWindow::select_filter);
        // or this
        QObject::connect(unit_filters, &QComboBox::currentIndexChanged,  [this]() { select_filter(0); });
        
        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tapsa
          wrote on last edited by
          #4

          Thank you, hskoglund. I added a cast to the signal.

          static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
          
          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