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. Altering requests from TreeView to Model for data...
Forum Updated to NodeBB v4.3 + New Features

Altering requests from TreeView to Model for data...

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 646 Views
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    The idea:

    Alter treeView requerst to model for data. Ie model(index,role) I would like to alter the Role in my treeview to fine tune what I would like to display in given row based on user filters. Ie say I have a namespace configuration. Ie, each item has a Qt::userRole+namespace QVariant. If a user configure tree to displaynamespace then the feeding function instead of using model.index(index,Qt::DisplayRole) should use model.index(index,Qt::UserRole+namespace).

    So the question is, who/where do I look for that function? Cant figure it out o.O.

    Or perhaps there is a better way?

    I though that treeview edition is a good way because if I have 5 treeviews accessing thesame model(), then they all should be able to pass different qtDisplayRole roles for different sets of data.

    Regards
    Dariusz

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      the answer is delegate:
      subclass QStyledItemDelegate and reimplement paint()

      class MyDelegate : public QStyledItemDelegate{
      Q_OBJECT
      Q_DISABLE_COPY(MyDelegate)
      public:
      explicit MyDelegate(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){}
      void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
      {
          Q_ASSERT(index.isValid());
          QStyleOptionViewItem opt = option;
          initStyleOption(&opt, index);
          opt.text = displayText(index.data(Qt::UserRole+something), opt.locale);
          const QWidget *widget = option.widge);
          QStyle *style = widget ? widget->style() : QApplication::style();
          style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
      }
      };
      

      Now from the view you can call setItemDelegate or setItemDelegateForColumn to set the delegate

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by Dariusz
        #3

        Hey
        Hohohohohooh that's way better than I thought! Amazing thanks!

        I already used item delegate to set the edit widget text with setEditorData and setModelData for my undo stack but did not realize how to use paint properly to paint the widgets.

        Do you know maybe if QStyledItemDelegate can somehow refresh/notify the views that use it to refresh their views? Or how do I fire refresh signal on it?

        VRoninV 1 Reply Last reply
        0
        • D Dariusz

          Hey
          Hohohohohooh that's way better than I thought! Amazing thanks!

          I already used item delegate to set the edit widget text with setEditorData and setModelData for my undo stack but did not realize how to use paint properly to paint the widgets.

          Do you know maybe if QStyledItemDelegate can somehow refresh/notify the views that use it to refresh their views? Or how do I fire refresh signal on it?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @Dariusz said in Altering requests from TreeView to Model for data...:

          Do you know maybe if QStyledItemDelegate can somehow refresh/notify the views that use it to refresh their views? Or how do I fire refresh signal on it?

          The delegate should not notify the view of changes ever. The flow of edits is:
          View asks delegate to start editing -> delegate creates the editor (createEditor() + setEditorData()) -> when editing is finished, the delegate updates the model (setModelData) -> the model signals the change (dataChanged())-> the view detects it needs to update -> the view asks the delegate to repaint

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1

          • Login

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