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. [Solved] How can I add a QLineEdit to Menubar?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How can I add a QLineEdit to Menubar?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 9.8k 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.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    Hi Developers,

    I am just trying to create a search bar kind of application by using a QLineEdit. In my Menu Bar i have enough space to fit this search Bar(QLlineEdit). But unfortunately i can't figure it out, how to get the QLineEdit into the Menu Bar. Can anyone please suggest me regarding this?

    Thanks & Regards

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #2

      This "thread":http://www.qtcentre.org/threads/41252-how-to-add-widget-to-QMenubar and this "thread":http://stackoverflow.com/questions/3915327/adding-custom-widgets-to-qmenubar may help you.

      I have not tried them though as I only tried adding QLineEdit to the MainToolbar. I've never seen any GUIs with a QLineEdit on the QMenuBar. Good Luck!

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        "QWidgetAction":http://doc.qt.digia.com/qt/qwidgetaction.html in theory allows you to put a widget anywhere you can put a QAction, in a menu for example:

        @
        #include <QtGui>

        class MainWindow : public QMainWindow
        {
        public:
        MainWindow(QWidget *parent = 0) : QMainWindow(parent){
        QWidgetAction *widgetAction=new QWidgetAction(this);
        widgetAction->setDefaultWidget(new QLineEdit(this));

            QMenu *fm=menuBar()->addMenu("File");
            fm->addAction(widgetAction);
        }
        

        };

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
        }
        @

        However, on MacOSX you won't be able to interact with the widget (see docs) so its not great for cross compatibility/multi platform apps.

        Also, I'm not sure that you can actually put a widget into the QMenuBar itself (i.e File|Edit|View|[search box]), the following certainly doesn't work for me:

        @
        menuBar().addAction(widgetAction);
        @

        Maybe someone else has had some luck? Convention would have you place this in the windows toolbar anyhow.

        Finally, if you really do want to have "File|Edit|View|[search box]" then the following might help, it even works on MacOSX (by breaking the native behavior I hasten to add):

        @
        #include <QtGui>

        class Widget : public QWidget
        {
        public:
        Widget(QWidget *parent = 0) : QWidget(parent){
        resize(640,480);

            QVBoxLayout *l=new QVBoxLayout(this);
            l->setAlignment(Qt::AlignTop);
            l->setContentsMargins(0,0,0,0);
            l->setSpacing(0);
        
            QHBoxLayout *ml=new QHBoxLayout;
            l->addLayout(ml);
        
            menuBar=new QMenuBar(this);
        
            QMenu *fm=menuBar->addMenu("&File");
            fm->addAction(new QAction("Item 1", this));
            fm->addAction(new QAction("Item 2", this));
            fm->addAction(new QAction("Item 3", this));
        
            QMenu *em=menuBar->addMenu("&Edit");
            em->addAction(new QAction("Item 1", this));
            em->addAction(new QAction("Item 2", this));
            em->addAction(new QAction("Item 3", this));
        
            QMenu *vm=menuBar->addMenu("&View");
            vm->addAction(new QAction("Item 1", this));
            vm->addAction(new QAction("Item 2", this));
            vm->addAction(new QAction("Item 3", this));
        
            ml->addWidget(menuBar);
        
            search=new QLineEdit(this);
            search->setPlaceholderText("Search...");
            search->setMaximumWidth(200);
            ml->addWidget(search);
        
            setFocus();
        }
        QMenuBar *menuBar;
        QLineEdit *search;
        

        };

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);

        #ifdef Q_WS_MAC
        a.setAttribute(Qt::AA_DontUseNativeMenuBar, true);
        #endif
        
        Widget w;
        w.show();   
        return a.exec&#40;&#41;;
        

        }
        @

        Hope (all) this helps ;o)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Macro
          wrote on last edited by
          #4

          Hi Jazzycamel...

          First of all, my applogies for late reply. Your code works fine... :-) Thanks Jazzycamel..

          Thanks & Regards

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Macro
            wrote on last edited by
            #5

            Thanks for your reply Code_ReaQtor....

            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