Reset GUI values in Python 2 - python-2.7

I have the following code, showing the segments that my question is related to. The program executes as expected for the first time when I run. The program gets user selected inputs from the user from a GUI. After the first run, I need to reset all the values and variables to their original status without shutting down the program and re-run. Specifically, the variable SaveFileName, is the variable that holds the file name from a PyQt LineEdit box and does not reset, even when the run button is again pressed (connected to the Run function shown below).
Is there an easy way to implement the reset in a short command? I know I can reset every value individually by putting a reset button and restore all the values to original one by one in the code, but I am looking for a simpler way if there is one. Any help is appreciated.
def Run(self):
global SaveFileName
SaveFileName==0
.......other codes creating and exporting data to excel based on
user selected options through a GUI
writer.save()
if SaveFileName==0:
self.Output_File_Window.setText("")
if SaveFileName!=0:
shutil.move(path_refs + "/TEMP_file.xlsx", SaveFileName)
SaveFileName=str(self.Output_File_Window.text())

Related

How can I save Checkbox selection in qt c++?

I am trying to develop an application with QT C++. I added a checkBox. How can I make my checkBox be in the last selection when I close my app and open it again. For example, if the checkbox was selected before the application was closed, I want it to be selected when the application runs again. If it wasn't selected before closing, it should come back unselected when it runs again. How can I do that?
Read the value of the checkbox when your app shuts down. Save the value somewhere, like; QSettings, a custom file, windows registry, etc. When your application starts, read the stored value and set the checkbox state to match.

How to get an element on a page, click something else and wait for the original element to dissapear

I am using Python 2.7, Robot Framework and Selenium2Library on a windows server.
On a page with a button and a data text box, I have a selenium test that clicks the button, this fires off a get request and on return recreates the data text box and updates with the new value. The test then sleeps for 10s to allow this to happen and then reads the value in the data text box.
What I would really like to do is get the element for the data text box, then click the button and keep polling for the original data text reference to become unavailable on the DOM i.e. it has been recreated, and then read the text box to get the value.
I can't for the life of me find out how I would do that using Robot Framework and Selenium2Library as all the calls are self contained and don't pass references back.
Could you offer any other solution?
There are a number of ways to do it with the SeleniumLibrary, all revolving around its Wait Until keywords - documentation link.
Option one - the element is the same, so its locator doesn't change; the check is done with Wait Until Element Contains, and passing the new text:
Click Element ${the_locator_for_the_button}
Wait Until Element Contains ${the_locator_for_the_element} your target text
Option two - if on click the target element changes to a different one, e.g. the locator is different. Then you'd first wait for the initial element to disappear, and the new one to appear:
Click Element ${the_locator_for_the_button}
Wait Until Element Is Not Visible ${the_locator_for_the_initial_element}
Wait Until Element Is Visible ${the_locator_for_the_new_element}
Option three - if you don't want to deal with locators, but to make sure the actual element as Selenium sees it disappears, you can get it with Get Webelement, and then pass that reference to the Wait ... keywords - most (if not all) SeleniumLibrary support both locators or acutal webelements:
Click Element ${the_locator_for_the_button}
${webelement}= Get Webelement ${the_locator_for_the_initial_element}
Wait Until Element Is Not Visible ${webelement}
The good thing about the Wait Until ... keywords is that they constantly poll the DOM for the expected change, and continue at the first detection the condition is met. E.g. it's not a hardcoded sleep that'll pause the execution for the predefined time, but finish as soon as ready.
Have also in mind all these keywords support the argument timeout=Xs, where the X is the time up to which the keyword waits for the condition to be met.

User Input with QT Widget Application

