QPlainTextEdit checking some condition - python

I want to create my own very simple editor of .txt files. As in real editors, I want that if you change the file and don't save it yet, the name of file will start with '*' (for example, *some_text_file.txt).
I think that fot this I should check QPlainTextEdit after the file was downloaded to editor and after the user pushed button 'save'. But I don't know how to check QPlainTextEdit without user control, but after some changes. Or maybe there is another way to do it. So, how to do it?

You could use the textChanged signal from the QPlainTextEdit. It indicates when the input text has changed. See:
https://doc.qt.io/qt-5/qplaintextedit.html#textChanged
Roughly, you would do the following:
load the file
display the filename without asterisk
connect the textChanged signal into a slot (function) that will add asterisk to the filename
when the Save button is pressed, remove the asterisk
If you're a Qt beginner, you might want to read about the Qt signal and slot mechanism:
https://doc.qt.io/qt-5/signalsandslots.html

Related

How to add function for QT-Designer Push Button

I am working on QT Designer to build GUI App. I want to ask how add command for push button.
For example I want to apply this action :
self.NfmtLoginButton.clicked.connect(lambda: SecondScript.printme(self.NfmtPasswordEntry.text()))
Is it possible through the QT Designer. to be permanently saved. and no need to add all the commands again when generating new python script file from UI file.
No, it's not possible, but that should not be a problem, as the files generated using pyuic should never, ever be modified (the warning in those files is not to be undertaken).
Those files should always be left unmodified and only updated again with pyuic, as they are only meant to be imported in the actual program file, as explained in the official guidelines about using Designer (the "multiple inheritance approach is generally the most suggested).

qt5 catch input from qtextedit

I am working on a small application which is basically a serial terminal with some added stuff.
for the terminal window I use the QTextEdit widget and allready overload add and overload some methods. However since this is a serial terminal I don't want the input that the user is typing in the QTextEdit to actually end up there. Most serial communication channels echo back the input that is send to them and I would like to show this in the QTextEdit and not what the user inputs.
The ideal would be I could overload the way QTextEdit handles its input and I work from there.
I have looked online but I can't seem to find what I am looking for. Maybe I am using the wrong search terms
You can set the QTextEdit widget to read-only mode and then just listen for its key events. That way nothing will be displayed in the QTextEdit and you will be able to intercept the keys.
If you subclass QTextEdit and reimplement the keyPressevent, you might want to call the base class implementation inside it. Otherwise you might not get the functionalities that for example page-up/page-down keys provide.

PySide text in QLineEdit not editable

I making a console like program for my application. I have a QLineEdit that takes up the whole height of the screen, where the user will be able to input the commands. I want to add "prompts" for instance 'hostname:current_dir># ' after the # the user will put the command. I want that prompt to NOT be editable (he can just backspace it away) but still have the user be able to type commands. Any ideas? Or may someone suggest a better way of doing this please?
You could connect a slot to the cursorPositionChanged () signal, check its position, and disable editing with setEnabled(False). You might also want to take a look into QTextEdit, QTextBrowser or QPlainTextEdit, where you could use the setReadOnly method.

PyQt4 File select widget

I want to make a QT4 (using QT designer) dialog, that contains a part where a file has to be selected.
Now, I know QFileDialog exists, and I can program something that does what I want.
But can I also just do it in QT designer?
Is there some way to get a "file select" widget in QT designer?
Or, I remember these buttons, having the selected file as a title and a little arrow allowing the user to select another file by the QFileDialog?
So is there a ready made solution, or do I have to program it myself?
There is no file dialog available from the Qt designer as far as I know. But you can easily do it with a few lines of code.
Assuming you have a simple button called pushButton and the path should be stored in lineEdit.
def selectFile():
lineEdit.setText(QFileDialog.getOpenFileName())
pushButton.clicked.connect(selectFile)
[edit]Just wondering though, are you using KDE by any chance? If so, than you can use the KUrlRequester for this. It can easily be configured to support anything from files to urls to directories.
QFileDialog exists in QtGui. At least in my version 4.4 and probably much earlier too. I think the reason it is not in Designer is because it opens its own window instead of being a widget to place on another window.
The documentation from QTDesigner could be better and at least hint of its existence.
Instantiate it and run the show command. It comes right up and defaults to /.
import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()
You can use method getOpenFileName() in QFileDialog Class.
QFileDialog.getOpenFileName() will return the file path and the selected file type
I got this : ('C:/Users/Sathsara/Desktop/UI/Test/test.py', 'All Files (*)')
To get only the file path use QFileDialog.getOpenFileName()[0]
Sample code:
def selectFile():
print(QFileDialog.getOpenFileName()[0])
dlg.locationBtn.clicked.connect(selectFile)

Change the focus from one Text widget to another

I'm new to Python and I'm trying to create a simple GUI using Tkinter.
So often in many user interfaces, hitting the tab button will change the focus from one Text widget to another. Whenever I'm in a Text widget, tab only indents the text cursor.
Does anyone know if this is configurable?
This is very easy to do with Tkinter.
There are a couple of things that have to happen to make this work. First, you need to make sure that the standard behavior doesn't happen. That is, you don't want tab to both insert a tab and move focus to the next widget. By default events are processed by a specific widget prior to where the standard behavior occurs (typically in class bindings). Tk has a simple built-in mechanism to stop events from further processing.
Second, you need to make sure you send focus to the appropriate widget. There is built-in support for determining what the next widget is.
For example:
def focus_next_window(event):
event.widget.tk_focusNext().focus()
return("break")
text_widget=Text(...)
text_widget.bind("<Tab>", focus_next_window)
Important points about this code:
The method tk_focusNext() returns the next widget in the keyboard traversal hierarchy.
the method focus() sets the focus to that widget
returning "break" is critical in that it prevents the class binding from firing. It is this class binding that inserts the tab character, which you don't want.
If you want this behavior for all text widgets in an application you can use the bind_class() method instead of bind() to make this binding affect all text widgets.
You can also have the binding send focus to a very specific widget but I recommend sticking with the default traversal order, then make sure the traversal order is correct.
It is really simple in PyQt4 simply use this one single line below and you will be able to change focus by pressing tab button:
self.textEdit.setTabChangesFocus(True)
The focus traversal is somewhat customizable, usually letting the X windows manager handle it (with focus follows mouse, or click). According to the manual it should be possible to bind an event to the key press event, for tab presses, and triggering a focusNext event in those cases.

Categories