Results 1 to 5 of 5

Thread: QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyledIte

  1. #1
    Join Date
    May 2009
    Posts
    83

    Default QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyledIte

    i have simple item delegate from QStyledItemDelegate type inside its paint method i have this code . it renders fine , but the main thing here that i like the text to be
    selectable for copy , and this dosn't work .


    Qt Code:
    1. void ItemDelegate::paintBody( QPainter* painter,
    2. const QStyleOptionViewItem& option,
    3. const QModelIndex& index ) const {
    4. painter->save();
    5. QLabel *l = new QLabel();
    6. l->setTextFormat(Qt::RichText);
    7. l->setTextInteractionFlags(Qt::TextSelectableByMouse);
    8. l->setGeometry(option.rect);
    9. l->setText("This is test");
    10. l->setStyleSheet("QLabel { background-color : transparent; }");
    11. l->render(painter, option.rect.topLeft());
    12. painter->restore();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyle

    What did you expect if you are only render QLabel! It doesn't have a parent it is not shown so it cant interact with any input. You have just created it and then you are using for painting. In fact you have big memory leak (small but frequent).
    What is you real initial problem? Currently you come up with some crappy solution of some mysterious problem and you are asking us to make it work.

    I suspect that you are doing something with some table view. WHAT?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyle

    You are aware that you producing memory leaks? An how should "the label" be selectble when you don't show it?

    And never create a label if you only want to draw it. Better use QStyle direct.

  4. #4
    Join Date
    May 2009
    Posts
    83

    Default Re: QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyle

    Thanks for all , i know i did wrong with the QLabel but
    im trying to make text that is selectable , it doesn't have to be Label , i before that used to drow simple text but didn't found any way to make it selectable
    like this:
    Qt Code:
    1. painter->save();
    2. painter->setFont(smallerBoldFont);
    3. if (!isSelected && !isActive)
    4. painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
    5. QString authorString = pStreamItem->author();
    6. QSizeF authorStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
    7. textLoc.setX(textLoc.x() + publishedStringSize.width() + PADDING);
    8. QRectF authorTextBox( textLoc , authorStringSize);
    9. painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
    10. painter->restore();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyle

    Why you're using painter in the first place?
    Don't.

    Use QLineEdit and set it to read only but don't paint it anywhere, leave it to Qt!
    Just parent it to widget you want it appear in.
    You can manipulate background palette to make it blend with the parent widget if you want.

Similar Threads

  1. Replies: 3
    Last Post: 19th October 2011, 12:41
  2. Replies: 1
    Last Post: 23rd January 2010, 13:16
  3. Set Text inside group box
    By bismitapadhy in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2009, 08:26
  4. text inside QGraphicsEllipseItem
    By stoned in forum Qt Programming
    Replies: 3
    Last Post: 10th June 2008, 07:44
  5. rotate Qlabel inside a QPopupMenu
    By mickey in forum Newbie
    Replies: 7
    Last Post: 1st May 2006, 17:33

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.