QDialog remove title bar - c++

The net is flooded with similar questions, but for all I have seen nothing suits to solve the problem at hand.
In my QT-C++ app, I have a mainwindow form with some functions, there is a QPushButton, Pressing which a QDialog opens. Now, all functionalities in forms work fine, but I want the final application to be without any top title bar. i.e. No Close / Minimize / Maximize Button.
In my main.cpp I have done --
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w.show();
return a.exec();
}
as a result the mainwindow has become -
For the dialog.cpp window, I have set -
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//QDialog Dialog(0, Qt::CustomizeWindowHint|Qt::WindowTitleHint); --- used this also; no use
QDialog Dialog(0, Qt::FramelessWindowHint | Qt::Dialog);
But the title bar of QDialog remains, it looks like -
Where am I going wrong ??? any ideas on how to remove the close button and the title bar ???

I needed to do the same thing as this question for dialogs but I wanted a border on the dialog without the window bar. The solution was actually quite simple. Just set the dialog's flags to Qt::CustomizeWindowHint:
dialog.setWindowFlags(Qt::CustomizeWindowHint);
You can also OR this with specific flags to further customize the windows appearance as noted in the documentation.

Solved it with help from a friend, posting the answer for ready reference for someone in need ---
in the mainwindow.cpp when the Fetch button is pressed, the qdialog opens, I have set the properties there;
void MainWindow::on_pushButton_2_clicked()
{
Dialog dialog;
dialog.setModal(true);
dialog.setWindowFlags(Qt::FramelessWindowHint);
dialog.exec();
}
This did the trick --
and the dialog --

I have had some problems with setting Qt::FramelessWindowHint flag, so I ended up with overriding resizeEvent instead of setting this flag:
void MyDialog::resizeEvent(QResizeEvent*)
{
this->setMask(QRegion(this->rect()));
}

Dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

Related

Set text for statusbar in QtDesigner?

By default, if I create a new Form in QtDesigner, of type "Main Window", I get three elements in there: centralwidget, menubar - which is not visible in Preview (Ctrl-R), unless actual menu entries are added; and statusbar.
The problem is - by default, the statusbar is the same background color as the rest, and so, when I do a Preview, I cannot really perceive whether the statusbar is there or not:
Basically, the only thing I can see is the "sizeGrip", which is not always easy to see - so I would like to have a text/message shown in the statusbar as well.
Now, I know that the API for QStatusBar Class has .showMessage used to show text in a statusbar - however, I cannot find any similar field in QtDesigner?!
So - is it possible to set a default/placeholder text in the statusbar in QtDesigner - and if so, how?
No, you cannot.
In QtDesigner, you can only set properties of a widget (see Q_PROPERTY), not invoke methods. The properties are listed in the Properties section of the documentation, and QStatusBar only has sizeGripEnabled (and the inherited properties from QWidget)
But what is the actual problem? You cannot clearly make out the status bar in the preview? The preview is supposed to help in checking singal/slots and layout constraints, not as a full functioning application.
There are also things that will influence the actual look&feel in the final application that cannot be checked in designer preview, like dynamic stylesheets, or custom styles.
if you want to check how your window looks like with text in the statusbar, you will need to make a mock-application that does just this: show some text
#include <QtCore>
#include <QtWidgets>
#include "ui_mainwindow.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
Ui::MainWindow ui;
QMainWindow wnd;
ui.setupUi(&wnd);
wnd.show();
ui.statusbar->showMessage("Hello World!");
return app.exec();
}

implementing custom completer, popup window steal the focus

