How can I use a QProgressDialog as a simple wait dialog without QProgressBar? - qt

There is an operation in my application that can take up-to few minutes to finish. I wish to show a simple dialog box which opens when the operation begins, displays a simple wait message like 'Operation in progress..' and closes automatically when the operation finishes.
To implement above functionality, I am trying to make use of a QProgressDialog which does not have a QProgressBar. It seems to work fine but I am unable to set the wait message. Following is the code:
QProgressDialog progress(this);
QLabel *lblCustom = new QLabel(&progress);
progress.setBar(new QProgressBar());
progress.setValue(0);
progress.setLabel(lblCustom);
progress.setLabelText("Operation in progress. Please wait...");
progress.setMaximum(0);
progress.setMinimum(0);
progress.setModal(true);
progress.setWindowTitle("Validate field data");
progress.setCancelButton(0);
progress.setFixedSize(400, 100);
progress.setWindowFlags(progress.windowFlags() & ~Qt::WindowCloseButtonHint);
progress.show();
// operation
progress.close();
Setting label text does not seem to work. Please let me know what is wrong here? I am relatively new to Qt.

To solve the actual problem, I was used QLabel as a dialog instead of QProgressDialog and invoked QApplication::processEvents(), to make sure QLabel window gets painted with suitable text as well.

I suggest you create a new widget, then you can customize as you like. You can insert images and labels and you can create your own methods (including signals and slots). It will take a little longer to implement but it will be much more flexible and extensible.

Related

How to pause game running in background during asynchronous event?

I am creating a game in GMS2. I am using "show_message_async()" in my code. I know that when it is run, a message pops up on the screen and the game still runs in the background. However, I want the game to freeze in the background while the message pops up. Is it possible to do this? And if so how.
You should try looking up instance_deactivate_all(notme) and instance_activatie_all(notme)
https://docs.yoyogames.com/source/dadiospice/002_reference/objects%20and%20instances/instances/deactivating%20instances/index.html
This will disable all objects in a room except the object that's calling it (which should be the menu object that shows the message)
The only tricky part of it, it that it also disables drawing the objects. resulting in an empty screen. For this, you could either use a black screen, or draw a screenshot of the scene before they're disabled.
I agree with Steven's answer. To add on his answer about taking the screenshot of the game, you probably need to make a new surface, then use surface_copy from application_surface before deactivating the object.

display Qprocess output window inside the QT mainwindow

how to display Qprocess output window inside the QT mainwindow? I am calling ffplay by Qprocess. the video playing in a detached window? how would i play the video in same application QTwindow
I don't know ffplay but it seems that ffplay is opening the window by itself. One approach is that you could try to get the current image and display that in your mainwindow. Every time a new image is available you have to replace the image in your ui with the new one. Of course you have to keep synchronization in mind, since this application is not single-threaded.
In general your approach looks not right. You shouldn't call a program to do that kind of task. Normally you use a library for that.
You should have a look at this example to get an idea what I'm talking about: https://doc.qt.io/qt-5/qtwidgets-widgets-movie-example.html

Qt Main window loosing focus and going to background unexpectedly

