Attributeerror: 'Qstring' Object has no attribute 'strip'

Asked

Viewed 220 times

4

I’m making a small application in Pyqt4 to understand how it works. In a certain part, I’m using a callback function to display in a QLabel the text that is typed in QtextEdit.

This text should be trimmed (remove spaces before and after the string).

When I tried to use the function strip present in str Python the following error occurred:

Attributeerror: 'Qstring' Object has no attribute 'strip'

From what I understand, in Pyqt, the string is not an object str and yes QString and QString does not have the method strip.

In that case, what can I do to get my string handled?

Code:

def onButtonOkClicked(self):

    def setText():
        text = self.line.toPlainText().strip();
        self.label.setText(text)

    QtCore.QObject.connect(self.buttonOk, QtCore.SIGNAL("clicked()"), setText)
  • Where is being used the strip()?

  • I don’t use much Qt in python (only in C++ anyway), but the QString has the function trimmed, removing whitespace at the beginning and end. It would be equivalent to strip? http://doc.qt.io/qt-4.8/qstring.html#trimmed

  • @C.E.Gesser puts, it worked. I thought it would be solved in another way :p

  • @bigown found the right code (the wrong code that generated the question). The idea of the question came up at the same time that I was editing here, kkkk

  • Qstring is different from python string, I think this is the reason for the problem, however I still do not understand the goal, I will try to run pyqt here to test.

  • @Guilhermenascimento as said in the question, the goal is to pass the text of Qtextedit to Qlabel. But I want Qlabel to be like the text "clean"

Show 1 more comment

3 answers

6


I’m just answering to explain something:

The strip (str.strip) is a method for Python strings, which is also supported by bytes (bytes.strip([chars]) and bytearray.strip([chars]))

Already the QString is different from str, resume it is a Qt object and will not have access to the same methods used in Python native strings.

You will have to use the methods of own QString as documented http://doc.qt.io/qt-5/qstring.html#public-functions

In the case as cited in the other answers, use the trimmed:

self.line.toPlainText().trimmed();

It is worth noting that there is no equivalent to ltrim(), rtrim() or pass arguments to change what type of character you want to trim, as occurs in strip([chars])

print("---foobar----".strip('-'))

that would result in foobar, then if you want certain operations that exist in str() use only str and then convert to QString (when necessary).

5

Can use simplified() of own QString. It is a more limited method, but it can work. Depending on what you want can be used to trimmed() also.

Can use replace(QString(" "), QString("")) if you want other characters.

Although I suggest avoiding, can also convert to the string python:

str(text)

The two encodings are completely different, so if you keep converting from one to the other every time you waste resources. When using Qt the idea is to get as much as possible with it and only convert when it is strictly necessary.

  • There is a part of the application that, to set the text as empty, I am doing self.line.setText(""). In this case, it is recommended to use self.line.setText(QtGui.QString(""))?

  • 1

    I think yes, better not mix text formats. If I am not mistaken have how to create a QString without going through the constructor, but it’s been a long time since I’ve touched Qt. I think it’s kind of new.

3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.