Two QWidget in QMainWindow to have minimize button and window title - c++

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
textEdit1 = new QTextEdit();
textEdit1->setWindowTitle("First Notepad");
textEdit2 = new QTextEdit();
textEdit2->setWindowTitle("First Notepad");
layout = new QVBoxLayout();
layout->addWidget(textEdit1);
layout->addWidget(textEdit2);
newTab = new QWidget();
newTab->setLayout(layout);
ui->setupUi(this);
setCentralWidget(newTab);
}
The above is my code sample of the MainWindow constructor. Ot has two qTextEdits which are in a VerticalBox layout. I want both the textEdits to have a title bar and minimize and maximize button so that at a time I can use one of them or both of them. But as you can see the output the Window Title bars are not there.
How can I make the Title bar appear? Why is it that setWindowTitle("First Notepad") do not display the Title?
If I am doing it wrong please suggest as what other way I can proceed. Any suggestion is welcome.
What I am trying is like one MainWindow having multiple sub-windows with fixed positions.

What I am trying is like one MainWindow having multiple sub-windows with fixed positions.
What you are looking for is probably a QMdiArea along with multiple QMdiSubWindow.
As mentioned in the documentation of `QMdiArea:
The QMdiArea widget provides an area in which MDI windows are displayed
Moreover:
QMdiArea is commonly used as the center widget in a QMainWindow to create MDI applications, but can also be placed in any layout.
I've used it, but I've never tried to give fixed positions to the subwindows. Anyway it's apparently possible. Probably QMdiArea::tileSubWindows is already enough for your requirements.
Set custom titles and bars to the windows is given for free instead:
QMdiSubWindow represents a top-level window in a QMdiArea, and consists of a title bar with window decorations, an internal widget, and (depending on the current style) a window frame and a size grip. QMdiSubWindow has its own layout, which consists of the title bar and a center area for the internal widget
See the official documentation for further details.

Related

Change QMainWindow layout programmatically in c++

I am using Creator to build main MainWindow and I populate it with all my widgets.
I do not set any MainWindow lay out in this stage (like "Lay out in a Grid" or "Lay out Horizontally".
When I launch the Application I want to dynamically change the MainWindow layout of widgets in it to "Lay out in a Grid" like in Creator mode by pressing the left button.
I’ve tried hard all possible combinations reading many posts around.
this solution:
Qt: Can't set layout in QMainWindow
doesn't work and it does not make much sense to me.
I've tried:
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
centralWidget()->setLayout(MainWindowLayout);
NO LUCK
I've tried to put all my widgets inside a big widget at desegn time named MainWindowWidget and then setting it as a centralWidget
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
setCentralWidget(ui->MainWindowWidget);
centralWidget()->setLayout(MainWindowLayout);
NO LUCK
Ain't there any way to change the MainWindow widget's layout like "Lay ouy in a Grid" at design time when using the Creator??
EDIT:
To be more specific with NO LUCK I mean that the widgets are not placed as in a grid as expected.
Here is a code snipped that you can try on an empty application
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
/*
Place some widgets at design time with Creator (at least 2) in the MainWindow form
in a misplaced order and do not apply any "Lay out xxx" right button on QT Creator
*/
ui->setupUi(this);
/* HERE I WANT THE MainWindow or either an Object to take a specific Layout */
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
centralWidget()->setLayout(MainWindowLayout);
}
It is almost 2 days that I am googling and I can't find any way out
Thank you all for your help...
You are creating the layout But you are not adding widgets to it. This should fix your issue:
ui->setupUi(this);
QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->label, 0, 0);
MainWindowLayout->addWidget(ui->label_2, 0, 1);
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);
#C137
Finally I got it working doing the following:
I places all my form widgets into 3 different widgets (containers QFrame in my case).
Then I placed them into the Layout as suggested and it worked.
This solution is a bit tricky
QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->MainFrame, 0, 0); // MainFrame --> My new object containing other widgets
MainWindowLayout->addWidget(ui->DebugButtonsFrame, 0, 1); // DebugButtonsFrame as above
MainWindowLayout->addWidget(ui->DebugMemoFrame, 1, 0); // DebugMemoFrame as above
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);
QT is handling this task in its own way a bit confusing from my point of view. While I was using Embarcadero the Layout were much much easier to manage.
I thought there could me a method to easily set the MainWindow Layout as in Creator mode which was much much easier and faster to handle.
So far it worked as expected but still confusing.
Thank you all for the support.

Modifying the QDockWidget default snap locations?

I am trying to model a UI from a wxWidgets application to QT. However, in the original application they have Dock Widgets that can snap in place and move around except for occupying the main window.
Every box appears to be a dock widget except for the main window. And if I want to move the Datasets window (top left) to the right of the Output Adjustment window I can:
So now my goal was to try and get the UI in QT to at least accomplish the dock widgets on the left side of the main widget. Which in my case, I was able to at least get the width and the height with some trouble to show up the same. However, I cannot create a QDockWidget like the Output Adjustment and have it snap next to the others. And the other problem comes when I try to move a dock widget it will not snap to the right or the left of the other widgets.
This is where I have my problems. I cannot snap it to either side of the dock widgets. Is this something that will need to be hard coded? I know I have seen other examples online that look awfully similar to this layout. However they are all hard coded. And it would be a lot more simple if I could just use the creator to add the sliders and toolbars as time goes on. And if it is not something I have to hard code, how do I modify the locations on where my widgets can snap? I tried to make a grid layout, however the widgets do not go into the grid.
By enabling dock nesting in your main window you can achieve a more flexible placement of the dock widgets, including positioning the dock widgets side-by-side. In Qt Designer, select the main window and in the Property Editor panel mark the dockNestingEnabled check box (present in the QMainWindow section). Alternatively, you can achieve the same result by calling the method QMainWindow::setDockNestingEnabled in your code.
If you want your application to start with dock widgets arranged side-by-side as in your reference application, you have to resort on the method QMainWindow::splitDockWidget. For example, you can create an initial arrangement in Qt Designer such as the one depicted below.
Next, you can rearrange the dock widgets in the main window constructor code,
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// Changes the dock widgets arrangement.
splitDockWidget(ui->dock1, ui->dock3, Qt::Orientation::Horizontal);
splitDockWidget(ui->dock1, ui->dock2, Qt::Orientation::Vertical);
}
which would produce the result below.

Qt 'glue' two widgets together

I have two widgets (both QFrames), none of them have any title bar associated with them (which I achieve through setWindowFlags(Qt::FramelessWindowHint)). One of them is a main widget, and the other a sidebar sort of widget, which is supposed to stick to it at its right boundary (its height being approximately 1/4th of the main widget).
I cannot keep them both in a transparent QFrame with static positioning, since the main widget is draggable through its top (since title bar is missing on it, I do it manually by intercepting mousepress/mousemove events and moving it accordingly). The custom drag on the main widget works fine, but when I try to move the sidebar along with, a very obvious visual delay shows up between the two, there are momentary gaps visible between the two while dragging the main widget to the left, or momentary overlapping between the two when dragging the main widget to the right (the sidebar is not draggable, no drag logic is implemented for it).
How do I 'glue' these two widgets together, such that they move together all the time without any delay? I browsed the Qt docs, it may be possible that QDockWidget can help here, but I could not understand how. The main widget here is not a QMainWindow.
Platform - OS X Yosemite, Qt 5.3.1, 32 bit.
You should definitely use QDockWidget here.
Make your "main widget" derive from QMainWindow rather than QFrame (it may not be "obvious" as QMainWindow does not derive from QFrame, but it should not be such a big deal).
Then, encapsulate your second widget within a QDockWidget and dock it in the main widget like that:
// secondWidget being your QFrame based widget
// mainWidget being your "main widget"
QDockWidget* dockingBar = new QDockWidget("My bar", mainWidget );
dockingBar->setWidget( secondWidget );
// dock on left side, change first parameter to dock somewhere else:
mainWidget->addDockWidget( Qt::LeftDockWidgetArea, dockingBar );
An alternative is to create a third widget that would become your top-level widget and use a QLayout to insert your two QFrames in this new one:
QWidget* newTopLevelWidget = new QWidget();
// QHBoxLayout to have mainWidget on the left hand side of secondWidget
// Replace by QVBoxLayout to have mainWidget on top of secondWidget
QLayout* layout = new QHBoxLayout( newTopLevelWidget );
layout->addWidget( mainWidget );
layout->addWidget( secondWidget );

Qt Creator Align button float to the top

I have this gridlayout added onto my centralWidget of my UI in Qt Creator. And I wanted to add several buttons onto the gridlayout. My code is as follows:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *button1 = new QPushButton();
button1->setText("hello");
ui->gridLayout->addWidget(button1, 0,0);
}
MainWindow::~MainWindow()
{
delete ui;
}
Even though the button is added, it is seen appearing in the middle of the UI when the program is run. And there is a huge gap between the menu bar and button. How do i go about adding the button so that the button will flow nicely below the menu bar?
Please advice.
Thanks
The grid layout was placed in the middle of the UI in design mode, so anything you place inside will fill the fixed rectangle of the grid layout.
To make the grid layout fill the main window, right click the main window and click Lay out > Lay out vertically.
You can also fix the huge gap by placing a vertical spacer below the grid layout.
I guess this method work as well. By setting the alignment for the addWidget portion.
eg: ui->gridLayout->addWidget(button1, 0,0, Qt::AlignTop);

Placing buttons at the center in horizontal line in QT

I am new QT and trying to develop the desktop application.
Currently I am facing a issue of alignment. I am using QTCreator 3.1.2 based on qt 5.3.1
I have 3 buttons placed in the windows as
After running application, If I resize the windows then buttons didn't stay in the center. like image 1 and if size is less then it is like image 2
I have tried using the hbox, but it didn't solve the problem and also scroll bar is also not visible in the window.
Would you please tell me how can I make these buttons to stay in the center only?
Thanks a lot
You can create QHBoxLayout passing this as its parent, which will set that layout as the layout of that widget, then add QPushButtons to that layout:
Widget::Widget(QWidget *parent) : QWidget(parent) {
// Prepare the horizonal layout, adding buttons
horizontalLayout = new QHBoxLayout(this);
pushButton = new QPushButton(this);
horizontalLayout->addWidget(pushButton);
pushButton_2 = new QPushButton(this);
horizontalLayout->addWidget(pushButton_2);
pushButton_3 = new QPushButton(this);
horizontalLayout->addWidget(pushButton_3);
// Set the layout of the central widget
setLayout(horizontalLayout);
}