Using Qt To Build A Full Screen Layout - c++

I am trying to use qt along with the qt designer to create a simple full screen webview with a line edit and a button above to create a really simple browser.
The problem is that the layout doesn't want to expand to fill all of the available space. I seem to think that I am missing something really simple, but I just can't seem to figure it out.
Here is an overview of my layout
<MainWindow>
<GridView>
<VBoxLayout>
<HBoxLayout>
<lineEdit /><PushButton />
</HBoxLayout>
<WebView />
</VBoxLayout>
</GridView>
</MainWindow>
Here is the code on the MainWindow Class
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->showFullScreen();
ui->pushButton->showFullScreen();
ui->webView->load(QUrl("http://google.com"));
ui->webView->showFullScreen();
}
and here is the main code
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
It comes out looking like this instead of the webview and lineedit filling the whole amount of space
Is there anyway of doing this in Qt, I would have assumed that this was a standard thing to want to do.
Many Thanks in advance!

I think you have to replace the GridView by the VBoxLayout directly above.
What is the grid view you use exactly ?
<MainWindow>
<VBoxLayout>
<HBoxLayout>
<lineEdit /><PushButton />
</HBoxLayout>
<WebView />
</VBoxLayout>
</MainWindow>

Make sure your Object Property "sizePolicy" is set Maximum and "maximumSize" is 16777215, then w.showFullscreen should work

If you mean that you want the window to fill the entire screen, hiding the task bar and title bar, you should replace this:
w.showMaximized();
with this:
w.showFullScreen();
If you instead mean that you want your line edit and web view widgets to fill the window that contains them, "full screen" doesn't mean what you think it does. Your problem is probably related to your layouts in designer, and we don't have enough information in your original post to properly diagnose it. Given how simple your UI is at this point, the easiest thing to do is probably break all layouts and start over.
Whatever it is that you want, you definitely shouldn't be calling showFullScreen() on your line edit, push button, or web view.

I suspect the problem is that you've set up the BoxLayout inside the parent widget, rather than setting the parent widget to use a BoxLayout. In Qt Designer, you should be able to right-click on the parent widget and set the layout, and it will expand like you want. If you just add a BoxLayout to a widget from the toolbar, it won't automatically expand to match its parent.
See this link for more details: http://embrisk.com/notes/qt_resize.html

Expanding upon Stephen Bell's answer (I'd comment but I lack the reputation), as it took me awhile to figure this out as well.
If you have completely designed the layout you wish to have fullscreen, right-click on the "Main Window" in the far right of the screen. A pull-down should appear. Click Layout, then Grid Layout. Main Window should assume a grid layout. It should not change the positioning of your objects at all in Qt Designer. Yet, it should fix the fullscreen.
If MainWindow does not have a layout, your layout will likely have whitespace or deadspace without an explanation.

Related

QT Window Modality disables actions in menu

I am developing a desktop application on macOS. I have a class that is a subclass of QMainWindow. Inside this window there are many dockwidgets. I need to set WindowModality to WindowModal, so user cannot interact with other opened windows. But my window has a menubar with many menus that have some QActions inside and when I setWindowModality(Qt::WindowModal) it automatically disables every action in menu and I need them to be enabled.
Can anybody provide some simple solution for this? Or it is not possible?
Thank you very much.
EDIT:
I have many windows in my application. I have a real main window from which you can open another window and also from that window another window. And here is the situation where i need my child windows to be modal. But they also have their own menubar which gets automatically disabled when window modality is turned on. Ive been googling it already maybe for 10 hours without any solution. I cannot test it but I guess that on windows the menubar would not disable because the native menu is quite different.
If there is no specific need of using QWindow, then it'll be easier to achive this using QDialog class instead. Using QDialog you can simply show dialog as modal using exec() method.
EDIT: Basically, you can add QMenuBar element to every QLayout class object using QLayout::setMenuBar method.
If you want to add menu bar to QDialog, you've got to set layout for your dialog, then programatically create desired QMenuBar object and pass it to QDialog layout (which you can access using QDialog::layout method). Simple example below:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QMenuBar* menu = new QMenuBar();
QMenu* fileMenu = menu->addMenu("File"); //Create 'File' menu inside menu bar
QAction* closeAction = fileMenu->addAction("Close"); //Create 'Close' action in 'File' menu
connect(closeAction, QAction::triggered, this, close); //Close dialog after triggering 'Close' action
layout()->setMenuBar(menu); //Add menu bar to QDialog layout
}
Please consider the usage of Qt::ApplicationModal.
This keeps the modality but gives you other behaviour on MAC.

Creating, handling and switching between windows QTCreator

