Important: Please read the Qt Code of Conduct -

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
    and render it like this in the paint method , the text is static and doesn't selectable
    QRect TextEditRect(option.rect.x()+THUMB_WIDTH+THUMB_WIDTH+PADDING, option.rect.y() ,
    pTextEdit_title->width(), pTextEdit_title->height());

    QPixmap pixmap(pTextEdit_title->size());


    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

    QLabel *l = new QLabel();
    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

  • Unfortunately, I don't have any solution for this at all, not even for using that functionality outside of a list/tree view.

  • Thanks for the help , still looking for solution ...

  • @
    pseudo code

    class QModelIndex;

    IView {
    virtual void setSelected(const QModelIndex& index, bool flag) = 0;

    MyListView : public QListView, public IView {
    ItemDelegate *delegate;

        MyListView(QWidget *parent = 0) : QListView(parent)
             delegate = new ItemDelegate;
    void setSelected(const QModelIndex& index, bool flag) {
           QListWidgetItem *item = item(index.row());
           MyWidget *w = static_cast<MyWidget*>(itemWidget(item));


    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);

  • How about using a lineEdit or something like this (some kind of text editor widget) and set the image containing the information you additionally want to display as the background of that text editor field?

  • i have no problem , i just need to see how to do it right .. some start point

  • 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.

  • Hi and thanks for the replay ,
    im using ListView , and the selectable text is part of the item delegate
    in this item there will be also images. its not a table.

  • Ok i found great solution , i really what you to say what you think ,
    after looking at the code of "QTwitter": , i saw it uses custom widget as the item
    i mean widget that made in the Q Designer .

  • If you are after a list-based UI like that, I would suggest looking into embedding a QML-based listview in your application.

  • Hi thanks for the replay , yes i do look after list based look.
    I will look into QML .
    can you give me your opinion about the solution used in the QTwitter ?

  • Sorry, I have no time to dig through its code right now, and I can't judge only based on screenshots.
    Just using a widget for every item in the model would not be performant for larger models. No idea if that is the approach taken though.

  • thanks again for your opinion , its not large list its like 30 items in first in last out kind of way .

Log in to reply