Close button on QTabWidget not the Tabs in the QTabWidget - c++

I am using Qt-5.8 over Ubuntu.
This is how my QTabWidget appears :
And the black dot is where I want the Minimize Button to happen.
One way I came across is that I can use QToolButton *qToolButton to create a new button and tabWidget->setCornerWidget(qToolButton) and then add the implementation over its click event.
But should there not be any other way to just show the minimize button as like in MainWindow or SubWindows has. Which just minimizes it.

Minimize button on top panel of your QMainWindow's instance is part of Windows Manager subsystem of your OS. So you can't use similar approach inside your window as toolbox with buttons, etc.
As you wrote, try to use tabWidget->setCornerWidget(qToolButton) to place your custom minimize button inside your window.

Related

Is there a way to attach or anchor two QWidgets together?

I'm getting started with Qt and decided to build a full-screen text editor. I want to have a button (button with arrow in screenshot) attached to a QDockWidget which opens and closes it so the button is always visible to the right side of the screen and stay anchored to it when dock is visible or resized.
My current app is a simple fullscreen textEdit set to centeralwidget in Mainwindow.
I haven't found a way to do this yet with layouts or existing addAnchor() functions so any help or direction is appreciated.
You can achieve what you want by using a container for your text edit and the button. A QWidget instance can be used as an "invisible"*** container for other widgets.
So in Qt Designer you add a widget as a central widget of the main-window, inside this widget you add the text edit and the button, then you set a vertical layout for this container widget.
Don't forget to restrict the docking widget to only dock to the right side, you can do that with: dock->setAllowedAreas(Qt::DockWidgetArea::RightDockWidgetArea); //assuming dock is the pointer to your QDockWidget.
In case you want the dockWidget to be able to dock to any side and the button to follow, you can do that too, but it get a little bit more complicated. Basically you need to connect a slot to dockLocationChanged of your dockWidget and based on where it's docked you need to set-up a new layout for the container widget to be vertical or horizontal and the order of the textEdit and the button based on the side the dock happened.
LE:*** you will most likely need to set the margins you want, since both the widget and it's layout can have them and the actual content might have higher spacing than you want.

How to change UI without opening a new window in Qt?

In my program, I'm moving from QMainWindow to QDialog on a press of a button.
I want to do the same without opening a new window and be able to move between the UI's.
The Target device will have a very small touchscreen, so I want my UI to sit still and require minimal repositioning.
Please point me in the right direction or give me an example on How-to.
To do that, you can use a QStackedWidget.
From the documentation:
The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.
Instead of opening a new window, push its content on top of the stack and pop it when you want to (let me say) close the window.
Each widget is a page of your application and no separate window is required. You can design them as you would design a central widget of a normal window or dialog.

How to use my custom minimize/close window buttons in my QMainWindow?

I would like to remove the windows title bar (and minimize/maximize/close buttons) of my QMainWindow. And I would like to use my own minimize/close buttons.
I know how to remove minimize/maximize/close window buttons. It's not a problem.
How to remove the window border (containing minimize, maximize and close buttons) from a Qt widget?
The problem is to add those new buttons (minimize/close). Is it possible to do that in Qt Style Sheet (or in C++) ?
ps: I would like something portable for Windows/Mac/Linux
Unfortunately that kind of decoration depends on the windows manager. You may find OS dependant solutions (this for Windows changes other aspects of the title bar, for example), but if you remove the window buttons, then the only portable option I know is to create your own container window, a QWidget that has a header region with the buttons you want and a centralWiget where you can add the rest of you application:
You then connect header buttons
connect(m_ui.btnMinimize, SIGNAL(clicked()), SLOT(showMinimized()));
connect(m_ui.btnClose, SIGNAL(clicked()), SLOT(close()));
To make it reusable, just add an insert(QWidget*) method to replace the centralWidget.
Note: take into consideration that you must create the container with the Qt::FramelessWindowHint flag set, therefore if you want to allow the user to move/resize the window, you must take care of those actions manually too.
You should create window without system bar (without minimize, full-screen, close buttons you can use the next flags for that Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint) and add your own customized system buttons.

Difference between QPushButton and QToolButton