I need to implement custom completer, similar to QCompleter but containing customized widgets. I got stuck at the very beginning, I can't make the popup work as it should work. In the following example I attach a completer (well, just a plain widget) to the first line edit. The second line edit is there just to test the focus in/out behavior. The problem is that when the popup displays, it steals the focus from the line edit. But this is not what I want, I want to be able to keep typing in the line edit. I tried several other options as commented out in the code below, but non of them worked.
#include <QApplication>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget container;
QLineEdit editor;
QLineEdit editor2;
QHBoxLayout layout(&container);
layout.addWidget(&editor);
layout.addWidget(&editor2);
QWidget completer; // or QWidget completer(&editor);?
//completer.setFocusProxy(&editor); // tried this, but it does not help
completer.setWindowFlags(Qt::Popup);
// the following lines are an alternative to Qt::Popup but the window
// does not close automatically, which is a problem
// e.g. when moving or resizing the parent window
//completer.setAttribute(Qt::WA_ShowWithoutActivating);
//completer.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
QObject::connect(&editor, &QLineEdit::textEdited,
[&] {
completer.resize(editor.width(), 100);
completer.move(editor.mapToGlobal(QPoint(0, editor.height())));
completer.show();
// editor.setFocus(); // does not help either
});
container.show();
return a.exec();
}
How to implement custom completer?
UPDATE: From the docs, I read that "A popup widget is a special top-level widget that sets the Qt::WType_Popup widget flag, e.g. the QMenu widget. When the application opens a popup widget, all events are sent to the popup. Normal widgets and modal widgets cannot be accessed before the popup widget is closed."
So it seems that when I open a popup it automatically receives the events and therefore I have to forward the events to the line edit using QCoreApplication::sendEvent(editor, event); inside my Completer::keyPressEvent. This seems to work fine except the fact that the cursor in the line edit is not visible or not blinking.
Just apply this method to your completer widget:
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
It works for me.

Non-modal QWidget dialog that stays on top of the window

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.
Dial::Dial(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Tool | Qt::Dialog);
// ...
Consequently, I looked into the docu and found this:
Indicates that the widget is a tool window. A tool window is often a
small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons. If there is a parent,
the tool window will always be kept on top of it.
Happily, I added this line into my singleton creating the dialog.
d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);
Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?
I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:
#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.show ();
QWidget *pLineEdit = new QWidget(&w);
pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
pLineEdit->show ();
a.exec ();
}
Tested with Qt 5.9.
Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);
Then use show() instead of exec() to make it non-modal:
dlg->show();
You need to set the modality (documentation) of the widget, like this:
QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();
However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:
QDialog *dialog = new QDialog(window);
dialog->exec();
Use QDialog instead of QWidget, and pass the parent widget in its constructor function.
QDialog* pDlg = new QDialog(this);
pDlg->show();

How to put osgEarth's ViewerWidget into an tabbed MdiArea?

Is there something special about putting osgEarth's ViewerViewer into a QMdiArea? I created a QMdiArea as central Widget (called setCentralWidget) instead of taking osgEarth's viewer directly as central widget.
QMdiArea *mdiArea = new QMdiArea(this);
setCentralWidget(mdiArea); // call to QMainWindows method, snippet is taken from app's MainWindow
mdiArea->addSubWindow(viewerWidget); // this doesn't work, globe is not drawn
Everything I tried didn't worked... except osgEarth's ViewerWidget is set as central widget of my MainWindow. Also tried MultiViewerWidget without any success but because I need only one view the ViewerWidget should be ok, or not?
I had a look into the examples but didn't succed to use one of them as starting point.
Any hints? Thank's in advance.
you can try this, where Form1 is a QDialog
in main.cpp
int main()
{
QApplication a(argc, argv);
Form1 w=new Form1();//qdialog
.................//do something to initial the map
w.loadWidget(viewerWidget);
w.show();//the order of the loadwiget() and show() is important!!!!!
a.exec();
}
in Form1.cpp
void Form1::loadWidget(QWidget *qwidget)
{
qwidget->setMinimumSize( ui.mdiArea->width(),ui.mdiArea->height());
QMdiSubWindow * subW=ui.mdiArea->addSubWindow(qwidget);
subW->setWindowFlags(Qt::SubWindow | Qt::FramelessWindowHint);
subW->maximumSize();
}
This works well with qt 4.8.4+osgearth 2.3
Try setting the subwindow's geometry before starting the UI.
QMdiSubWindow* sw = mdiArea->addSubWindow(viewerWidget);
sw->setGeometry(...);
Otherwise OSG will probably become confused.
Got this answer by Gwaldron in the osgEarth forum here and it worked.
Also setting a minimum size for the viewerWidget will help (e.g. for positioning on TabWidget). See my question and answer here.

Set QLineEdit focus in Qt

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus() sets the focus on the line widget #app startup only if it is placed after laying out the widgets and if used before it's not working?
Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.
I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.
Another trick that might work is by using the singleshot timer:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum