I am new to Qt.
I am doing a project using Qt Creator. In my project, I have one mainWindow. From the main window I start 4 screens (one after another, showing Initialization process). A new QDialog screen is opened, if there are any errors on any screen. My error screens have two Button (Retry, Continue). If i press Retry, i have to restart the initialization process over again.
eg;
void ErrorScreen1::on_Retry_pressed()
{
Screen1 *scrn = new Screen1(this);
scrn->show();
this->close();
}
In above example, it restarts the process.
Is there any way, I can start the initialization process from the point it was left?
Thanks in advance,
In common you should somehow keep the current state of your process and then you can restore it.
Have you considered making those four dialogs into a wizard with four steps? The next button could work for the continue, and you could have a retry button on each page that it makes sense, with just that page doing the retry logic necessary.
I resolved this issue by using QMessageBox as my error window.
It allowed me to start my process from the point i left.
void Screen1::ErrorMessage()
{
timer->stop();
QMessageBox *msgbox = new QMessageBox(this);
msgbox->setWindowTitle("ERROR MESSAGE");
msgbox->setText("Initialization Failed.");
msgbox->setStandardButtons(QMessageBox::Cancel | QMessageBox::Retry);
msgbox->setDefaultButton(QMessageBox::Retry);
int ret = msgbox->exec();
switch (ret)
{
case QMessageBox::Retry: timer->start(); break;
case QMessageBox::Cancel:
timer->disconnect();
ui->progressBar->setValue(0);
break;
default: break;
}
}
Related
I'm trying to add quit confirmation message at OS shutdown according to this example:
https://doc.qt.io/qt-5/qsessionmanager.html#allowsInteraction
I interact with the user via QML interface. It is asynchronous, so I use slots/signals. And I use additional QEventLoop to stay inside of QGuiApplication::commitDataRequest call while interacting with the user.
The trouble is that after I shutdown OS (Windows 10) - my app prevents OS to shutdown and it's good, but its screen is just white, or contains old state without the confirmation dialog. I need to hide/restore window or change its size, or do some mouse clicks inside of it to force it repaint. When it repaint all is OK and my confirmation dialog is there.
Is this a bug? Is there a workaround?
This is the code I use:
void AppQuitConfirmationAtOsShutdownManager::onCommitDataRequest(
QSessionManager &m)
{
if (!m_ui->isConfirmationRequired())
return;
if (!m.allowsInteraction())
return;
bool isConfirmed = true;
QEventLoop loop;
qtconnect(m_ui.data(), &AppQuitConfirmationUiManager::confirmationResult,
&loop, [&](bool confirmed)
{
isConfirmed = confirmed;
loop.quit();
},
Qt::QueuedConnection);
m_ui->requestConfirmation();
loop.exec();
m.release();
if (!isConfirmed)
m.cancel();
}
In order to verify that all changes made by the user have been saved I want to intercept the exiting/quitting of a JavaFX application.
Is there a common way-to-go to achieve this like overriding an event or is there more to it?
As they have already said, this is done by intercepting WindowEvent.WINDOW_CLOSE_REQUEST. You can then stop the suspension by calling event.consume().
This is an example of how to capture the event and display a confirmation dialog. Depending on the user's choice, you can take serialization actions if you wish.
primaryStage.setOnCloseRequest(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initOwner(primaryStage);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setHeaderText("Exit");
alert.setContentText("Do you want to exit?");
alert.getDialogPane().getButtonTypes().setAll(ButtonType.OK, ButtonType.NO);
Optional<ButtonType> optional = alert.showAndWait();
if(optional.isPresent() && optional.get() == ButtonType.OK) {
// save data
return;
}
event.consume();
});
In order for the implementation to be complete, you need to implement a logic for clear exit from the application from control. For example, when choosing from the File menu -> Close. When capturing the event, you must run WindowEvent.WINDOW_CLOSE_REQUEST to trick the exit logic.
closeMenuItem.setOnAction(event -> {
Window window = menuBar.getScene().getWindow();
window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
});
In the class Application there is the stop method which you can override possibly.
EDIT 4:
EDIT 3
EDIT 2
string currentWindow = driver.CurrentWindowHandle;
driver.SwitchTo().Window("");
string childTitle = driver.Title;
driver.SwitchTo().Window(currentWindow);
string parentTitle = driver.Title;
the above code gives me the same title for parent window or child window.
EDIT:
<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay Around</a>
how to verify the title of a newly window open and once i verified then close the opened new window?
so in my page I have a link and click on the link and it opens a new window and now I am not sure how to verify the title of that window.
here is what i have done so far.
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
//it opens a new window
now i want to switch focus on the new window and verify the title and close the new window
back to the previous window.
The piece that most people miss when dealing with popup windows in IE is that a click on an element is asynchronous. That is to say, if you check the .WindowHandles property immediately after a click, you may lose the race condition, because you're checking for the existence of a new window before IE has had the chance to create it, and the driver has had a chance to register it exists.
Here's the C# code I would use to perform the same operation:
string foundHandle = null;
string originalWindowHandle = driver.CurrentWindowHandle;
// Get the list of existing window handles.
IList<string> existingHandles = driver.WindowHandles;
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
// Use a timeout. Alternatively, you could use a WebDriverWait
// for this operation.
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
while(DateTime.Now < timeout)
{
// This method uses LINQ, so it presupposes you are running on
// .NET 3.5 or above. Alternatively, it's possible to do this
// without LINQ, but the code is more verbose.
IList<string> currentHandles = driver.WindowHandles;
IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
if (differentHandles.Count > 0)
{
// There will ordinarily only be one handle in this list,
// so it should be safe to return the first one here.
foundHandle = differentHandles[0];
break;
}
// Sleep for a very short period of time to prevent starving the driver thread.
System.Threading.Thread.Sleep(250);
}
if (string.IsNullOrEmpty(foundHandle))
{
throw new Exception("didn't find popup window within timeout");
}
driver.SwitchToWindow(foundHandle);
// Do whatever verification on the popup window you need to, then...
driver.Close();
// And switch back to the original window handle.
driver.SwitchToWindow(originalWindowHandle);
Incidentally, if you're using the .NET bindings, you have access to a PopupWindowFinder class in the WebDriver.Support.dll assembly, which uses a very similar approach to the locating popup windows. You may find that class meets your needs exactly, and can use it without modification.
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
// Post above operation a new window would open as described in problem
// Get hold of Main window's handle
string currentWindow = Driver.CurrentWindowHandle;
// Switch to the newly opened window
Driver.SwitchTo().Window("Your Window Name");
// Perform required Actions/Assertions here and close the window
// Switch to Main window
Driver.SwitchTo().Window(currentWindow);
Normal status message -these are always shown by an application unless a temporary message is shown. This is what I know about normal status message. So using these code on my constructor
ui.statusbar->showMessage("Temp message", 3000); // ui is the Ui::AutoGenHeaderForForm
QLabel *label = new QLabel;
ui.statusBar->addWidget(label);
label->setText("hello world");
I get that, when I run my project I get the status Temp message for 3 sec. Then I don't get the hello world back. Should the hello world come automatically after 3 sec in the position of Temp message ?
Assuming the code you show is in the constructor of your main window, the problem might be due to events not being properly processed because the event loop is not yet started at the time of the main window creation.
Try to execute the showMessage in a "delayed initialization" slot, e.g.
QLabel *label = new QLabel;
ui.statusBar->addWidget(label);
label->setText("hello world");
QTimer::singleShot ( 0, this, SLOT(delayedInit() );
void MainWindow::delayedInit()
{
ui.statusbar->showMessage("Temp message", 3000); // ui is the Ui::AutoGenHeaderForForm
}
I think the documentation is pretty clear:
The widget is located to the far left of the first permanent widget
(see addPermanentWidget()) and may be obscured by temporary messages.
I have a ClientSocket Class which is a TcpSocket in a certain state of conversation I need to ask the user to enter a Communication password. So I've created a Dialog DG::ChallangeDialog . in DG::ChallangeDialogs ctor I've
ui->setupUi(this);
QPushButton* okButton = ui->buttonBox->button(QDialogButtonBox::Ok);
if(okButton != 0x0){
okButton->setText("Challange");
}
QObject::connect(this, SIGNAL(accepted()), this, SLOT(acceptedSlot()));
acceptedSlot again emits a signal challanged(QString)
void ChallangeDialog::acceptedSlot(){
QString text = ui->passBox->text();
emit challanged(text);
}
in ClientSocket I do
case Hallo:{
if(m->message().startsWith("welcome")){
DG::ChallangeDialog* dlg = new DG::ChallangeDialog;
dlg->setModal(true);
connect(dlg, SIGNAL(challanged(QString)), this, SLOT(challanged(QString)));
dlg->exec();
/*
DG::MessagePacket* res = new DG::MessagePacket((int)Hallo);
res->setMessage("challange");
send(res);
state = Challange;
*/
}
}break;
In ClientSocket::challange slot I send a Message challange (text) over the socket and store the password.
and I expect the Dialog to hide there and the normal socket conversation to continue. and after the Dialog is accepted or rejected the main application quits (It quits it doesn't crash). Why ?
My Application has no Other Widgets. I just works like an QCoreApplication. But still I've used QApplication cause I've some GUI Needs.
Is this the only window that is shown at this time? If so, I would guess that your QApplication instance is set to quit when the last window is closed. It is true by default.
If this is the case, you should explicitly set this to false before showing any windows.