I have a QT widget that is functioning as a GUI for an external Process I am running. I am manually setting the path for this executable in the code. I would like to allow for the user to specify the path to the executable, from the GUI. My idea was to have some line edit box where the user enters the path, and once the path was entered it saved the path into a variable called program_path. This way when I call the process using this variable, it allows the user to choose which executable to run. I have searched the Internet, as well as stack overflow, and I was not able to find something relevant enough to what I was doing to attempt a solution. I was hoping someone could point me in the right direction as to how to implement this. Any suggested QT class or widgets to use? Any help is appreciated, in advance.
Your idea is not practical a GUI for an external Process ... I would like to allow for the user to specify the path to the executable.
If you are using an application in this way, then you should expect troubles. because giving the user that much control on your program is not good ..
QProcess can be used to run external applications but you are expected to have control inside your code .. different executables run in different ways (some are command line, some are GUI ...etc) and take different inputs (some executables run without input or switches from user , some require command line parameters ...etc) and they are different with their outputs (some apps require permissions, they give results in different ways).
Second, users are usually interested in final results not remember many executables names and details of using it.
Practically, it might be more suitable therefore to offer uses a list of choices of requests each corresponds to an executable, while you hide the details of calling each process inside your code.
To do that you need for example a QComboBox that shows users what options they have, and based on user selection you run QProcess needed to complete the action.
After all, taking a string from QLineEdit or a QCombobox is straightforward:
QString action = QLinedit::text();
QString action = QComboBox::currentText();
You might also need to show another selection process based on initial user selection, that takes more input from user and finally construct your QProccess
with all details given from user.
To allow a user to select a file or repository, I advise to use the QFileDialog class (http://doc.qt.io/qt-5/qfiledialog.html#details). It's very convenient.
Code should look like:
QFileDialog *_DialogWindow = new QFileDialog(this); // Creates a dialog window.
bool result = _DialogWindow->exec(); // Window opens. User select something in his file system. Instructions returns only once he's done.
if( result ) {
_exePath = _DialogWindow->selectedFiles().first() ; // Get the path that was selected
}
You may have to set some flags on your QFileDialog object, to specify which kind of file should be selected (.exe in your case)

Qt C++ preventing program to show error-messages when busy

Hi I have a question concerning error-messages.
I have a window with several buttons including a OK and Cancel-button. My OK-button executes a program that moves some chart series and for doing so it needs to read in lots of data from a file and shift these values. The Cancel button cancels this operation. The calculations cannot be separated into smaller portions of code.
This works well for smaller amount of data but when I use it with large sets of data the program acts as if it crashed. Nevertheless, after some time everything is back to normal, the calculation is done.
There are 2 things I don`t like:
1) When I leave the program alone the program changes the headerline of my window to ....(keine Rückmeldung) which means no response.
After the end of the calculation the text ...(keine Rückmeldung) disappears in the header and everything is back to normal.
2) When I try to press the "cancel" button in my window while running the calculation, an additional window appears:
There again, when I leave the program alone and the calculation is finished this window disappears ( as well as the (keine Rückmeldung) in the header of my window) and and everything is back to normal.
To solve problem 2 I tried to disable my "Cancel" button but this does not help. The slot which is behnid the cancel-button gets executed anyway.
My question now is: Since I don´t want the user to see these error-messages, is there a way to prevent the program of showing them?
Thank you
Consider using a QThread for expensive computation tasks. Or better, you can use other built-in multi-threading utilities such as QConcurrentRun and QFuture.
You can then easily get the state of your background function and show a loading Window, or allow the user to perform other operations in the meantime.

How to display all images from directory one after another in qt,opencv

i try to make GUI Application in QT Creator.İ wanna display all images from chosen directory (a sort of slide show),i read QDir,QStringlist,QFileDialog class references but it doen't work.it shows only last image in folder.
for(int i=0;i<10;i++)
{
QImage image2(directory+"/"+fileList[i]);
ui->label_pic2->setPixmap(QPixmap::fromImage(image2));
//Sleep(550);
}
Sleep function only makes to load slowly not to show one by another.
First you need to undertand that GUI is only updated when you code is not executed and the application can process incoming events. If you create an endless circle your GUI will neved be updated. That's why you can see only the last image - because you don't let GUI to be updated.
You can force the application to process incoming events by QCoreApplication::processEvents(). In this case you will probably see quick flashing of images. But you will certainly need to make a pause after showing of each image. And your Sleep will probably work there, but during the pause your application will be freezed without being able to process the events.
That's why you have been suggested to use QTimer. It calls a slot in a certain amount of time. All the other time the application is processing messages and updates GUI and so on. When the slot is called you can switch label to show the next image. And this is exactly what you need.
Summary:
1. Create a QTimer, set interval and connect it to a slot, start the timer.
2. In the slot implementation read the next image in the list and assign it to the label.
Tip: To track what image is shown create a interger member variable. Set it to 0 when starting the animation, increase it in the slot.