Working with QTextEdit, QLineEdit, and the sender() Function in Qt
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertText("hello");
ui->textEdit->append("hello");
QString selectedText = ui->textEdit->textCursor().selectedText();
Modal and Modeless Dialogs
// Modal dialog
int outcome = dialog.exec();
// Modeless dialog
dialog.show();
dialog.setAttribute(Qt::WA_DeleteOnClose);
if (outcome == Qt::Accepted) {
// Accepted handling
} else if (outcome == Qt::Rejected) {
// Rejected handling
}
textChanged(const QString &text)
- Signal Description: Emitted whenever the text content in a QLineEdit changes.
- Parameter:
textcontains the complete current text of the QLineEdit, including the latest modifications. - Trigger: Fired on any text change, whether from keyboard input, paste operations, or undo/redo actions.
textEdited(const QString &text)
- Signal Description: Emitted when the user directly edits the QLineEdit content.
- Parameter:
textrepresents the current text after the most recent user edit. - Trigger: Only occurs when the user modifies the text via keyboard or direct input, not when changes are made programmatically.
// Set text in a QLineEdit
ui->lineEdit->setText("Hello World!");
// Move cursor to the start of the text
ui->lineEdit->setCursorPosition(0);
// Clear any text selection
ui->lineEdit->setSelection(0, 0);
The QLineEdit will display "Hello World!", with the cursor positioned at the beginning and no text selected.
sender() is a utility function in Qt's signals and slots mechanism that returns a pointer to the object that emitted the signal currently being processed. It is typically called within a slot to identify the signal source.
QWidget *widget = qobject_cast<QWidget*>(sender());
QPair<int, int> coordinate(10, 20);
qDebug() << coordinate.first << coordinate.second;
coordinate.first = 20;
coordinate.second = 80;
QPair<int, int> anotherCoordinate = qMakePair(80, 90);
qDebug() << anotherCoordinate.first << anotherCoordinate.second;