Results 1 to 3 of 3

Thread: QLabel selection: start and end position?

  1. #1
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLabel selection: start and end position?

    I have no idea if this is possible, but i need to figure out how to get the start and end position of the selected text in a QLabel. I have multiple QLabels that can have their text selected, so i also need to know how to pick up which one has text selected. I use QLabels because it's the only widget i know that doesn't supply its own scrollbars when there is a lot of content; i need the widget to stretch out as much as the content needs. However, if the same can be done with other widgets (i.e. stretch out according to content), then i'm all ears. I can pretty much figure out how to obtain the selected text in a QLabel, but not the start and end positions.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLabel selection: start and end position?

    Interesting problem. The only way I found was to rely on a private header:
    Qt Code:
    1. #include <QtGui>
    2. #include <private/qtextcontrol_p.h>
    3.  
    4. class Label : public QLabel
    5. {
    6. Q_OBJECT
    7. public:
    8. Label(QWidget* parent = 0) : QLabel(parent)
    9. {
    10. setText("Qt Centre");
    11. setTextFormat(Qt::RichText); // <-- mandatory to force QTextDocument to be created
    12. setTextInteractionFlags(Qt::TextSelectableByMouse);
    13.  
    14. QTextControl* control = findChild<QTextControl*>();
    15. Q_ASSERT(control);
    16. connect(control, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
    17. }
    18.  
    19. private slots:
    20. void onSelectionChanged()
    21. {
    22. QTextControl* control = findChild<QTextControl*>();
    23. Q_ASSERT(control);
    24. QTextCursor cursor = control->textCursor();
    25. qDebug() << cursor.selectionStart() << cursor.selectionEnd() << cursor.selectedText();
    26. }
    27. };
    28.  
    29. int main(int argc, char* argv[])
    30. {
    31. QApplication app(argc, argv);
    32. Label label;
    33. label.setText("Qt Centre");
    34. label.show();
    35. return app.exec();
    36. }
    37.  
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    truefusion (18th January 2009)

  4. #3
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel selection: start and end position?

    Didn't realize QTextControl existed. Thanks for the help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.