My research into the question suggests that I'm somewhat beating a dead horse, however I can't seem to get a conclusive answer.
I'm using QT Creator to create a GUI that will help interface with a register of units(simple objects, with some ID's and such).
I have a Main Menu, which contains 5 push buttons and a table. Pictured here. My project currently includes these files, and my main currently looks like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
UnitRegister e;
CommInterface c;
w.setRegistryPtr(e);
w.setCommPtr(c);
w.setTablePtr(w.findChild<QTableWidget*>("unitTable"));
w.show();
return a.exec();
}
What I want to do is press one of the four push-buttons on the left, and use that to switch to a different view in the main window.
For this purpose I considered using a QStackedWidget, and then having a page for each menu button. My question is this:
How do I/Should I create a separate QT Item Form Class(header, .cpp
and .ui file) for each page
How do I switch between the pages I've added using the QT Designer.
How do I ensure that the different pages can access the UnitRegister created in my main file.
I'm not necessarily searching for a complete answer, but something to get me going.
Switching Pages: Consider a QButtonGroup to give your buttons ids that you can map to the indexes of your QStackedWidget. Then you can do this:
connect(buttonGroup, SIGNAL(buttonClicked(int)), stackedWidget, SLOT(setCurrentIndex(int)));
Organizing the Pages: Create a .ui file and corresponding container widget for each page in your widget stack. This is much easier than one massive .ui file.
Accessing the UnitRegister: There are tons of ways to do this. Adding a setter function to your classes is one way.

Open a QFileDialog over a QQuickItem

I want to open a QFileDialog over a QQuickItem.
void
MyCoolQQuickItem::loadFileDialog()
{
QString filename = QFileDialog::getOpenFileName(this, "Open a file",
"C:\\path to my stuff\\", "*.*");
if(filename.size()>0) {
// load file and do stuff
}
}
My MyCoolQQuickItem is a Subclass of QQuickItem.
And QFileDialog::getOpenFileName need a QWidget* as parent. If I pass a null_ptr instead, the dialog opens correctly as modal dialog. But after I close the dialog the wrong window gets the focus.
I tried to pass the window instead(this->window(), but I have a QQuickWindow, what also could not be parsed in a QWidget.
I could use the QML File-Dialog instead.
http://qt-project.org/doc/qt-5/qml-qtquick-dialogs-filedialog.html
But I want to use a QFileDialog here.
http://qt-project.org/doc/qt-5/qfiledialog.html
Any Idea how I can solve this?
Just use the QML version, it is absolutely the same dialog you will get from the QWidget based dialog. The only thing you will gain from using a QWidget based dialog is you will make it backward and you will drag several MBs for the Qt5Widgets library.
Whatever you think you might be gaining from wanting to do that, it will not be worthy the effort to do it. The old QtQuick1 offered a proxy component to show widgets in QML, but this is not available with QtQuick2, because it has a different approach towards rendering.

How do you adjust the window decoration size on a QMainWindow?

I was wondering how you adjust the size (particularly height) of the window decoration on a QMainWindow. I want behaviour similar to the QtDesigner, Xcode and Skype. I tried adding a QToolBar with the following code:
QMainWindow *mainWindow = new QMainWindow();
QToolBar *bar = new QToolBar();
bar->setFixedHeight(100);
bar->setFloatable(false);
bar->setMovable(false);
mainWindow->addToolBar(bar);
mainWindow->show();
Compared to what I want it is still a long way off - :
On the left you can see what I want and on the right you can see what I currently have.
Is this possible in Qt? If yes, how would this effect be accomplished?
The solution was simple. All I had to do is call:
mainWindow->setUnifiedTitleAndToolBarOnMac(true);
Problem solved!
Edit, 2013-08-19:
It appears that as of Qt5, you need to pull this git repository and build the library - then link it to your project. In order to set the unified title and toolbar, see the provided examples in the repository.

Creating a tree-view with buttons? in QT

I am trying to make a dialog box like below in QT, the only problem is I have no idea what the widget is called. The bar on the left is like a tree-view widget, but when you click on it, it updates the text on the right. Does anybody happen to know what the widget is called or what widget(s) are required to perform this? I am using QT C++ on Windows.
There is an example with Qt showing you how to do this.
https://doc-snapshots.qt.io/4.8/dialogs-configdialog.html
If you're using Qt Creator as IDE, you can find it under the "Demos and Examples" tab in the Welcome Screen too.
It uses a QListWidget for the selector, and QStackedWidget to control the different pages. Connect the currentItemChanged signal of the list widget to change what page should be shown. Everything you'll need is in configdialog.cpp.
If you realy need to add QPushButton into QListWidget, use setItemWidget, or into ListView use QAbstractItemView::setIndexWidget