PDA

View Full Version : QLabel text not selectble even when i set Qt::TextSelectableByMouse inside QStyledIte



umen
17th January 2012, 12:50
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 .



void ItemDelegate::paintBody( QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const {
painter->save();
QLabel *l = new QLabel();
l->setTextFormat(Qt::RichText);
l->setTextInteractionFlags(Qt::TextSelectableByMouse) ;
l->setGeometry(option.rect);
l->setText("This is test");
l->setStyleSheet("QLabel { background-color : transparent; }");
l->render(painter, option.rect.topLeft());
painter->restore();

}

MarekR22
17th January 2012, 13:17
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?

Lykurg
17th January 2012, 13:19
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.

umen
17th January 2012, 14:25
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:

painter->save();
painter->setFont(smallerBoldFont);
if (!isSelected && !isActive)
painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
QString authorString = pStreamItem->author();
QSizeF authorStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
textLoc.setX(textLoc.x() + publishedStringSize.width() + PADDING);
QRectF authorTextBox( textLoc , authorStringSize);
painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
painter->restore();

Spitfire
19th January 2012, 10:17
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.