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 Updated to NodeBB v4.3 + New Features

Template function

Scheduled Pinned Locked Moved Solved General and Desktop
template
7 Posts 3 Posters 2.3k Views 1 Watching
  • 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.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on 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

    mrjjM 1 Reply Last reply
    0
    • UnitScanU UnitScan

      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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      • UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on 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

        mrjjM 1 Reply Last reply
        0
        • UnitScanU UnitScan
          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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          • UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on 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())));
            }
            
            mrjjM 1 Reply Last reply
            0
            • UnitScanU UnitScan

              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())));
              }
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @UnitScan

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

              1 Reply Last reply
              2
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 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

                • Login

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