0

I am trying to get all values of the object pg.InfiniteLine() using getXPos() method.

I tried the following (this code snippet is set inside of class MainWindow(QMainWindow)):

inf = pg.InfiniteLine(movable=True, angle=90, label='x={value:.3f}:{value:.3f}', 
                       labelOpts={'position':0.1, 'color': (200,200,100), 'fill': (200,200,200,50), 'movable': True})
inf.setPos([0,0])
self.graphWidget.addItem(inf)

Note: value in label='{value:.3f}:{value:.3f}' is after converting a list of strings t_string_list[:5] = ['0:0', '0:1', '0:2', '0:3', '0:4'] (of format H:M) to minutes stored in a variable t_time_value.

What I am trying to do is that label be set back to x=H:M displayed in the graph. To do that, I am trying to get all values of getXPos() for me to use Converting a float to hh:mm format.

I tried to do print(inf.getXPos()) but prints only 0. I also tried print(inf.getXPos(t_time_value)) I got TypeError: getXPos() takes 1 positional argument but 2 were given. I am not sure how to label the InfiniteLine with x=H:M where H:M should be values using label='{value:.3f}:{value:.3f}'.format(*divmod(pos / 60, 1)) argument of inf where I am facing difficulties to define pos.

This post is continuous to How to plot hour:min time in ISO 8601 time format?

Note: label='{value:.3f}:{value:.3f}' already gives correct positions whenever I move the line, but I couldn't do like this label='{value:.3f}:{value:.3f}'.format(value/60, 1) because value isn't defined.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
Joe
  • 575
  • 6
  • 24
  • Your first sentence doesn't make a lot of sense, as you cannot get "all values" of a line. Also, `getXPos()` cannot obviously accept arguments, since it returns the *current* x position. Now, the question is: if you want to convert the value to 'H:M', why are you trying to use `.3f`, which will always get 3 decimal values? Also considering your previous question, it seems to me that you're a bit confused about time specifications and string formatting... – musicamante Aug 09 '22 at 17:48
  • I used the following: `self.inf.sigPositionChanged.connect(self.x_pos_is_changed)` and `def x_pos_is_changed(self): print(self.inf.getXPos())` now I am able to print all positions whenever I move the line, but I am not sure how to store the values to use them for my case! – Joe Aug 09 '22 at 18:34
  • I am trying to go step by step, I believe being able to store the values of `getXPos()` so I can use them in `' '.format()` will allow me to review how to change the time. – Joe Aug 09 '22 at 18:36
  • No, you cannot "store the values". This is a floating point based system, there's no way you can get "all possible values", since there are *infinite* values even between 0 and 1. – musicamante Aug 09 '22 at 18:41
  • I believe there is a way by using Signals and Slots. The data plotted is of fixed-size. I am just trying to use use the values to label the line with more appropriate label using the format `H:M` instead of real `minutes` only. `getXPos()` values are just minutes. – Joe Aug 09 '22 at 18:46
  • 1
    The fact that the data has fixed size is completely irrelevant: the position is *always* in real values, not integers. – musicamante Aug 09 '22 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247160/discussion-between-joe-and-musicamante). – Joe Aug 09 '22 at 19:07

1 Answers1

2

You cannot use evaluation in a basic format string, nor try to "get all values", since graphs use real numbers: you can have infinite points between 0 and 1.

The possible solution is to add the InfLineLabel manually, using a subclass that overrides the default behavior of valueChanged:

class InfTimeLabel(pg.InfLineLabel):
    def valueChanged(self):
        if not self.isVisible():
            return
        value = self.line.value()
        h, m = divmod(value, 60)
        text = '{}:{:06.3f}'.format(int(h), m)
        self.setText(text)
        self.updatePosition()

Then add it like this:

    infLine = pg.InfiniteLine(movable=True, angle=90)
    opts = {
        'position': 0.1,
        'color': (200, 200, 100),
        'fill': (200, 200, 200, 50),
        'movable': True
    }
    infLine.label = InfTimeLabel(infLine, **opts)

    self.graphWidget.addItem(infLine)
musicamante
  • 41,230
  • 6
  • 33
  • 58