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 send signal when tree item is clicked.
Forum Updated to NodeBB v4.3 + New Features

How to send signal when tree item is clicked.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 513 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.
  • S Offline
    S Offline
    summit
    wrote on 25 Jan 2021, 04:11 last edited by summit
    #1

    I am promoting a QWidget to a custom tree model which is derived from the QAbstractItemModel.

    Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.

    In order to provide the tree item clicked signal i am emitting the item selected signal from the tree item .

    In the tree item class which is derived from QObject , i am creating the signal.

    Signal
    void channelSelected(SumChannel* channel);
    
    bool StageItem::selectChannel() {
    		if (m_dataType != DirectorDataType && m_dataType != ContainerDataType) {
    			auto channel = m_data.value<SumPosition_Channel*>();
    			emit channelSelected(channel);
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    

    In the tree model class i am creating the the clicked signal.

    Signal
    void channelDeleted(SumChannel* channel);
    

    Than i am linking it to the tree item clicked signal.

    connect(m_rootItem, &StageItem::channelSelected, this, &StageModel::channelSelected);
    

    This is how i am emitting the signal from the tree model.

    QVariant StageModel::data(const QModelIndex &index, int role) const
    	{
    		if (!index.isValid())
    			return QVariant();
    
    		auto *item = getItem(index);
    
    		switch (role) {
    		case Qt::UserRole:
    			return item->data();
    		case Qt::DisplayRole:
    			switch (index.column()) {
    			case StageItem::TreeColumnIndex:
    				if (item->dataType() != StageItem::DirectorDataType && item->dataType() != StageItem::ContainerDataType)
    					item->selectChannel();
    				
    				return item->name();
    			}
    	
    		default:
    			return QVariant();
    		}
    	}
    

    This signal does gets fired even when i create a new tree item or the mouse hovers over it.

    How can i truly get a tree item clicked kind of signal ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Jan 2021, 05:53 last edited by
      #2

      @summit said in How to send signal when tree item is clicked.:

      Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.

      Can you please elaborate this? How do you use a model with a plain widget?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply 25 Jan 2021, 06:34
      0
      • C Christian Ehrlicher
        25 Jan 2021, 05:53

        @summit said in How to send signal when tree item is clicked.:

        Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.

        Can you please elaborate this? How do you use a model with a plain widget?

        S Offline
        S Offline
        summit
        wrote on 25 Jan 2021, 06:34 last edited by
        #3

        @Christian-Ehrlicher i am promoting the widget to timelineview.cpp and in the timelineview.cpp i have this function.

        void StageTimelineView::setModel(models::StageModel *stageModel)
        	{
        		if (m_model) {
        			disconnect(m_model, &QAbstractItemModel::rowsInserted, this, &StageTimelineView::onRowsInserted);
        		}
        		m_model = stageModel;
        		m_view->setModel(m_model);
        		m_header->setSectionResizeMode(0, QHeaderView::Interactive);
        		m_header->setSectionResizeMode(1, QHeaderView::Stretch);
        
        		connect(m_model, &QAbstractItemModel::rowsInserted, this, &StageTimelineView::onRowsInserted);
        	}
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Jan 2021, 15:41 last edited by
          #4

          @summit said in How to send signal when tree item is clicked.:

          i am promoting the widget to timelineview.cpp

          Sorry but I don't understand what you're writing here.

          QTreeView (or better it's base class QAbstractItemView) has enough signals to get notified when an item is clicked.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          1/4

          25 Jan 2021, 04:11

          • Login

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