Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Can someone explain this syntax to me?

Can someone explain this syntax to me?

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 4.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.
  • C Offline
    C Offline
    CynicalPenguin
    wrote on last edited by
    #1

    I'm using the terminal example to learn how to make a terminal in qt, and I don't understand this syntax:

    connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
            this, &SettingsDialog::showPortInfo);
    

    I understand that the serialPortInfoListBox (a combo box) is the sender. I don't understand this part:

    static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
    

    This is my signal argument, but I don't get what it's saying. Is this like a really fancy pointer to a function? I would go to the documentation but idk where to look for this. The (QComboBox::*)(int) is especially confusing because it essentially points to nothing, right? Shouldn't a scope have to have something after the :: ?

    joeQJ 1 Reply Last reply
    0
    • C CynicalPenguin

      I'm using the terminal example to learn how to make a terminal in qt, and I don't understand this syntax:

      connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
              this, &SettingsDialog::showPortInfo);
      

      I understand that the serialPortInfoListBox (a combo box) is the sender. I don't understand this part:

      static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
      

      This is my signal argument, but I don't get what it's saying. Is this like a really fancy pointer to a function? I would go to the documentation but idk where to look for this. The (QComboBox::*)(int) is especially confusing because it essentially points to nothing, right? Shouldn't a scope have to have something after the :: ?

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by joeQ
      #2

      @CynicalPenguin Hi, friend. Welcome.

      First, this way is convert function type.

      We will see signals in QComboBox

      void currentIndexChanged(int index)
      void currentIndexChanged(const QString &text)

      There are two signals have the same function name, but parameters type is different.

      In Qt4. we used SIGNAL and SLOT in connect, like

      connect(cbox,SIGNAL(currentIndexChanged(int index)),this,SLOT(slot_function(int index));
      connect(cbox,SIGNAL(currentIndexChanged(const QString &text)),this,SLOT(slot_function(const QString &text));
      

      In compile time. The Qt use compare string ways to connect signal and slots. It is In Qt4, using the SIGNAL and SLOT we do not need to distinguish these signal has same function and different parameters.

      But in Qt5. we can use pointer of signal and slot function in connect. like

      connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use index
      connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use text
      

      But, Qt don't know which function you want to use. so when we use pointer of function in connect, if signal or slot has overloaded. we should convert pointer type of function to tell Qt which function we want to connect.

      so we used static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), tell Qt, I want to use index not text.

      About the convert syntax, like

      static_cast<returnType (ClassName::*)(parameter1Type,parameter1Type)>(pointerOfFunction)
      
      • static_cast c++ convert type keyword
      • returnTypr means function return type, in signal and slot, most is void
      • ClassName::* means which class of the function in.
      • the paramters types in function, one, two, three...
      • the function pointer.

      Just like convert pointer type

      int *pint;
      void* pVoid;
      pVoid = static_cast<void *>(pint); // convert int* => void*
      pint   = static_cast<int *>(pVoid); // convert void* => int*

      Just do it!

      J 1 Reply Last reply
      9
      • C Offline
        C Offline
        CynicalPenguin
        wrote on last edited by
        #3

        Gotcha. Thanks!

        1 Reply Last reply
        0
        • joeQJ joeQ

          @CynicalPenguin Hi, friend. Welcome.

          First, this way is convert function type.

          We will see signals in QComboBox

          void currentIndexChanged(int index)
          void currentIndexChanged(const QString &text)

          There are two signals have the same function name, but parameters type is different.

          In Qt4. we used SIGNAL and SLOT in connect, like

          connect(cbox,SIGNAL(currentIndexChanged(int index)),this,SLOT(slot_function(int index));
          connect(cbox,SIGNAL(currentIndexChanged(const QString &text)),this,SLOT(slot_function(const QString &text));
          

          In compile time. The Qt use compare string ways to connect signal and slots. It is In Qt4, using the SIGNAL and SLOT we do not need to distinguish these signal has same function and different parameters.

          But in Qt5. we can use pointer of signal and slot function in connect. like

          connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use index
          connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use text
          

          But, Qt don't know which function you want to use. so when we use pointer of function in connect, if signal or slot has overloaded. we should convert pointer type of function to tell Qt which function we want to connect.

          so we used static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), tell Qt, I want to use index not text.

          About the convert syntax, like

          static_cast<returnType (ClassName::*)(parameter1Type,parameter1Type)>(pointerOfFunction)
          
          • static_cast c++ convert type keyword
          • returnTypr means function return type, in signal and slot, most is void
          • ClassName::* means which class of the function in.
          • the paramters types in function, one, two, three...
          • the function pointer.

          Just like convert pointer type

          int *pint;
          void* pVoid;
          pVoid = static_cast<void *>(pint); // convert int* => void*
          pint   = static_cast<int *>(pVoid); // convert void* => int*
          J Offline
          J Offline
          JJLee
          wrote on last edited by
          #4

          @joeq This was very helpful to me. Thanks for the complete and detailed explanation of the syntax involved. Your answer should be included in the Qt All Classes webpage for QComboBox::currentIndexChanged(int index) and QComboBox::currentIndexChanged(const QString &text).

          Since QComboBox::currentIndexChanged(const QString &text) is overloaded, the previous syntax doesn't seem to work anymore.

          jsulmJ 1 Reply Last reply
          0
          • J JJLee

            @joeq This was very helpful to me. Thanks for the complete and detailed explanation of the syntax involved. Your answer should be included in the Qt All Classes webpage for QComboBox::currentIndexChanged(int index) and QComboBox::currentIndexChanged(const QString &text).

            Since QComboBox::currentIndexChanged(const QString &text) is overloaded, the previous syntax doesn't seem to work anymore.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @jjlee said in Can someone explain this syntax to me?:

            the previous syntax doesn't seem to work anymore

            It does, but you need to tell the compiler which version you want to use. For that you can use https://doc.qt.io/qt-5/qtglobal.html#qOverload macro.

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

            1 Reply Last reply
            3

            • Login

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