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. A QMenu Popup to Confirm a Button Click
QtWS25 Last Chance

A QMenu Popup to Confirm a Button Click

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmenuqactionpopup
7 Posts 2 Posters 747 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
    Calvin H-C
    wrote on last edited by
    #1

    I am writing code in QT to replicate this operation in an application previously written for MFC: In a dialog with a list of items, when one selects an item and clicks the "Delete" button, instead of just deleting it, a popup menu with one item, "Confirm Delete" appears and the user must click it to perform the delete. If they click somewhere else or hit ESC, the menu closes and the delete operation does not get executed.

    Here is the I thought would work, but the menu never appears and the QAction pointer is left as nullptr, so it just sees the operation cancelled:

    void MyDialog::on_Delete_clicked()
    {
    	QMenu	menu( ui->Delete );
    	QAction	*atAction = nullptr;
    
    	menu.addSection( "Confirm Delete" );
    	menu.popup( QCursor::pos(), atAction );
    	qDebug() << "Result:" << ( ( atAction == nullptr ) ? "Escaped" : "Confirmed" );
    }
    

    Once I can get this to have a result that confirms the user clicked the menu, the debug line will be removed and a call to the code that deletes the list item will replace it.

    JonBJ 1 Reply Last reply
    0
    • C Calvin H-C

      I am writing code in QT to replicate this operation in an application previously written for MFC: In a dialog with a list of items, when one selects an item and clicks the "Delete" button, instead of just deleting it, a popup menu with one item, "Confirm Delete" appears and the user must click it to perform the delete. If they click somewhere else or hit ESC, the menu closes and the delete operation does not get executed.

      Here is the I thought would work, but the menu never appears and the QAction pointer is left as nullptr, so it just sees the operation cancelled:

      void MyDialog::on_Delete_clicked()
      {
      	QMenu	menu( ui->Delete );
      	QAction	*atAction = nullptr;
      
      	menu.addSection( "Confirm Delete" );
      	menu.popup( QCursor::pos(), atAction );
      	qDebug() << "Result:" << ( ( atAction == nullptr ) ? "Escaped" : "Confirmed" );
      }
      

      Once I can get this to have a result that confirms the user clicked the menu, the debug line will be removed and a call to the code that deletes the list item will replace it.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Calvin-H-C
      I believe QMenu::popUp() is asynchronous/does not wait. And menu goes out of scope, hence you don't see it. Did you mean menu.exec() here?

      C 1 Reply Last reply
      2
      • JonBJ JonB

        @Calvin-H-C
        I believe QMenu::popUp() is asynchronous/does not wait. And menu goes out of scope, hence you don't see it. Did you mean menu.exec() here?

        C Offline
        C Offline
        Calvin H-C
        wrote on last edited by
        #3

        @JonB I had tried both to see what would display the menu, and should have lead with using exed() first:

        void OccupancyTab::on_Del1_clicked()
        {
        	QMenu	menu( ui->Del1 );
        
        	menu.addSection( "Confirm Delete" );
        	QAction	*Result = menu.exec( QCursor::pos() );
        	qDebug() << "Result:" << ( ( Result == nullptr ) ? "Escaped" : "Confirmed" );
        }
        

        As you mention, popup() is asynchronous, so it returned immediately which indicated "Escaped".

        Using exec(), it waits, but still does not display the menu. If one hits ESC or clicks somewhere on the screen, it will return and indicate "Escaped".

        What am I missing to actually display the menu?

        JonBJ 1 Reply Last reply
        0
        • C Calvin H-C

          @JonB I had tried both to see what would display the menu, and should have lead with using exed() first:

          void OccupancyTab::on_Del1_clicked()
          {
          	QMenu	menu( ui->Del1 );
          
          	menu.addSection( "Confirm Delete" );
          	QAction	*Result = menu.exec( QCursor::pos() );
          	qDebug() << "Result:" << ( ( Result == nullptr ) ? "Escaped" : "Confirmed" );
          }
          

          As you mention, popup() is asynchronous, so it returned immediately which indicated "Escaped".

          Using exec(), it waits, but still does not display the menu. If one hits ESC or clicks somewhere on the screen, it will return and indicate "Escaped".

          What am I missing to actually display the menu?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Calvin-H-C Why are you using QMenu::addSection()?

          C 1 Reply Last reply
          0
          • JonBJ JonB

            @Calvin-H-C Why are you using QMenu::addSection()?

            C Offline
            C Offline
            Calvin H-C
            wrote on last edited by
            #5

            @JonB when I found there was no "AddItem()" type of function, I went searching for examples and came across one that used addSection().

            JonBJ 1 Reply Last reply
            0
            • C Calvin H-C

              @JonB when I found there was no "AddItem()" type of function, I went searching for examples and came across one that used addSection().

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Calvin-H-C
              Well per the link that's just a separator with a tooltip. Maybe you don't see it. Use addAction(), does it then show?

              C 1 Reply Last reply
              1
              • JonBJ JonB

                @Calvin-H-C
                Well per the link that's just a separator with a tooltip. Maybe you don't see it. Use addAction(), does it then show?

                C Offline
                C Offline
                Calvin H-C
                wrote on last edited by
                #7

                @JonB that works. Don't know why I didn't try that, other than getting lost in all the overloads that involved signals/slots. The simplest one with just a QString argument does the trick.

                Thanks!

                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