How to change the Title of the window in Qt? - c++

How to change the title of the window in Qt? (Both for QDialog and QMainWindow.)

void QWidget::setWindowTitle ( const QString & )
EDIT: If you are using QtDesigner, on the property tab, there is an editable property called windowTitle which can be found under the QWidget section. The property tab can usually be found on the lower right part of the designer window.

For new Qt users this is a little more confusing than it seems if you are using QT Designer and .ui files.
Initially I tried to use ui->setWindowTitle, but that doesn't exist. ui is not a QDialog or a QMainWindow.
The owner of the ui is the QDialog or QMainWindow, the .ui just describes how to lay it out. In that case, you would use:
this->setWindowTitle("New Title");
I hope this helps someone else.

I know this is years later but I ran into the same problem. The solution I found was to change the window title in main.cpp. I guess once the w.show(); is called the window title can no longer be changed. In my case I just wanted the title to reflect the current directory and it works.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle(QDir::currentPath());
w.show();
return a.exec();
}

You can also modify the windowTitle attribute in Qt Designer.

system("title WhateverYouWantToNameIt");

Related

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.

Why does opening a QDialog the second time cause it to be shown immediately, not animated smoothly?

In my Qt application, I have a QMainWindow subclass for the UI (ProgramWindow) and a QDialog subclass (SettingsDialog) which is shown when the user clicks the "Settings" button in the main window. I'm using the following code to implement this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ProgramWindow pw;
pw.show();
pw.setFixedSize(pw.size());
SettingsDialog set(&pw);
QObject::connect(&pw, SIGNAL(settingsButtonClicked()), &set, SLOT(show()));
return a.exec();
}
However, I have noticed that when the settings dialog is shown for the second time (the user clicks the settings button, the dialog is shown, then the user closes it, then clicks on "Settings" again) the window is not shown "smoothly", as other windows are in Windows 8.1's default theme, it just suddenly appears on the screen like in the old XP theme, for example. Note that this doesn't happen the first time the window is shown.
What could be the reason?
EDIT: I suspect that instead of destroying the window, close() called on a QDialog will end up calling Win32's ShowWindow with the SW_HIDE parameter, and so when it's shown again later, the Windows UI manager doesn't see it as creating the window but merely re-showing it, so it doesn't "play" the animation. The solution is to create the dialog using new and letting Qt dispose of it when appropriate:
MyDialog* dialog = new MyDialog;
dialog->show();
// in MyDialog's constructor
setAttribute(Qt::WA_DeleteOnClose);

QT 5 add a custom widget inherited from QGraphicsView to designer

I have a custom widget from modified elastic nodes example. It inherits from QGraphicsView. In the example it is set as a central widget in the main window, so nothing can be added. Here's the code for main from the example:
#include "graphwidget.h"
#include <QApplication>
#include <QTime>
#include "mainwindow.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
GraphWidget *widget = new GraphWidget;
QMainWindow window;
window.setCentralWidget(widget);
window.show();
return app.exec();
}
I want to add the widget to the ui form, so that i can add some buttons and input. How can i do that?
What you are doing now is making an application.
If you want to make a custom widget in Qt , you need to build it as Custom Designer Widget - a plugin (as Dll).
In Qt creator choose OtherProject=>Qt Custom Designer Widget and create a project. You need to build as Release version, not the debug one.
After building, if Qt hasn't already, you need to copy the .dll file into Qt/plugins/designer and Qt/Tools/QtCreator/bin/plugins - you will find .dll's there. You also need to build plugin with tool chain, as Qt creator/designer was i.e. VisualStudio 2013 64bit or Mingw - MUST be the SAME.
It's not easy, but i wish you luck.
An easier way is to add QGraphicsView in the ui form and promote it to GraphWidget class.

Qt: Access Widget from main function and implement exit button

I want to implement an exit button in my application, which has the following setup:
I have a main function which looks like this:
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
I also have a mainWindow function which has been generated by the QT Creator IDE.
I design the GUI with the Qt Designer and when I want a pushbutton to do something when clicked, I use a function like this:
void on_selection_clicked();
I hope the setup is now sufficiently described.
Now to my problem: I want to implement a button, which, when clicked, terminates the window and the application. I first tried implementing this in a function like this:
void on_exit_clicked();
But I don't know that to to here.
Then I heard of a aproach via QObject::connect, but I have two questions:
1.) Where should I put this? In the main function? 2.) can I access the object simply via the object name given in the QT Designer?
no you should connect it in the constructor of the MainWindow
connect(ui->exit,SIGNAL(clicked()),QCoreApplication::instance(), SLOT(exit()));
QCoreApplication::instance()->exit() will quit the application
yes through the ui field in MainWindow see the code above
I don't know which Qt version you use, so I will suppose Qt 5.0 (signal/slot mechanims was updated).
QWidget has slot QWidget::close().
QPushButton provides signal QPushButton::clicked(bool checked = false)
So you can connect them in constructor of your MainWindow:
QObject::connect(your_button, &QPushButton::clicked, this, &QWidget::close());
Also I suggest to look into the files generated from *.ui files - so you have deeper understanding of what's going on.

Using Qt To Build A Full Screen Layout

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.