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. Menù spawn point
Qt 6.11 is out! See what's new in the release blog

Menù spawn point

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 3.7k 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

    How can I spawn this menù at the top of the button? Something like this

    [menù]
    [button]

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->setAcceptDrops(true);
    //...
        add = new QPushButton(this);
        add->setIconSize(QSize(16,16));
        add->setIcon(QIcon("add.png"));
        add->setGeometry(10,304,48,22);
        add->setText("Add");
        connect(add, SIGNAL(released()), this, SLOT(addMenuShow()));
    //...
    }
    
    void MainWindow::addMenuShow()
    {
        QMenu* menu = new QMenu(add);
        addFile = new QAction(tr("Add file…"), this);
        addFile->setShortcut(QKeySequence(tr("L")));
        menu->addAction(addFile);   
        QPoint relative;
        relative.setX(10);
        relative.setY(275);
        QPoint pos = menu->mapFrom(add, relative);
        menu->move(pos);
        menu->exec();
    
    }
    

    Thank you in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean add a menu to the QMenuBar from your QMainWindow ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      UnitScanU 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you mean add a menu to the QMenuBar from your QMainWindow ?

        UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on last edited by UnitScan
        #3

        @SGaist No, spawn this menù at the top of his parent, the button called "add". The menù spawns on "add" click

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That looks a bit strange and I haven't seen anything like that in any OS style guidelines. Why not use QPushButton::setMenu ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          UnitScanU 1 Reply Last reply
          0
          • SGaistS SGaist

            That looks a bit strange and I haven't seen anything like that in any OS style guidelines. Why not use QPushButton::setMenu ?

            UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on last edited by
            #5

            @SGaist Let me explain: I have a button called "add", and I have a menu item called "addFile" When the button "add" is clicked, I would make sure that the menu item "addFile" appeared above it, anchored at the top: how can I do that?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Use the position of your button as reference and set the position of your menu based on that.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • UnitScanU Offline
                UnitScanU Offline
                UnitScan
                wrote on last edited by UnitScan
                #7

                [SOLVED]

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    this->setAcceptDrops(true);
                //...
                    add = new QPushButton(this);
                    add->setIconSize(QSize(16,16));
                    add->setIcon(QIcon("add.png"));
                    add->setGeometry(10,304,48,22);
                    add->setText("Add");
                    connect(add, SIGNAL(released()), this, SLOT(addMenuShow()));
                //...
                }
                
                void MainWindow::addMenuShow()
                {
                    QMenu* menu = new QMenu(add);
                    addFile = new QAction(tr("Add file…"), this);
                    addFile->setShortcut(QKeySequence(tr("L")));
                    menu->addAction(addFile);
                    QSize s = menu->sizeHint();
                    menu->popup(add->mapToGlobal(QPoint(0,0-s.height())));
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Be aware that you are, sort of, leaking memory here. You are creating a new menu each time you click on the add button which will only be destroyed once the button is destroyed.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  UnitScanU 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Be aware that you are, sort of, leaking memory here. You are creating a new menu each time you click on the add button which will only be destroyed once the button is destroyed.

                    UnitScanU Offline
                    UnitScanU Offline
                    UnitScan
                    wrote on last edited by
                    #9
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        this->setAcceptDrops(true);
                    //...
                        add = new QPushButton(this);
                        add->setIconSize(QSize(16,16));
                        add->setIcon(QIcon("add.png"));
                        add->setGeometry(10,304,48,22);
                        add->setText("Add");
                    
                        bottomAdd = new QMenu(add);
                        addFile = new QAction(tr("Add file…"), this);
                        addFile->setShortcut(QKeySequence(tr("L")));
                        bottomAdd->addAction(addFile);
                        connect(addFile, SIGNAL(triggered()), this, SLOT(openmediaOnClick()));
                    //...
                    }
                    
                    void MainWindow::addMenuShow()
                    {
                        QSize size = bottomAdd->sizeHint();
                        bottomAdd->popup(add->mapToGlobal(QPoint(0,0-size.height())));
                    }
                    
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Why not use bottomAdd->height(); or its top left coordinate ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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