The documentation for the textEdited signal shows the use of a required parameter const QString&

void textEdited (const QString&)

I am unable to get the Signal to work. Perhaps because I do not understand how to use the "const QString&"

Is this a literal copy into the parameter or is it created from something else first?

I have a similar problem with

void itemActivated (QListWidgetItem *)

where it appears they are using a pointer to a QListWidgetItem

Coming from a VB6 background where C++ constructs do not exist.

I could really use some help with this.

Thanks!

Dani AI

Generated

Quick summary for : the C++ signatures you saw (a Qt string passed by reference, or a pointer to a QListWidgetItem) are C++ implementation details. PyQt wraps those for you — a Qt string argument becomes a normal Python string (unicode under Python 3) and an item pointer becomes a wrapped QListWidgetItem object you can call .text() on. This means you don’t need to manage references or pointers by hand in Python. (doc.bccnsoft.com)

Practical note about QLineEdit signals: the user-edit signal is emitted only when the user types (not when you call setText()), and the handler receives the text as a Python string. Prefer the modern (new-style) signal API when possible because it’s clearer and gives better error checking. Example pattern (PyQt4 ≥4.5 / PyQt5+):

self.line_edit.textEdited.connect(self.on_text_edited)

def on_text_edited(self, text):
    # 'text' is a Python str/unicode
    self.label.setText(text)

The Qt docs describe the difference between textEdited and textChanged. (tool.oschina.net)

For QListWidget: connect to the activation signal and accept the single item parameter; get the string with item.text():

self.list_widget.itemActivated.connect(self.on_item_activated)

def on_item_activated(self, item):
    print(item.text())   # item is a QListWidgetItem wrapper

That matches the Qt signal that carries the item pointer. (tool.oschina.net)

Troubleshooting tips (why a connection sometimes seems to “not work”): make sure the widget still exists (store as self.xxx or give it a parent), ensure your slot signature accepts the right number/type of arguments, and if a widget has overloaded signals use the indexed form (e.g. textChanged[str].connect(...)) to pick the correct overload. ’s old-style example is valid, but switching to new-style calls reduces subtle bugs. (docs.huihoo.com)

Recommended Answers

All 4 Replies

That is not Python, it is C++. Before transferring to C++ forume, why do you have pyQT in title?

I think in C and C++ the & operator points to the string (array of characters). In Python that would simply be the string.

If you wanted to transfer from QLineEdit to a QLabel as you type you would use:

QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.label.setText) 

Dang, is it ever tough to work wit the new code areas on DaniWeb!

Here is a simple example:

# explore PyQT QLineEdit and QLabel connection

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyForm(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, 300, 120)
        self.setWindowTitle("start typing")

        line_edit = QLineEdit(self)
        label = QLabel(self)
        # connect line_edit to label
        # update label each time the text has been edited
        line_edit.connect(line_edit, SIGNAL("textEdited(QString)"), label.setText)

        # use a grid layout for the widgets
        grid = QGridLayout()
        # addWidget(widget, row, column, rowSpan, columnSpan)
        grid.addWidget(line_edit, 0, 0, 1, 1)
        grid.addWidget(label, 1, 0, 1, 1)
        self.setLayout(grid)

app =  QApplication([])
form = MyForm()
form.show()
app.exec_()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.