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. Template function
Forum Update on Monday, May 27th 2025

Template function

Scheduled Pinned Locked Moved Solved General and Desktop
template
7 Posts 3 Posters 2.3k 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.
  • U Offline
    U Offline
    UnitScan
    wrote on 28 Feb 2017, 11:08 last edited by UnitScan
    #1

    In .cpp file I have this template function:

    template <typename M, typename N> void MainWindow::showMenu(M m, N n)
    {
        QSize size = m->sizeHint();
        m->popup(n->mapToGlobal(QPoint(0,0-size.height())));
    }
    

    where
    · m = QMenu
    · n = QPushPutton

    In what form I should declare this function in the header file?

    Thanks in advanve

    M 1 Reply Last reply 28 Feb 2017, 11:10
    0
    • U UnitScan
      28 Feb 2017, 11:08

      In .cpp file I have this template function:

      template <typename M, typename N> void MainWindow::showMenu(M m, N n)
      {
          QSize size = m->sizeHint();
          m->popup(n->mapToGlobal(QPoint(0,0-size.height())));
      }
      

      where
      · m = QMenu
      · n = QPushPutton

      In what form I should declare this function in the header file?

      Thanks in advanve

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Feb 2017, 11:10 last edited by mrjj
      #2

      @UnitScan

      What about MainWindow.h ?

      Also since all QWidgets are QObjects, you dont need templates for most GUI operations.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        UnitScan
        wrote on 28 Feb 2017, 11:19 last edited by UnitScan
        #3
        connect(sel, SIGNAL(released()), this, SLOT(showMenu(QMenu, QPushButton)));
        
        void MainWindow::showMenu(QMenu *m, QPushButton *pb)
        {
            QSize size = m->sizeHint();
            m->popup(pb->mapToGlobal(QPoint(0,0-size.height())));
        }
        

        Doesn't work

        M 1 Reply Last reply 28 Feb 2017, 11:25
        0
        • U UnitScan
          28 Feb 2017, 11:19
          connect(sel, SIGNAL(released()), this, SLOT(showMenu(QMenu, QPushButton)));
          
          void MainWindow::showMenu(QMenu *m, QPushButton *pb)
          {
              QSize size = m->sizeHint();
              m->popup(pb->mapToGlobal(QPoint(0,0-size.height())));
          }
          

          Doesn't work

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Feb 2017, 11:25 last edited by mrjj
          #4

          @UnitScan said in Template function:

          Hi
          That is because the released() signal DO NOT have QMenu* and QPushButton * parameters.

          You cannot invent new parameters for a signal.
          You can define new signals though.

          http://doc.qt.io/qt-5/signalsandslots.html

          also note that connect returns true if connect works.

          so

          qDebug() << "tcheck:" << connect(xxx) to see if they work.

          1 Reply Last reply
          2
          • U Offline
            U Offline
            UnitScan
            wrote on 28 Feb 2017, 11:44 last edited by
            #5

            SOLVED

            void MainWindow::showMenu()
            {
                QPushButton *pb = qobject_cast<QPushButton*>(sender());
                QMenu* m = qobject_cast<QMenu*>(pb->children().at(0));
                QSize size = m->sizeHint();
                m->popup(pb->mapToGlobal(QPoint(0,0-size.height())));
            }
            
            M 1 Reply Last reply 28 Feb 2017, 11:50
            0
            • U UnitScan
              28 Feb 2017, 11:44

              SOLVED

              void MainWindow::showMenu()
              {
                  QPushButton *pb = qobject_cast<QPushButton*>(sender());
                  QMenu* m = qobject_cast<QMenu*>(pb->children().at(0));
                  QSize size = m->sizeHint();
                  m->popup(pb->mapToGlobal(QPoint(0,0-size.height())));
              }
              
              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Feb 2017, 11:50 last edited by
              #6

              @UnitScan

              Hi you should check both pb an m for not being null :)

              1 Reply Last reply
              2
              • V Offline
                V Offline
                VRonin
                wrote on 28 Feb 2017, 12:30 last edited by VRonin
                #7

                since I hate sender() and pb->children().at(0) is even less robust I suggest (assuming you declared QMenu* MainWindow::m):

                connect(sel, &QPushButton::released, [=]()->void{
                const QSize size = m->sizeHint();
                m->popup(sel->mapToGlobal(QPoint(0,-size.height())));
                });
                

                P.S.
                Looking at this functionality, are you sure QToolButton is not a better choice? see QToolButton::setMenu

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1

                1/7

                28 Feb 2017, 11:08

                • 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