Interesting problem. The only way I found was to rely on a private header:
#include <QtGui>
#include <private/qtextcontrol_p.h>
{
Q_OBJECT
public:
{
setText("Qt Centre");
setTextFormat(Qt::RichText); // <-- mandatory to force QTextDocument to be created
setTextInteractionFlags(Qt::TextSelectableByMouse);
QTextControl* control = findChild<QTextControl*>();
Q_ASSERT(control);
connect(control, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
}
private slots:
void onSelectionChanged()
{
QTextControl* control = findChild<QTextControl*>();
Q_ASSERT(control);
qDebug() << cursor.selectionStart() << cursor.selectionEnd() << cursor.selectedText();
}
};
int main(int argc, char* argv[])
{
Label label;
label.setText("Qt Centre");
label.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <private/qtextcontrol_p.h>
class Label : public QLabel
{
Q_OBJECT
public:
Label(QWidget* parent = 0) : QLabel(parent)
{
setText("Qt Centre");
setTextFormat(Qt::RichText); // <-- mandatory to force QTextDocument to be created
setTextInteractionFlags(Qt::TextSelectableByMouse);
QTextControl* control = findChild<QTextControl*>();
Q_ASSERT(control);
connect(control, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
}
private slots:
void onSelectionChanged()
{
QTextControl* control = findChild<QTextControl*>();
Q_ASSERT(control);
QTextCursor cursor = control->textCursor();
qDebug() << cursor.selectionStart() << cursor.selectionEnd() << cursor.selectedText();
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Label label;
label.setText("Qt Centre");
label.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks