How to make QLabel text selectble in QStyledItemDelegate type of class
-
I have simple QListView item delegate that inherit form QStyledItemDelegate.
i like to put in this item text that can be selected with the mouse and copyed .
when i try to put simple item QLable with the flags
@Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse;@
and render it like this in the paint method , the text is static and doesn't selectable
@pTextEdit_title->setText(Title);
QRect TextEditRect(option.rect.x()+THUMB_WIDTH+THUMB_WIDTH+PADDING, option.rect.y() ,
pTextEdit_title->width(), pTextEdit_title->height());QPixmap pixmap(pTextEdit_title->size()); pTextEdit_title->render(&pixmap);
painter->drawPixmap(TextEditRect,pixmap);@
how can i make it selectable?
ok after digging the web i set new code under the paint method , that shows me the label and all but still can't make it selectable
@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(); @any idea what is wrong here ?
-
Your label belongs nowhere.
The paint method of a delegate does not work on labels, but on a QPainter directly. If you absolutely need a QLabel, you will have to set it with setItemWidget on the view.
To enable selectable text, you will have to reimplement the mouse event handlers (mouse pressed, mouse moved, etc.) and probably "steal" some of the code used in QLabel for displaying stuff. I vaguely remember that QLabel uses an internal helper class for that - it might not be easy to reuse that for your purpos, though.
-
Thanks For the replay
wow .. is there some other simple solution for selectable text ?
i dont absolutely need QLabel not at all i just wanted to present text that is selectable in the
itemdelegate . what more "easy" options do i have ?
i have more elements in the paint methods that im presenting like images.also im using QListView so i can't use : setItemWidget
-
@
pseudo code
class QModelIndex;
IView {
public:
virtual void setSelected(const QModelIndex& index, bool flag) = 0;
}MyListView : public QListView, public IView {
ItemDelegate *delegate;public: MyListView(QWidget *parent = 0) : QListView(parent) { delegate = new ItemDelegate; delegate->setView(this); setItemDelegate(delegate); } void setSelected(const QModelIndex& index, bool flag) { QListWidgetItem *item = item(index.row()); MyWidget *w = static_cast<MyWidget*>(itemWidget(item)); w->setSelected(flag); }
}
void ItemDelegate::setView(IView *view)
{
this->view = view; //pimpl
}void ItemDelegate::paint (QPainter* p, const QStyleOptionViewIte& o, const QModelIndex& i) const
{
QStyleOptionViewItemV4 option = o;initStyleOption(&option, i); bool selected = ( option.state & QStyle::State_Selected ); view->setSelected(i, selected);
QSyledItemDelegate::paint(p, o, i);
}
@ -
I gather you need to have text selectable for a read-only table, right?
So... what you could try, is to set QLabel as your 'editor' by using a custom delegate or by using a class like [[doc:QItemEditorCreator]] or [[doc:QStandardItemEditorCreator]]. Then, you could use a [[doc:QIdentityProxyModel]] to make the cells in your table writable again. That way, when you click a table cell, the editor will be used. However, in this case the editor will be a QLabel: still not editable, but selectable nontheless.
-
Ok i found great solution , i really what you to say what you think ,
after looking at the code of "QTwitter":http://qt-apps.org/content/show.php/qTwitter?content=99087 , i saw it uses custom widget as the item
i mean widget that made in the Q Designer .