[Qt-interest] Retrieve a particular item from the QTreeView

Constantin Makshin cmakshin at gmail.com
Fri Mar 25 18:47:36 CET 2011


Here is a part of my application I'm currently working on:

void TechniqueView::selectOperation (const QItemSelection& item)
{
    m_selectedOperation = item.isEmpty() ? 0 : m_model->operationIdAtRow(item.first().top());
    emit selectionChanged();
}

TechniqueView::TechniqueView (...)
{
    ...
    m_table = new QTableView(this);
    m_model = new TechniqueModel(technique, m_table, false);
    m_table->setModel(m_model);
    m_table->setSelectionMode(QTableView::SingleSelection);
    ...
    connect(m_table->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(selectOperation(QItemSelection)));
    ...
}

Since I need to track only selected item(s), the second signal parameter is ignored.
QItemSelection contains selection *ranges*, so even if the user selects all items in a view, this selection may be represented as one QItemSelection object. In my case only one item (table cell) can be selected at once, so the slot takes only the first item in the list.

I admit this is somewhat simplified example of selectionChanged() signal usage, but unfortunately there's nothing [significantly] more complex I can demonstrate.

On Friday 25 March 2011 19:52:14 Sajjad wrote:
> Hello Constantin,
> 
> Thanks for the hint.
> 
> say for example i have to connect the click() signal to a slot. In my
> previous post i specified how i did it.
> 
> connect(object1, SIGNAL(clicked()),object2, use_declared_slot());
> 
> As you have suggested, could you please any example hint either in the
> QtDemos or in any other
> free downloadable apps that have used selectionChanged(const
> QItemSelection&,const QItemSelection&).
> 
> 
> Thanks
> Sajjad
> 
> On Fri, Mar 25, 2011 at 5:02 PM, Constantin Makshin <cmakshin at gmail.com>wrote:
> 
> > I don't know how exactly the pressed() signal of QAbstractItemView works, I
> > used only clicked() signal of item views and signals emitted by selection
> > models (particularly selectionChanged(QItemSelection, QItemSelection)). I
> > think you should try them. And make sure you don't get any
> > connection-related warning/error messages when the program is running.
> >
> > On Friday 25 March 2011 11:18:35 you wrote:
> > > Hello Constantin,
> > >
> > > I gave the pressed signal a try and connected the signal to  a slot.
> > > Initially the slot definition contains  a cout statement
> > >
> > >
> > > to check if the pressed signal is actually captured. It is connected as
> > > follows:
> > >
> > > ///////////////////////////////////////////////////////////////
> > >
> > >
> > connect(m_h3dNodeTreeView,SIGNAL(pressed(QModelIndex)),this,SLOT(getH3DTreeItemData(QModelIndex)));
> > >
> > >
> > > ///////////////////////////////////////////////////////////////
> > >
> > >
> > > But withe the pressed signal i get not output. Any other hint?
> > >
> > >
> > > Regards
> > > Sajjad
> > >
> > > On Thu, Mar 24, 2011 at 12:24 AM, Constantin Makshin <cmakshin at gmail.com
> > >wrote:
> > >
> > > > I can't say for sure what's the difference, but my guess is that
> > pressed()
> > > > is emitted when a mouse button is pressed and clicked() — when it's
> > > > released.
> > > >
> > > > And you don't have to override mousePressEvent() as signals emitted by
> > item
> > > > views and selection models are more convenient, particularly because
> > they
> > > > provide index of the selected item.
> > > >
> > > > On Thursday 24 March 2011 01:08:39 Sajjad wrote:
> > > > > Hello Constantin,
> > > > >
> > > > > Is there any difference between the signals pressed() and clicked().
> > > > >
> > > > > I have subclass the QTreeView as well. I was wondering if i should
> > > > over-ride
> > > > > the mousePressEvent() of QTreeView instead.
> > > > >
> > > > > Regards
> > > > > Sajjad
> > > > >
> > > > > On Wed, Mar 23, 2011 at 10:44 PM, Constantin Makshin <
> > cmakshin at gmail.com
> > > > >wrote:
> > > > >
> > > > > > Take a look at QAbstractItemView::clicked(),
> > > > QAbstractItemView::pressed()
> > > > > > signals and selection model (returned by
> > > > > > QAbstractItemView::selectionModel()) functions.
> > > > > >
> > > > > > On Thursday 24 March 2011 00:26:14 Sajjad wrote:
> > > > > > > Hello forum,
> > > > > > >
> > > > > > > I have created a customized model by sub-classing the
> > > > QAbstractItemModel
> > > > > > > class and view the model in the QTreeView.
> > > > > > >
> > > > > > > Now when the user the click on a particular item in the tree view
> > i
> > > > want
> > > > > > > retrieve the tree item from it.
> > > > > > >
> > > > > > > I have encapculated the QTreeView inside the customized widget.
> > And
> > > > then
> > > > > > i
> > > > > > > have over-ridden the mousePressEvent()
> > > > > > >
> > > > > > > {
> > > > > > >      //I check for the left mouse press event and if not i return
> > > > > > >
> > > > > > >     //i need to look for the current clicked item - any way
> > inside
> > > > the
> > > > > > > QTreeView to get it?
> > > > > > > }
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > Any hint to get it?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110325/ddb30427/attachment.bin 


More information about the Qt-interest-old mailing list