QTextEdit *editor = new QTextEdit(); QTextCursor cursor = editor->textCursor(); if (cursor.hasSelection()) { // do something with the selected text } else { // do something else }
QPushButton *deleteButton = new QPushButton("Delete"); QTextEdit *editor = new QTextEdit(); QTextCursor cursor = editor->textCursor(); connect(editor, &QTextEdit::cursorPositionChanged, [=]() { if (cursor.hasSelection()) { deleteButton->setEnabled(true); } else { deleteButton->setEnabled(false); } });In this example, we create a QPushButton object and a QTextEdit widget object. We connect a lambda function to the QTextEdit's cursorPositionChanged signal that checks whether the cursor has a selection or not. Depending on the result of the check, we enable or disable the delete button accordingly. Package library: Qt Core.