I'm new to Qt and the difference between QPushButton and QToolButton is not so clear to me.
I know that a QToolButton is usually used in a QToolBar and it usually shows only an icon, without text, but I don't quite understand the main difference between both.
Does it have any bigger difference?
When should I use QPushButton and when should I use QToolButton?
I would like to know this to use the most appropriate button, and I need to perform some GUI tests and maybe it can be relevant.
QPushButton is simply a button. QToolButton is part of a group of widgets in the QtWidgets module that operate on QActions: QMenu and QToolBar are other examples. As a result, QToolButton is much more complex under the hood than QPushButton.
Some examples of how they are different in practice:
QToolButton is tightly integrated with QAction. Changing the icon, text, or other properties of a tool button's default action is reflected on the button.
You can change the layout of the tool button contents (icon only, text only, text beside icon, text below icon). This is not possible for a QPushButton.
QToolButton supports a "split" button type: a sidebar hot zone opens a menu instead of triggering the default action.
Tool buttons can be created directly in a QToolBar by adding an action. Other widgets must be explicitly added to the toolbar.
A tool button is meant to be displayed in a grid, so it has smaller default internal margins than a push button.
QPushButton is more for "Ok"/"Close" type buttons that contain text with an optional icon.
A QToolButton should generally have an icon.
A QPushButton should always have text.
From Qt doc: http://doc.qt.io/qt-5/qtoolbutton.html#details
"A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead."
When i want a button in the GUI simple with only an icon, I use QToolButton. But when i want a classic button, i use QPushButton.
No big differences,

Qt/C++ Technique To Show Inline Dialog

I have QtCreator 5.5 for the Mac, using C++, and I'm new at this. I have a MainForm window. I want to show a dialog inside that but draw it separately in its own UI designer. Then, at runtime, my code will pull that other UI and show it inside that window.
Imagine for instance a main application window. You click a button and you need to see a confirmation dialog. Normally you could do that with a separate window. Instead, I want it to be inline at runtime, to show it like a widget on the main window.
Sure, I could draw my dialog on top my existing window in a widget, but that defeats the point where I want to be able to adjust it in its own designer window, and just load it as necessary in the main window when I need it.
What's the technique in QtCreator and C++ to do this?
Open QtCreator. This task can only be done through there.
Assuming you have a regular Qt Widget Application, you need to just add a QWidget control on the MainWindow's centralWidget in the UI Designer.
Next, click Edit in the UI Designer and then rightclick Forms. Choose Add New...
In the dialog that opens up, choose Qt > Qt Designer Form Class, and click Choose...
Select Widget and then go with the defaults on the rest. This creates a widget called Form, and the form.h and form.cpp will also be added to your project.
Now find the form.ui and open it in Qt Designer. Add a Stacked Widget Control on the QWidget form. Size it to the same size as the parent widget. Next, stick a Label Control on the Stacked Widget Control's first page. This label will be temporary just to prove that this works. You'll set it to the text of "Testing". Now close the designer.
Flipping back to your UI File of your Main Window, find that QWidget you added and rightclick it. Choose Promote... and a dialog box appears. In that dialog, set Promoted Class Name to Form and then set Header File to form.h. Leave everything else on defaults and click Add and then Promote.
You may be expecting to see an immediate change in your Main Window in that QWidget that you added previously. However, that's not how this works unfortunately. You only see the change at runtime. So, now you should Run your application in Qt Creator. You'll see when it runs that your widget now contains the contents of that form.ui component you created.
Now, here's where it gets interesting. You can now draw all your inline modal dialogs on that form.ui, just putting them on different pages of your Stacked Widget control. You can also set the properties of your widget so that the background of the QWidget container is opaque and dark (creating a background dimmer) and then, at runtime, resize the QWidget to the size of your Main Window. When you need to switch to a different dialog, you can use setCurrentWidget() or setCurrentIndex() on that StackedWidget item like so:
QStackedWidget *w = ui->widget->findChild<QStackedWidget *>("stackedWidget");
w->setCurrentIndex(1);
In that example, I had a QWidget control on my MainWindow named simply widget. I then found the control on it called stackedWidget. Once I had it, I used the setCurrentIndex(x) method on it to change the page of that widget to show the one I needed. Remember that this is a zero-based index. So, (1) would be page 2.
QMainWindow::setCentralWidget
Inside your QMainWindow constructor add:
MyDialogClass myDialog = new MyDialogClass();
setCentralWidget(myDialog);