PyQt get value from GUI - python

I've built an User Interface using QtDesigner and then converted the .ui to .py. The User Interface has different comboBox and textBox from which I want to read the values once the Run button is clicked. Run a function and then populate other text boxes of the user interface once the calculations are completed. However when I change the value of the comboBox and click the button the script still reads the initial value.
I did a simple GUI with a comboBox with two items and a textBox. I'm trying to read the the comboBox text and based on the selected item set the text of the textBox.
Here is the code I'm using to run the GUI and read the value:
from PyQt4 import QtGui
from pyQt4 import QtCore
import sys
import GUI
class MyThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def run(self):
self.gui = Window()
name = self.gui.gui_Name.currentText()
print (name)
if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.updated.emit(str(1))
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self._thread = MyThread(self)
self._thread.updated.connect(self.updateText)
self.update()
self.
self.pushButton.clicked.connect(self._thread.start)
def updateText(self,text):
self.Country.setText(str(country))
Any thoughts?
Thanks

If the code that you implement in the run is the one that I think you are abusing the threads since with the currentTextChanged signal it would be enough as I show below:
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self.gui_Name.currentTextChanged.connect(self.onCurrentTextChanged)
def onCurrentTextChanged(self, text):
if if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.Country.setText(str(country))
On the other hand if the real code is a time-consuming task then the use of the threads is adequate. If the task takes as reference the value of the QComboBox at the moment of pressing the button then it establishes that value as property of the thread, in your case you are creating a new GUI in another thread instead of using the existing GUI:
class MyThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def run(self):
name = self.currentText
print(name)
if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.updated.emit(country)
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self._thread = MyThread(self)
self._thread.updated.connect(self.Country.setText)
self.pushButton.clicked.connect(self.start_thread)
def start_thread(self):
self._thread.currentText = self.gui_Name.currentText()
self._thread.start()

Related

QtCore.signal not doing anything

