Using layout of QMenuBar - qt

Is it possible to get layout from QMenuBar object and add items to it. What I am trying to do is a menu bar containing custom widgets (a clock and login/off widget) on the right.
This code crashes.
QPushButton *b1 = new QPushButton("Button",ui->menuBar);
QHBoxLayout *rlayout = new QHBoxLayout(this);
ui->menuBar->layout()->addItem(rlayout);
rlayout->addWidget(b1);
b1->show();
Is there any other way?
Best regards,
Valentin Heinitz

Would it be possible to create your own container widget, put the menu bar in on the left, and the other widgets in on the right? That should get you similar functionality and appearance to the Qt3 version of the menu bar.

You should look into QWidgetAction, that allows you to insert custom widget in a menubar. You would then have to call QMenuBar::addAction(QAction*) to put your QWidgetAction in the menubar.
I know that it is not part of your question, but maybe QDockWidget would do a better job for what you need??
Hope this helps.

Related

Truncated tab bar in Qt

On this site
there is a picture with two "tab-systems".
I would like to have the second one, which is described as "A truncated tab bar shown in the Plastique widget style." There you have arrows, which allow you to slide through the tabs.
I have implented a "tab-system" which looks like this:
QTabWidget *tabWidget = new QTabWidget();
tabWidget->addTab(ToolGroupBox(),"Toolbox");
tabWidget->addTab(CameraGroupBox(),"Camera");
...
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tabWidget);
As I understand it is possible to create the truncated tabs if I use the tabRect-function of the QTabBar-class. I have tried several things but unfortunately its not working.
You want the usesScrollButtons property of the QTabBar. You can access the QTabBar of your QTabWidget via the tabBar method.
You don't need to get involved with tabRect at all. The documentation just before the picture of the two tab systems is not directly related to it. The figure is just showing you two possible tab styles.

How can I add a QLineEdit to Menubar

I am attempting to reclaim some screen real estate in my application. I've got a search bar that is a basic QLineEdit and takes up space. In my menu bar, I easily have enough room to fit this search box, but I can't figure out how to get the LineEdit into the menubar.
Can someone assist me in getting this added to a menubar?
I am using Qt 4.7.
Here is an image of what I am attempting to accomplish. It's fairly basic image, but I'm looking to use the right half of the menubar as a search box.
Use QWidgetAction. QWidgetAction is for inserting custom widgets into action based containers, such as toolbars.
here is an example to add a progressbar to menu bar :
QWidgetAction *widgetAction = new QWidgetAction(this);
widgetAction->setDefaultWidget(new QProgressBar(this));
menubar.addAction(widgetAction);
You could use
void QMenuBar::setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )
to add your widget in the menu.

QT and custom menu item (QLineEdit)

How can I use QLineEdit as a menu item in QMenu? Is there any solutions to do this?
Just to do something like this (look at the edit box under the avatar):
You can use a QWidgetAction to contain your QLineEdit. It inherits QAction.
Take a look at QComboBox. It allows editing of items in a pop-up menu. But if you mean the menu in the main menu bar, I don't think there's a way to do it. And it's not a good UI anyway.

QT How to remove the action menu item

when i add the widget to the main window, by default the action menu item will be present,
how to remove that?
menuBar()->setVisible(false);
verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);
connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));
exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));
Thanks
If you want to hide an QAction and display it when you need it, you can use the setVisible function.
If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.
[Edit]
If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);
I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....
You can do this in the Design section (not in code).
Then you see your Object-Stack on the right side like
MainWindow QMainWindow
centralWidget QWidget
webView QWebView
Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.
In order to remove the default context menu with the label "Actions" the following code may be used:
// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
Essentially, the same as the Joel's answer, but the code version :)
(Code taken from QFriendFeed sample from forum.nokia.com)

QStackedWidget inside QTabWidget tab?

In my QT application I use a QTabWidget for the base navigation. This QTabWidget I setup in the ui. In some of the tabs of the QTabWidget I need to have QStackedWidget to be able to "drill down in the view".
I tried adding the QStackedWidget inside the ui also but it automatically adds a page to the stack. I want to add the pages for the QStackedWidget in code instead. If I in the code try to do this the stackedWidget already have a standard page so myWidget will be the second in the stack.
MyWidget *myWidget = new MyWidget(ui.stackedWidget);
ui.stackedWidget->addWidget(myWidget);
What is the best and easiest way to setup a QStackedWidget inside QTabWidget tab?
How about:
QTabWidget *myTabWidget = new QTabWidget(this);
QStackedWidget *myStackedWidget = new QStackedWidget(myTabWidget);
myTabWidget->addTab(myStackedWidget, "Stacked Widget");
Also you can remove all existing stack pages in Qt's Designer/Creator. Just right-click on the stacked widget and remove all existing pages. Then you can add the needed pages in the code using addWidget().
I'd say - create it in ui, just like you do (this way it's easier to layout/position, add other widgets on the tab later, etc), but simply remove all existing pages (added by designer) from code and add your new ones.
Actually Designer from Qt 4.6 allows to delete all pages from stacked widget - you need to right click, go to submenu "Page X of Y", and choose Delete. Repeat until all pages are gone :)
Maybe this got added to the Designer just recently, so you may still need to remove them from the code if you have an earlier version of Qt.
Speaking of keeping stuff inside ui against keeping it in code i'd vote for "as much in UI-file as possible" :)

Resources