How do I cut a single line with polygons (in several segments) so that each cut line take data from the polygons? Something like a centroid takes.
Since this appears to solve the users problem Ill add it here.
In ArcGIS and QGIS The intersect tool splits the lines at the polygon boundaries and does keep the attributes of each line feature as well as adds in the attributes of the polygon feature that it lies in if the Join Attributes selection is set to ALL.
Just to revive an old question, because this topic is seldom discussed (I wonder why), here is another option using PyQGIS:
If you want to clip a single Geometry by a single rectangle, there is a
QgsGeometry::clipped() function that does precisely that.
For clipping a line, one can ignore the alert that this function is not robust - it creates valid geometries from the point where the line enters the rectangle until the point leaving it. When entering the rectangle and leaving it multiple times, the function creates a multilinestring geometry with the intersection parts.