I am a beginner with PyQt5 and I am having trouble to use QtCore.signal
I'd like to send a signal when I press my buttons and switch the current widget displayed.
I don't have any errors when I run my code but when I press the buttons nothing happen and I guess it is because I am doing something wrong with the QtCore.Signal
Here is my code :
from PySide2 import QtCore, QtWidgets
from ui_Page_accueil import Ui_MainWindow
from ui_NouvelleVerif import Ui_Dialog as Ui_NouvelleVerif
from ui_NouvelleVerifEssieux import Ui_Dialog as Ui_NouvelleVerifEssieux
import sys
class MainWindowUi(Ui_MainWindow):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
#self.pushButton.clicked.connect(self.pushbutton_handler1)
self.pushButton_2.clicked.connect(self.pushbutton_handler2)
#def pushbutton_handler1(self):
# self.to_MainWindow.emit()
def pushbutton_handler2(self):
self.to_NouvelleVerif.emit()
class NouvelleVerifUi(QtWidgets.QWidget, Ui_NouvelleVerif):
to_MainWindow = QtCore.Signal()
to_NouvelleVerifEssieux = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
#self.pushButton_2.clicked.connect(self.pushbutton_handler2)
self.pushButton_3.clicked.connect(self.pushbutton_handler3)
def pushbutton_handler1(self):
self.to_MainWindow.emit()
#def pushbutton_handler2(self):
# self.switch_window.emit()
def pushbutton_handler3(self):
self.to_NouvelleVerifEssieux.emit()
class NouvelleVerifEssieuxUi(QtWidgets.QWidget, Ui_NouvelleVerifEssieux):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
def pushbutton_handler1(self):
self.to_NouvelleVerif.emit()
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
def show_MainWindow(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.MainWindow = MainWindowUi()
self.NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
def main():
controller = Controller()
controller.widget.show()
sys.exit(controller.app.exec_())
if __name__ == '__main__':
main()
#musicamante I don't know why I though the QtCore.Signal could be call even if he was in a function which has not been called. I did what you said and I realise I made another mistake with the widgets.
In the controller class they are create and add to the QStackedWidget in the __init__, but I was creating new ones and trying to set them as CurrentWidget without adding them to the QStackedWidget.
#alexpdev I wanted to navigate through my three differents with pushButton UI this way :
Start with MainWindowUI
MainWindowUI pushButton_2.clicked --> set the current widget display to NouvelleVerifUI
NouvelleVerifUI pushButton_1.clicked --> set the current widget display back to MainWindowUI
NouvelleVerifUI pushButton_3.clicked --> set the current widget display to NouvelleVerifEssieuxUI
NouvelleVerifEssieuxUI pushButton_1.clicked --> set the current widget display back to NouvelleVerifUI
Now everything work I did what you said #musicamante and I also use the UI created at first.
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
def show_MainWindow(self):
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
Thank you all for your time

Pyqt Pass Variable to QDialog Class

I have developed a QGIS plugin using Pyqt. I now have a need within this plugin to have a QDialog popup have its fields populated with data from our database.
My problem is how to pass a variable (in this case it might be a table and row reference) into a class and have it used by the static method.
If I print a passed in variable from within the class it will return the variable in addition to None?? If I take the same variable and try and populate a qplaintextedit it won't work because its complaining of being type 'None'.
Here is some test code I'm trying out just to get the concept down...
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import pdb
class mydi(QDialog):
def __init__(self, pass_var, parent=None):
super(mydi, self).__init__(parent)
layout = QVBoxLayout(self)
self.pass_var = pass_var
print pass_var
self.txt_comments = QPlainTextEdit(self)
self.txt_comments.appendPlainText(pass_var)
layout.addWidget(self.txt_comments)
self.buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
layout.addWidget(self.buttons)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
def something(self):
return self.somevar
def comments(self):
return self.txt_comments.toPlainText()
#staticmethod
def getData(parent=None):
dialog = mydi(None)
dialog.exec_()
return (dialog.comments())
def main():
app = QApplication([])
pass_in_var = "test"
dia = mydi(pass_in_var)
data = dia.getData()
print data

What is the correct way to read strings or send strings to a qtextbrowser from another class using python and pyqt gui?

I'm trying to send a string from one class to a qtextbrowser located in another class, my gui is built in pyqt and the code i wrote it in python 2.7.
Here is my test code:
class print_text():
def __init__(self):
text1 = "Acesta este un text de proba"
self.classMyWindow = MyWindow()
self.classMyWindow.statusText_updater("Merge ok ")
class systemValues(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def cpuRunValue(self):
text1 = "Acesta este un text de proba"
self.classMyWindow = MyWindow()
self.classMyWindow.statusText_updater("Merge ok ")
def run(self):
self.cpuRunValue()
class MyWindow(QtGui.QMainWindow):
def __init__(self):
#QtGui.QDialog.__init__(self)
super(MyWindow, self).__init__()
file_path = os.path.abspath("im-manager.ui")
uic.loadUi(file_path, self)
self.myThread = systemValues()
self.myThread.start()
def statusText_updater(self,text):
time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
read1 = self.status.toPlainText()
self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ")
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
# app.exec_()
sys.exit(app.exec_())
I get this error:
QPixmap: It is not safe to use pixmaps outside the GUI thread
What is the correct way to read strings or send strings to a qtextbrowser from another class?
I need this because my app needs to read some cpu and ram values on a different thread to keep my app from freezing and display a text message when the job is done.
UI File
To show data from a QThread in a window it is not necessary to create an instance, since the one you are creating will be different from the original window.
To display data from the thread to the GUI you must do it through signals and connect it to a slot that updates the data each time the signal is emitted.
In this case we will create the newMessage signal and emit it:
newMessage = QtCore.pyqtSignal(str)
#emit signal
self.newMessage.emit("Merge ok ")
Then we connect it to the statusText_updater slot
self.myThread.newMessage.connect(self.statusText_updater)
Complete code:
class systemValues(QtCore.QThread):
newMessage = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def __del__(self):
self.wait()
def cpuRunValue(self):
text1 = "Acesta este un text de proba"
self.newMessage.emit("Merge ok ")
def run(self):
self.cpuRunValue()
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent=parent)
file_path = os.path.abspath("im-manager.ui")
uic.loadUi(file_path, self)
self.myThread = systemValues()
self.myThread.newMessage.connect(self.statusText_updater)
self.myThread.start()
def statusText_updater(self,text):
time = datetime.datetime.now()
time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
read1 = self.status.toPlainText()
self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ")
As this is now your QThread class will only display once the message, if you want the message to be shown from time to time you must modify the run method:
def run(self):
while True:
self.cpuRunValue()
time.sleep(1)
This error is occurring because of your threading, not just because you have multiple classes. Whenever threading, you should always use signals and slots to communicate changes within the GUI thread that you would like to trigger. Check out this link for comprehensive examples:
http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/

Signal Not coming though - PySide

I am sending a signal from another class to update a PySide QTableWidget but nothing is coming though. I have made this very simple for this demonstration:
This is in the controller module called Records.py
class Records(QDialog, randomDialog.Ui_watchingDialog):
signal = 1
atSig = Signal(int)
def add_button_clicked(self):
# Do some stuff
self.signal = 1
self.atSig.emit(self.signal)
# Do some other Stuff
This sits out side the controller called main.py
from controller import Records
class main(QMainWindow, pyMainWindow.Ui_mainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
signal_records = Records.Records()
signal_records.atSig.connect(self.showNewData)
def showNewData(self, signal):
if signal == 1:
print "It worked!"
else:
print "Problem"
How come this signal is not coming through? No error messages are being thrown and neither of the print statements aren't being called. How can I fix this?
signal_records falls out of scope as soon as main.__init__() returns and is garbage collected. You need to make it a member of main so that it persists for the lifetime of the class.
self.signal_records = Records.Records()
self.signal_records.atSig.connect(self.showNewData)
Alternatively, you could assign main as the parent of signal_records
signal_records = Records.Records(self)
Both methods ensure a reference to signal_records sticks around.
If It's possibly, please define this before caller to receive;
.
.
#Slot (int)
def showNewData(self, signal):
.
.
Or it not, Please check your caller def add_button_clicked(self). I work in pyqt4 (same pySide) and (I cut some part out and put some path for test in your) code, It's work.
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class QRecordsDialog (QtGui.QDialog):
addButtonClickedSignal = QtCore.pyqtSignal(int)
def __init__ (self, parent = None):
super(QRecordsDialog, self).__init__(parent)
self.myQPushButton = QtGui.QPushButton('Test Signal', self)
self.myQHBoxLayout = QtGui.QHBoxLayout()
self.myQHBoxLayout.addWidget(self.myQPushButton)
self.setLayout(self.myQHBoxLayout)
self.myQPushButton.clicked.connect(self.addButtonClicked)
def addButtonClicked (self):
self.addButtonClickedSignal.emit(1)
class QMainWindow (QtGui.QMainWindow):
def __init__ (self, parent = None):
super(QMainWindow, self).__init__(parent)
myQRecordsDialog = QRecordsDialog(self)
myQRecordsDialog.addButtonClickedSignal.connect(self.showNewData)
myQRecordsDialog.show()
#QtCore.pyqtSlot(int)
def showNewData (self, signal):
if signal == 1:
print "It worked !"
else:
print "Problem ?"
myQApplication = QtGui.QApplication(sys.argv)
myQMainWindow = QMainWindow()
myQMainWindow.show()
sys.exit(myQApplication.exec_())
If you want to modify your PyQt code to use the PySide naming scheme, that can be done using a simple definition:
QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot
Reference this
Regards,

How to stop QTreeWidget from creating the item duplicates on Drag and Drop

The code below creates QTreeWidget with five items.
The self.setDragDropMode(self.InternalMove) flag assures that when the item is dragged on top of
another there will be no copy of it made (so the number of items always stay the same).
If we replace this line with self.setDragDropMode(self.DragDrop) then every time an item is dragged/dropped a new copy of will be created.
Since I don't want a copy of item to be created on every dragAndDrop event I would be happy with InternalMove flag if it wouldn't be blocking QTreeWidget from accepting the drops from outside of its own view (if InternalMove flag is set QTreeWidget does not allow dragging-dropping from another QTreeWidget, QListView or File Browser).
Is there a way to set an override so QTreeWidget doesn't create a duplicate of dragged item and yet allows a dropping from outside of its own window.
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Tree(QtGui.QTreeWidget):
def __init__(self, *args, **kwargs):
super(Tree, self).__init__()
self.setDragEnabled(True)
self.setDropIndicatorShown(True)
self.setDragDropMode(self.InternalMove)
items=[QtGui.QTreeWidgetItem([name]) for name in ['Item_1','Item_2','Item_3','Item_4','Item_5']]
self.addTopLevelItems(items)
self.resize(360,240)
self.show()
tree=Tree()
sys.exit(app.exec_())
The key to solving this problem is that you have to implement when the object has moved to the next QListsWidget and check whether or not the data is duplicated or not. Take the data from source to destination by removing the source and adding this data to destination QListsWidget.
Use two methods, dragEnterEvent and dropEvent, to handle them all;
Implemented dragEnterEvent Check object to move is same QListsWidget.
Implemented dropEvent Check data is duplicated or not And take data from source to destination.
Example:
import sys
from PyQt4 import QtCore, QtGui
class QCustomTreeWidget (QtGui.QTreeWidget):
def __init__(self, parent = None):
super(QCustomTreeWidget, self).__init__(parent)
self.setDragEnabled(True)
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.resize(360,240)
def dragEnterEvent (self, eventQDragEnterEvent):
sourceQCustomTreeWidget = eventQDragEnterEvent.source()
if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget):
if self != sourceQCustomTreeWidget:
sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
eventQDragEnterEvent.accept()
else:
sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
QtGui.QTreeWidget.dragEnterEvent(self, eventQDragEnterEvent)
else:
QtGui.QTreeWidget.dragEnterEvent(self, eventQDragEnterEvent)
def dropEvent (self, eventQDropEvent):
sourceQCustomTreeWidget = eventQDropEvent.source()
if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget):
if self != sourceQCustomTreeWidget:
sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
sourceQTreeWidgetItem = sourceQCustomTreeWidget.currentItem()
isFound = False
for column in range(0, self.columnCount()):
sourceQString = sourceQTreeWidgetItem.text(column)
listsFoundQTreeWidgetItem = self.findItems(sourceQString, QtCore.Qt.MatchExactly, column)
if listsFoundQTreeWidgetItem:
isFound = True
break
if not isFound:
(sourceQTreeWidgetItem.parent() or sourceQCustomTreeWidget.invisibleRootItem()).removeChild(sourceQTreeWidgetItem)
self.invisibleRootItem().addChild(sourceQTreeWidgetItem)
else:
sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
QtGui.QTreeWidget.dropEvent(self, eventQDropEvent)
else:
QtGui.QTreeWidget.dropEvent(self, eventQDropEvent)
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.my1QCustomTreeWidget = QCustomTreeWidget(self)
self.my2QCustomTreeWidget = QCustomTreeWidget(self)
items = [QtGui.QTreeWidgetItem([name]) for name in ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']]
self.my1QCustomTreeWidget.addTopLevelItems(items)
self.allQHBoxLayout = QtGui.QHBoxLayout()
self.allQHBoxLayout.addWidget(self.my1QCustomTreeWidget)
self.allQHBoxLayout.addWidget(self.my2QCustomTreeWidget)
self.setLayout(self.allQHBoxLayout)
app = QtGui.QApplication([])
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.show()
sys.exit(app.exec_())
Useful reference for event handle

Categories