I created a menu in my QMainWindow derived class, and added a QWidget to it via QWidgetAction. Now my app does not close and stays in infinite loop when I try to close it. Any hints on why this happens?
Here's the code:
MainWindow* parent = ...;
auto menu = parent->menuBar()->addMenu("Menu");
auto action = new QWidgetAction(menu);
auto widget = new QLabel("Lol");
action->setDefaultWidget(widget);
menu->addAction(action);
This happens under OSX Sierra, Qt 5.7
Related
I have an issue with Qt where the behavior on Windows is different than Mac or Linux. I discovered the issue on PySide2 but was able to reproduce it in a minimal C++ application as well (see below).
When I dismiss a Popup widget by clicking outside the click is ignored by the rest of the application. This is the desired behavior and is the way it works on Linux and Mac. However in Windows this click is registered by the widget that was clicked which in my application leads to unwanted user input on the underlying widgets.
Is there a way to will prevent the dismissal click from being passed on in Windows? I am fine with having platform dependent code for this issue.
The behavior can be reproduced with this example. When the popup is open and testButton is clicked the onTestButton method will be executed.
#include "mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setFixedHeight(600);
this->setFixedWidth(800);
QWidget* w = new QWidget();
QDialog* popUp = new QDialog();
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlag(Qt::Popup | Qt::FramelessWindowHint);
popUp->setModal(true);
popUp->setVisible(false);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);
QPushButton* openButton = new QPushButton("open popup");
// same behavior whether using QDialog::show or QDialog::exec
connect(openButton, &QPushButton::clicked, popUp, &QDialog::show);
QPushButton* testButton = new QPushButton("catch mouse");
connect(testButton, &QPushButton::clicked, this, &MainWindow::onTestButton);
layout->addWidget(openButton);
layout->addWidget(testButton);
this->layout()->addWidget(w);
}
void MainWindow::onTestButton()
{
qDebug() << "caught mouse";
}
QWidget is just a widget, if you want it to "hold" focus, then use QDialog instead.
Also it changes the "logic" if u use it as QDialog.show or QDialog.exec.
Can you try with QDialog?
QDialog::setModal(bool) should set the dialog to modal, thus prohibiting clicking into the mainwindow.
Your windowflag Qt::Popup blocked modality. If you want to use that approach and create Qwidget as a modal dialog (Qt::Dialog), it should be launched from another window, or have a parent and used with the QWidget::windowModality property.
Tested:
auto w = new QWidget;
QDialog* popUp = new QDialog;
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlags( Qt::FramelessWindowHint);
popUp->setModal(true);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);
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();
For some reason all my menu bar items are greyed out when I use the native menu bar on OS X Mavericks:
I create the menu actions using the following code:
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("New"));
newAct->setShortcutContext(Qt::ApplicationShortcut);
newAct->setEnabled(true);
newAct->setAutoRepeat(false);
addAction(newAct);
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
...
These actions are then added to the menubar like this:
// _menuBar = new QMenuBar(0);
_menuBar = menuBar();
//_menuBar->setNativeMenuBar(false);
fileMenu = _menuBar->addMenu(tr("&File"));
fileMenu->addAction(newAct);
Uncommenting the first line shows the same behaviour. It does however work fine when I use the the non-native menu bar.
Qt version:
$ /usr/local/qt/5.3/clang_64/bin/qmake -v
QMake version 3.0
Using Qt version 5.3.1 in /usr/local/qt/5.3/clang_64/lib
Any ideas/suggestions?
I suspect this line is your culprit:
addAction(newAct);
You shouldn't be adding the QActions to your window, since you'll be adding them to the fileMenu object instead. Try removing the above line.
I had the same problem.
Setting the windowModality property of my MainWindow to NonModal worked for me.
I´ve beeing programming Java for some time right now...Now that I got into C++ and Qt I am a bit lost about GUI Thread (EDT Thread) and Worker Thread
I am trying to make the main window of my application open only when the configuration window is closed.
I dont want to put the code for creating the main window in the OK button of my configuration window.
I tryed to make them modal but the main window still opens.....
Afther configuration is complete I still have to see if there is an application update...So its something like
EDIT: This is my main:
ConfigurationWindow *cw = new ConfigurationWindow();
//if there is no text file - configuration
cw->show();
//**I need to stop here until user fills the configuration
MainWindow *mw = new MainWindow();
ApplicationUpdateThread *t = new ApplicationUpdateThread();
//connect app update thread with main window and starts it
mw->show();
Try something like this:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog *dialog = new QDialog;
QSlider *slider = new QSlider(dialog);
QHBoxLayout *layout = new QHBoxLayout(dialog);
layout->addWidget(slider);
dialog->setLayout(layout);
dialog->exec();
qDebug() << slider->value(); // prints the slider's value when dialog is closed
QMainWindow mw; // in your version this could be MainWindow mw(slider->value());
w.show();
return a.exec();
}
The idea is that your main window's constructor could accept parameters from the QDialog. In this contrived example I'm just using qDebug() to print the value of the slider in the QDialog when it's closed, not passing it as a parameter, but you get the point.
EDIT: You might also want to "delete" the dialog before creating the main window in order to save memory. In that case you would need to store the parameters for the main window constructor as separate variables before deleting the dialog.
You have to learn about signals and slots. The basic idea is that you would send a signal when you configuration is finished. You put your QMainWindow in a member variable and call mw->show() in a slot of your main programm that is connected with the configurationFinished signal.
If your ConfigurationWindow is a QDialog, you could connect the finished(int) signal to the MainWindow's show() slot (and omit the show() call from main).
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