Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make QLabel text selectble in QStyledItemDelegate type of class

How to make QLabel text selectble in QStyledItemDelegate type of class

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 11.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stima_ua
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  umen242
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      umen242
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • U Offline
                        U Offline
                        umen242
                        wrote on last edited by
                        #11

                        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 .

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • U Offline
                            U Offline
                            umen242
                            wrote on last edited by
                            #13

                            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 ?

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • U Offline
                                U Offline
                                umen242
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved