Truncated tab bar in Qt - 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.

Related

Qt: Tab Icon when QDockWidget becomes docked

Qt's QMainWindow has a ability to dock windows derived from QDockWidget. It also would put one on top on the other if few of them are stacked, producing a tab bar. Whenever a QDockWidget's state changes a signal topLevelChanged() is emmitted. At this point I would like to get access to underlying QTabWidget to set an icon for a a newly added tab. How can I do it? My patience is over trying to dig the answer out from Qt's documentation and source code. Thank you in advance.
So icon I want to be on Contents/Index tabs.
Once at least one dockwidget has been tabified, the main-window will create a QTabBar to provide the dock-tabs. Each dock-area can have its own tab-bar. These tab-bars will become children of the main-window, so you can use findChildren() or children() to get references to them.
The main difficulty will be in finding which dock-widget belongs to which tab and in which tab-bar. If the dock-widget window-titles are all unique, you can just search using the tabText(). Otherwise, you might be able to use the tabData(), which Qt sets internally to a quintptr from the dock-widget.
Once you have the correct tab, you can of course use setTabIcon() to add your icon. But note that every time a dock-widget is untabified or moved to another tab-bar, the icon will be lost.

How to have common control in QTabWidget

I want to use a common control (QTreeView) in two different tab pages in QTabWidget.
how to do this ?
I added a tabwidget and controls in tab pages in Qt designer.
using qt creator version 2.4.1 in Win 7.
You can't have the same QTreeView in two different QTabWidget pages. When you add any widget to a layout, that layout takes ownership of the widget. Since there can only be one owner, you're stuck with one parent per widget.
But you can fake it. Give your main page a grid layout. Put a QTabBar running along the top, your QTreeView on the left (or wherever), and a QStackedLayout on the right. Connect the tab bar and the stacked layout so changing tabs in the bar changes the visible page in the stack layout.
That should be just what you're looking for - just be prepared to fight with QTabBar to get it to display like you want it to...
Alternatively, just live with having two separate tree views - they'll both be viewing the same model, after all, so the bulk of the data won't be duplicated. Saves you a fight with QTabBar, too.
Hope that helps!

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.

QLabel embedding in QStatusBar using Qt Designer

Is there any solution to embed a QLabel in QStatusBar using Qt Designer?
I don't believe so. It's fairly simple to add one programmatically, though.
If you're just wanting to show a message, you could use: statusBar()->showMessage(tr("Message Here"));, or alternatively if you really needed a QLabel on the status bar, you could do something along the lines of:
QLabel *label = new QLabel("Message");
statusBar()->addWidget(label);
label would become a child of statusBar(), and appear in the first empty spot from the bottom left (addPermanentWidget(label) would add it to the first empty spot from the bottom right). If you place QLabel label in the classes header (or other var name), you'd be able to access the variable directly later (removing the initial QLabel type from the first line, of course).
It is not possible with Qt Designer. I resolve it by creating label a in Qt Designer and later in constructor of my MainWindows add this line:
Ui::"class name of my MainWindows"::"name of statusBar Object"->addWidget("Object Name of Label");
In my application, the class name of mainwindows is MainWindowsForm, the status bar is named statusBar and the label is named informationLabel. Then I have:
Ui::MainWindowsForm::statusBar->addWidget(informationLabel);
It's not possible even if you would manually edit UI file.

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