Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Displaying real-time data on a 2nd window



  • So the idea is that I have a window where I display a map of a country. Through a button I open another new window (auxiliary to the 1st window, I mean both windows exist after the opening of the 2nd window) where it should display some data. Everytime I move the mouse pointer (on the map) to different places, the second window should update the values and show new data, which does not happen. In order to check that the code works, I also print the data (that should be displayed and updated in real-time on the second window) on the terminal which work just fine! What I am missing here?

    In C++ I think this a volatile variable problem. But in python I have no idea! Is this a thread problem? Any ideas? (I work on PyQt5)



  • @john_hobbyist
    It should be perfectly doable. You will have to show some code.

    But in python I have no idea! Is this a thread problem?

    Hopefully you are not using threads anyway? If you do, again show relevant code.



  • The 2nd window that displays the names of the towns (from where the pointer points on the 1st map) keeps only the 1st town I pointed. When I change position to the mouse pointer, it does not refresh the name on the 2nd window. I use a .csv file in order to pass data from the 1st window (class) to the 2nd window (class). Can you guide me? What should I check?


  • Lifetime Qt Champion

    Hi,

    Did you check that the signal arrives ?
    What are you using to show the information ?
    How do you do it ?



  • @john_hobbyist said in Displaying real-time data on a 2nd window:

    I use a .csv file in order to pass data from the 1st window (class) to the 2nd window (class).

    Please first answer @SGaist's questions. But if you are using a file somehow to pass data between two windows rather than a signal that is likely not a good idea.



  • @SGaist said in Displaying real-time data on a 2nd window:

    Hi,

    Did you check that the signal arrives ?
    What are you using to show the information ?
    How do you do it ?

    1. How I check them? With "print" commands do you mean?
    2. QLineEdit (on the 2nd window)
    3. I have a .csv with coordinates and the towns' name, when I change coordinates (by moving the mouse pointer) the name of the town on the 2nd window should change/refresh.

    What I see now, is that If I close the 2nd window and open a new one (2nd window again), the town's name is refreshed. But I cannot continuously refresh the 2nd window, the text inside the QLineEdit should be updated only... Do I miss something here?


  • Lifetime Qt Champion

    Yes, print is one option.

    Please show us the code you are using.



  • From google search I found this: https://stackoverflow.com/questions/14630863/changing-text-in-qlineedit-of-qt-application-in-real-time and this: https://pythonpyqt.com/qtimer/

    So, how I attach Qtimer to a QLineEdit and update the text it has inside?



  • @john_hobbyist said in Displaying real-time data on a 2nd window:

    So, how I attach Qtimer to a QLineEdit and update the text it has inside?

    Since both of your links, especially the first, show you, what is the question?



  • @JonB said in Displaying real-time data on a 2nd window:

    @john_hobbyist said in Displaying real-time data on a 2nd window:

    So, how I attach Qtimer to a QLineEdit and update the text it has inside?

    Since both of your links, especially the first, show you, what is the question?

    It is in C++...


  • Lifetime Qt Champion

    Indeed but that code is not really hard to translate to Python.



  •     def __init__(self):
            super().__init__()
            import sys
            from PyQt5.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,QLabel
            from PyQt5.QtCore import QTimer,QDateTime
            from PyQt5 import QtWidgets
            from PyQt5.QtWidgets import QApplication, QMainWindow
            from PyQt5 import QtCore
            
            timer = QtCore.QTimer()
            timer.timeout.connect(self.ShowText)
            timer.start(1000)
            
        def ShowText(self):
            import pandas
            win = QWidget()   
            grid=QGridLayout()   
            ct = pandas.read_csv('CurrentTown.csv')   
            town = QLabel("town:")   
            town.setStyleSheet("border: 1px solid black; background-color: yellow")   
            grid.addWidget(town , 1,1)   
            self.textEdit_1 = QLineEdit(ct)   
            self.textEdit_1.setReadOnly(True)   
            grid.addWidget(self.textEdit_1,1,2)   
                   
            import os
            os.remove('CurrentTown.csv')   
            self.setLayout(grid)   
            win.setGeometry(100,100,200,100)   
            win.setWindowTitle("Test")   
            win.show()
    

    It does not display even a static text...and obviously it does not update anything on the textboxes, any ideas why?


  • Lifetime Qt Champion

    That code does not define a widget nor anything within it so it's pretty hard to tell you what's wrong.



  • I updated it again...Any ideas???


  • Lifetime Qt Champion

    Why are you recreating the whole widget each time ?
    Why are you deleting the data file each time as well ?



  • @SGaist I put

    win = QWidget()   
    grid=QGridLayout()
    

    inside

    def __init__(self):
    

    again the same thing! One widget without any even a QLabel, QLineEdit, no data, nothing...

    I use the .csv in order to pass and get value from another method from another class.

    Any ideas?


  • Lifetime Qt Champion

    Your code is incomplete so it's not possible to answer.
    However one thing is sure: creating and deleting a file to communicate between two classes in the same application is not the correct solution.

    If you want more help provide a minimal runnable script that shows what you are trying to do.



  • How do I make this command:

    qle.textChanged[str].connect(self.onChanged)
    
    

    (source: https://zetcode.com/pyqt/qlineedit/)

    change the text in QLineEdit when "ct" variable has different value. "ct" variable is in my code...


  • Lifetime Qt Champion

    @SGaist said in Displaying real-time data on a 2nd window:

    If you want more help provide a minimal runnable script that shows what you are trying to do.



  • @john_hobbyist said in Displaying real-time data on a 2nd window:

    change the text in QLineEdit when "ct" variable has different value. "ct" variable is in my code...

    You ask about a piece of code which has no "ct variable" used in it. How do you think anybody can answer this?



  • @JonB said in Displaying real-time data on a 2nd window:

    @john_hobbyist said in Displaying real-time data on a 2nd window:

    change the text in QLineEdit when "ct" variable has different value. "ct" variable is in my code...

    You ask about a piece of code which has no "ct variable" used in it. How do you think anybody can answer this?

    Please look at my code above. I have posted the code and where "ct" is used...



  • @john_hobbyist

    ct = pandas.read_csv('CurrentTown.csv')
    

    We don't know what pandas.read_csv() returns. I would guess some kind of table? So I don't know what you expect to put in a QLineEdit for its "value".

    Anyway, if you have qle.textChanged[str].connect(self.onChanged) that means self.onChanged() will be called when qle's text changes. If qle is the QLineEdit you are talking about

    change the text in QLineEdit when "ct" variable has different value.

    then that connection is not relevant. If this is what you want, then "when "ct" variable has different value" however that happens just call qle.setText(str(ct)) or whatever you want. Else you mean something different.



  • @JonB said in Displaying real-time data on a 2nd window:

    @john_hobbyist

    ct = pandas.read_csv('CurrentTown.csv')
    

    We don't know what pandas.read_csv() returns. I would guess some kind of table? So I don't know what you expect to put in a QLineEdit for its "value".

    Anyway, if you have qle.textChanged[str].connect(self.onChanged) that means self.onChanged() will be called when qle's text changes. If qle is the QLineEdit you are talking about

    change the text in QLineEdit when "ct" variable has different value.

    then that connection is not relevant. If this is what you want, then "when "ct" variable has different value" however that happens just call qle.setText(str(ct)) or whatever you want. Else you mean something different.

     ct = pandas.read_csv('CurrentTown.csv')
    

    holds the name of one town each time I read the .csv. nothing else. It is use in order to pass the name of the town from another class/method and that's it. When I move the cursor of the mouse to another town the .csv stores the other new town (from the other class/method) and I read it with the "ct". I only need to update the town name on the QLineEdit field...



  • @john_hobbyist

    Everytime I move the mouse pointer (on the map) to different places, the second window should update the values and show new data, which does not happen.

    Show the code you use to connect the mouse move signal or event in the first window to the slot in the second window.

    If you say everything works when you output the town name on the terminal (print() maybe?), and now want to display it on a QTextEdit instead, then as I already said all you have to do is call the line edit's setText().



  • class Town(QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()
            
        def initUI(self):
            import sys
            from PyQt5.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,QLabel
            from PyQt5.QtCore import QTimer,QDateTime
            from PyQt5 import QtWidgets
            from PyQt5.QtWidgets import QApplication, QMainWindow
            from PyQt5 import QtCore
            
            win = QWidget()
            grid=QGridLayout()
           
            import pandas
            ct = pandas.read_csv('CurrentTown.csv')
            town = QLabel("town:")
            town.setStyleSheet("border: 1px solid black; background-color: yellow")  
            grid.addWidget(town, 1,1)
            textEdit_1 = QLineEdit(ct)
            textEdit_1.textChanged[str].connect(self.onChanged)
            textEdit_1.setReadOnly(True)
            grid.addWidget(textEdit_1,1,2)
    
            
            import os
            os.remove('CurrentTown.csv')
        
            self.setLayout(grid)
            win.setGeometry(100,100,200,100)
            win.setWindowTitle("Test")
            win.show()
            
        def onChanged(self, text):
             self.textEdit_1.setText(str(ct))
    

    Any ideas??



  • @john_hobbyist said in Displaying real-time data on a 2nd window:

    textEdit_1.textChanged[str].connect(self.onChanged)

    I already said. You think you want to connect QLineEdit.textChanged signal to do something. That is when something changes the text. But you don't have anything changing the text. You need a signal for when the file content changes to connect to a slot to change the text in the line edit.

    You claimed you had it working printing on terminal fine, but I don't see that.

    Conceptually the code you have to read the file must be in a method called when the text in the file is changed. You have all this code in the constructor of the Town widget, which cannot be right.

    There is no connection at all between your textEdit_1 = QLineEdit(ct) and the self.textEdit_1.setText(str(ct)) you use later. Please review local versus member variables in Python.

    You don't want to have any self.textEdit_1 in class Town as the line edit you want to address is not on the Town widget, it is on the win widget.

    Start by creating a separate Python module and class for your second window. Put the code to read the file and set the line edit text there. Then in this Town module create an instance of that class and show it. In Town you will have some connection like:

    self.signalForFileDataChanged.connect(self.otherWindow.onFileDataChanged)
    

    where the second window's onFileDataChanged() reads the new text from the file and sets its line edit.

    When you have that working you may see that you could have the Town.signalForFileDataChanged() signal send a parameter of the new town name to the OtherWindow.onFileDataChanged() slot for it to show. Then you can ditch having them communicate via the content of an external file.



  • I have another method that I want to call from another class. I have this code:

    class Two(CentralWindow):
        def do_something(self, checked):
        . . .
    

    I want to call the do_something method from initUI(self) ??
    I have tried google search solutions, but they show to call a method outside a class/method. I need to call the do_something method from inside initUI(self) method....



  • @john_hobbyist
    Fundamental Python. Nothing special about initUI(self).

    • From within class Two: self.do_something(checked).
    • From outside class Two: class_two_instance.do_something(checked).


  • This post is deleted!


  • In order to start from somewhere, after your very useful comments I did a change in the code so that when the ct changes to call the method def onChanged(self, text) which prints "OK". However, this happens only the first time that ct changes value. All the next times it does not print "OK"...What do I miss here???


  • Lifetime Qt Champion

    I may sound harsh but: proper architecture.

    I would suggest to start with something really minimal to just learn to use signals and slots properly, how to manage multiple different complex widgets that are only linked together by signals and slots.



  • I am trying! How can I call a method before declaring it?


  • Lifetime Qt Champion

    You just don't.

    As I already suggested: start with Qt's basics.

    • Single widget with signals and slots within it.
    • Second widget completely unrelated to the first one
    • Connect them only after they both properly work independently
    • While doing that: write unit tests


  • @SGaist Thank you, I will try it.

    Please tell me a reason that the code runs commands such as open/make a widget + QLinedit + QLabels (I checked it with print commands) but I see only the widget (window) and nothing inside... What is wrong?


  • Lifetime Qt Champion

    @john_hobbyist said in Displaying real-time data on a 2nd window:

    What is wrong?

    Please show the code, else only guessing is possible!
    Did you add these widgets to your container widget (main window or whatever)?



  • I am trying to embed a grid layout to the right side of the mainwidget. Imagine that I have divided the mainwidget area into 2 areas, left and side. I get this error though:

    File "code.py", line 1643, in __init__
      mylayout.addWidget(self.imageviewer, 1, 1)
    TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 3 has unexpected type 'int'
    

    What did I do wrong?


  • Lifetime Qt Champion

    Are you sure mylayout is a QGridLayout ?



  • I have it like this:

    mylayout = QHBoxLayout()
    

  • Lifetime Qt Champion

    So since it's a QHBoxLayout, why are you trying to use it like a grid layout ?

    The error message tells you exactly the problem you have. If you read the method documentation, you'll see that the second parameter shall be a value from the Alignment enum.



  • This post is deleted!

Log in to reply