I have an application in qml. I am using quickview and setting it as full screen. But when I try to open a .ui file(QWidget) from full screen mode it is going behind the mainwindow. this doesn't happen when I am not in full screen mode. I have tried setting flag (Qt::WindowStaysOnTopHint) but it doesn't work. I am using QT5 . Thanks for the help
QUiLoader loader;
QFile file("abc.ui");
file.open(QFile::ReadOnly);
QWidget * _parent = loader.load(&file);
file.close();
_parent->setParent(mainlayout);
_parent->setWindowFlags(Qt::Tool);
_parent->setWindowFlags(_parent->windowFlags() | Qt::WindowStaysOnTopHint)
In this the mainlayout is quickview
The widget parenting behavior in Qt differs between QDialog and QWidget. A QDialog is always a top-level widget (a window). I guess that your the object that your .ui file creates is not a QDialog. Please ensure that it is:
QWidget * _parent = loader.load(&file);
Q_ASSERT(qobject_cast<QDialog*>(_parent));
If this assert fails, then you need to change your .ui file's base class to be a dialog.
You should probably also activate and raise your dialog:
_parent->activateWindow();
_parent->raise();
Related
I have an application in Qt in which I can switch between c++ app and QML app (setting central widget to one or other).
The problem is that in order to work this, I had to change my main.qml from ApplicationWindow to Page and now Dialogs and all Popups in my QML app are not working properly (modality and focus is not working at all).
My code looks like that:
QQuickWidget *view = new QQuickWidget(this);
view->setSource(QUrl(QStringLiteral("qrc:/main.qml"))); //Page
MyCppApp *myCppApp = new MyCppApp (this); //QWidget
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(view);
stackedWidget->addWidget(myCppApp);
stackedWidget->setCurrentIndex(1);
setCentralWidget(stackedWidget);
I know in Popup's documentation there is that "It can be used with Window or ApplicationWindow." but is there a way to get Popups in my QML work properly?
I am using Qt 5.8 and Qt Quick Controls 2.
For the modality you should not make the dialog window part of central widget in main window (otherwise it is modal relative to what?). Invoke it from main window. And to give it modal dialog looks and behavior you can apply window flags like that:
// this view is not a part of the app main window
view->setFlags(Qt::Dialog | Qt::WindowTitleHint |
Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint);
Mind that Qt::MSWindowsFixedSizeDialogHint is of course applicable on Windows only. And for setting the focus:
// to set the edit focus such trick needed
QTimer::singleShot(0, view, SLOT(setFocus()));
I use that with QML widget container. Implementation details may differ. And no QML part visible. There you may need to take care of FocusScope then.
How can I paint on a widget without overwriting the paintEvent.
I want to paint on a widget which is inside another one generated by Qt Designer , so i can't overwrite its paintEvent.
I tried to paint directly like this :
QPainter *painter= new QPainter(ui->drawArea);
painter.drawLine(50,50,50,150);
painter.close();
but Qt tell me that the QPainDevice is 0 or something like this,
I tried the same example by creating the painter then call the begin() method with the QPaintDevice (the widget) but same problem.
Qt version : 4.8.6.
Using custom widgets in Designer is not an issue. In Designer, add your widget as any other QWidget or QPushButton, depending which has the closest inheritance. Then with right click menu select Promote to ..., add your MyWidget.h and then promote the widget to MyWidget with reimplemented paintEvent(). Read more:
http://doc-snapshots.qt.io/4.8/designer-using-custom-widgets.html#promoting-widgets
I’ve created 2 .ui files, one is a main window, the other a widget. Designer generates the 2 .header files each with QT_BEGIN_NAMESPACE around the class declaration.
The problem is, what works in opening my main window, does not work in opening the second widget window.
To display my main window, I created a class that inherits from my .ui file:
class myWindow: public QMainWindow, private Ui::uiClassWindow
setupUi(this);
That opens fine, so then to open the second widget window, I declare a generic widget object and then save it with a pointer to my Widget Ui header file:
QWidget newWidget;
setupUi(newWidget)
But setupUi resolves to my Main Window header file… How do I tell it to use the Widget’s setupUi?
Is there a better way to go about this?
Up to my knowledge, for setupUi function is defined in Ui namespace. You need to give scope (Ui) for other widget too.
The setupUi() method is created by uic from your UI file, and it's different for each compiled UI.
In your myWindow, you inherit from Ui::uiClassWindow and can use its setupUi() method without qualification. You'll need an instance of a different UI class for your newWidget:
auto widget_ui = new Ui::myWidget;
QWidget newWidget;
widget_ui->setupUi(newWidget)
You could delete the widget_ui immediately if you wanted - but usually, you'll need to keep it in order to access the children it has now created in newWidget.
The only window in my application is a QQuickView which is a subclass of QWindow, not of QWidget, so if I want to use a QFileDialog to give the user the option to save a file, I do not know how to set its parent to be the QQuickView. I know that the QFileDialog works without a parent, but if it does not have a parent, it is not centered over the QQuickView. This is not a big problem on its own, but on my OS (Ubuntu 13.10) a QFileDialog without a parent sometimes appears under the QQuickView window and the user sees only a “flashing” taskbar icon.
I use Qt 5.2. I initially tried to use the FileDialog type in the Qt Quick Dialogs module but it does not have some features I need (such as the defaultSuffix property).
Right, this is a serious problem of QML. You cannot utilize the QFileDialog because you don't have a QWidget app and FileDialog lacks some critical features.
See: FileDialog in QTQuick (QML): Save file under given name
At the moment we are using code from deep down in the Qt source code, which is not in the public API. It only requires a Window instead of a Widget to do modality right.
Maybe I can write a blog post about that over Christmas.
I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?
Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.