Going up the learning curve and everything has been making sense so far except for this one:
I have a main window from where I call one or more dialog like this:
d->setWindowFlags(Qt::CustomizeWindowHint | Qt::Window | Qt::FramelessWindowHint);
d->setGeometry(etc...
d->show();
The dialogs are not bound to the main window, and they are actually set to be displayed right beside the main window looking like an extension.
To close the dialogs I use a mouse 'enterEvent' on the mainwindow to send a close signal to the dialog(s) which will use hide().
void MainWidget::enterEvent(QEvent *event)
{
emit signal_related_close();
}
This works but randomly this causes a very annoying issue. When the signal is sent to close the dialogs, the main window would loose focus and go in the background (behind whatever application that is on the screen). Note that it does not minimize but only goes in the background and like I said it is random (about one out of 3 times I do it). I can't seem to get a pattern.
I initially thought the signal was messed up and instead used a dialog::leaveEvent() but that didn't help. I also tried using dialog::mouseMoveEvent and using that to hide the dialog but that would still randomly loose focus on the application.
The only method that doesn't loose focus is using a push button on each dialog that will initiate the same hide commands but that's not great for my UI. So it would seem that using the mouse enterEvent/leaveEvent,mouseMoveEvent will cause that random issue.
Anyone experienced this issue before? Any idea what could be wrong? Thx.
I can't explain why but this problem occurred when using QEvent or any mouse related event together with recursive children widgets. For example take this code in the parent widget constructor:
ChildWidget c1 = new ChildWidget (this);
ChildWidget c2 = new ChildWidget (c1);
Childwidget c3 = new ChildWidget (c2);
Using QEvent in the parent widget will randomly make the window go to the background and loose focus.
Setting all the ChildWidget to the same parent stopped the problem.

How to draw something in a tooltip?

I am trying to develop a functionality using Qt which I don't know whether it is possible to implement. Here is the requirement:
When the user hovers over a node (an object derived from QGraphicsItem), a window will be shown near the node, in the window there might be some histograms or buttons that can be clicked to show further information. When the mouse leaves the window, it will automatically close.
I tried to use a tooltip, because it can pop-up near the node and close when the mouse leaves it, but it can only show text. So, it still cannot work that way. I am wondering if there is another way to do this? I did lots of google search, but there is still no answer.
Thanks so much for helping me with this.
If you're ok with using a 3rd party library, Qxt provides a class that provides a tooltip that is QWidget based, which will let you use an arbitrary widget as the tooltip rather than just text.
See: Qxt::ToolTip
you don't have to use tooltip for your app
you can use or call widget or dialog, on hover mouse event
Please refer Qt Example EmbeddedDialog Example, It is advanced, But you can understand how hover Enter/Leaving events are working. I personally prefer don not create instance of Popupdialog for each item, create it if only nesessary. Otherwise create one dialog and pass its reference to all the items through the constructor initialization.
1. These are the API you are intrested on, reimplemet this.
QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) and void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2. When You create Dialog, You can pass Qt::WindowFlags as Qt::ToolTip.

Qt: how can i put my main window in the background while im processing with window in forground?

Im using Qt framework , and i looking for the best method to show the user im processing something
how in Qt i can:
put the main window in the background and pop up window to the foreground with
for example " processing " massage
until its done processing the " processing " window will close and the main window returns to the foreground .
Use QProgressDialog. It is designed for that kind of use. You can use the QProgressDialog as a modal dialog thus preventing any user interaction to your main window. QProgressDialog also gives you an easy way to present the progress of your processing and an optional pushbutton to abort processing.
Edit:
QProgressBar can be used in two states: progressing in steps or just showing busy state.
QProgressDialog's progress bar cannot be used showing busy state because that would require setting QProgressDialog's min and max values to 0, which immediately closes the progress dialog. However, you can give the QProgressDialog a new progress bar using setBar()-function. Then you can set this progress bar's min, max and value to 0 and then getting the busy look.
QProgressDialog progressDialog("Processing...", "Abort", 0, INT_MAX, this);
QProgressBar* bar = new QProgressBar(&progressDialog);
bar->setRange(0, 0);
bar->setValue(0);
progress.setBar(bar);
progressDialog.setMinimumWidth(350);
progressDialog.setMinimumDuration(1000);
progressDialog.setWindowModality(Qt::WindowModal);
progressDialog.setValue(0);
// Do your time consuming processing here, but remember to change
// the progress dialog's value a few times per second.
// That will keep the busy indicator moving.
progressDialog.setValue(progressDialog.value() + 1);
// And check if the user has cancelled the processing
if (progressDialog.wasCanceled())
break or return or whatever necessary
// When your processing is done, close the dialog.
progressDialog.close();
You can try that:
In the function that call the pop-up just hide the main window once the process pop-up displayed.
Connect the end processing signal to the main window slot Show(). If you have not predefined signal for that, create one and emit it.
emit NameOfSignal;
Hope that helps
Edit:
For disabling the main window use setDisabled instead of hide and setEnabled instead of show.
You can give your "Processing"-window the Qt::WindowStaysOnTopHint to make it stay on top of your disabled main window.
If it's not to fancy for you, you can blur your main window using QGraphicsBlurEffect while the processing window is on top. That way the user gets the impression of the main window to be not accessable until your processing is done.

Resources