[Solved] How can I add a QLineEdit to Menubar?
-
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
-
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!
-
"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();
}
@Hope (all) this helps ;o)
-
Hi Jazzycamel...
First of all, my applogies for late reply. Your code works fine... :-) Thanks Jazzycamel..
Thanks & Regards
-
Thanks for your reply Code_ReaQtor....