Results 1 to 5 of 5

Thread: Programattically Clicking an Item in QTreeView

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Programattically Clicking an Item in QTreeView

    I have the following class defined (with alot of the extra stuff removed for clarity), and I have a question about its behavior:

    Qt Code:
    1. class DatabaseTreeWidget : public QTreeView
    2. {
    3. Q_OBJECT;
    4. public:
    5. DatabaseTreeWidget(QString Filename, QWidget *parent = 0);
    6. ~DatabaseTreeWidget();
    7. void Update_Tree_From_Flags();
    8.  
    9. signals:
    10. void Message_Changed(QString *Message);
    11. void DB_File_Path_Changed(QString *File_Path, const QModelIndex *index);
    12. void DB_Key_Changed(QString *Key);
    13. void DB_Flags_Changed(int Flags);
    14.  
    15. private slots:
    16. void Database_Item_Clicked(const QModelIndex &index);
    17. bool Database_Is_Empty(QString File_Name);
    18. void Click_Current_Database_Selection();
    19. };
    To copy to clipboard, switch view to plain text mode 

    I can use this class with no problems except for one. I need to get the currently selected item and click it from the program (triggering another event). I tried some testing code as follows:

    Qt Code:
    1. void DatabaseTreeWidget::Click_Current_Database_Selection()
    2. {
    3. this->selectAll();
    4. std::cout<<"Selected list size: "<<this->selectedIndexes().size()<<"\n";
    5. //ScanDatabaseItem *Item = (ScanDatabaseItem *) selectedIndexes().at(0).internalPointer();
    6. }
    To copy to clipboard, switch view to plain text mode 

    When I test the code, I would expect that I would get a count of all items in the tree. I get zero. I initally used currentIndex() but could not obtain the currently selected item in the widget.

    How can I achieve this? Thanks.

    JS

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Programattically Clicking an Item in QTreeView

    Qt Code:
    1. emit clicked(currentIndex());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Programattically Clicking an Item in QTreeView

    This is a problem: segmentation fault.... makes me think there is no item selected although the widget shows otherwise.

    I tried something like this, but it appears like there is nothing to "click". The currently selected item in the widget shows that something is selected, but a count of selected items returns zero items selected.

    The QTreeView clearly shows an item selected, but I cannot even get that "currentItem" and read values from the underlying model.

    What am I doing wrong here?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Programattically Clicking an Item in QTreeView

    Quote Originally Posted by johnny_sparx
    This is a problem: segmentation fault....
    You probably have something wrong with a slot which is connected to the signal. Emit can't cause a segfault here.

    makes me think there is no item selected although the widget shows otherwise.
    There is a difference between "selected" and "current".

    I tried something like this, but it appears like there is nothing to "click". The currently selected item in the widget shows that something is selected, but a count of selected items returns zero items selected.
    Try using the selection model (QAbstractItemView::selectionModel()) and use QItemSelectionModel::selectedIndexes() to get a list of selected indexes. You'll probably get the same result as using selectedIndexes() from QAbstractItemView, but it'll just make you certain there are no items selected

    The QTreeView clearly shows an item selected, but I cannot even get that "currentItem" and read values from the underlying model.
    Does it? A selected item is marked with a different coloured background (at least in most styles) and current item is marked with a dotted border (at least in most styles). Are you sure you have a selection? Try selecting more than one item to be sure of that.

    What am I doing wrong here?
    Where exactly?

  5. #5
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Programattically Clicking an Item in QTreeView

    I am quite sure that I have an item selected... background is dark, there is no dotted line around the item that I can see.

    I am not quite sure how to use the selection model. All I want is to have the model index of the item that is highlighted by a background change in the actual widget.

Similar Threads

  1. QTreeView and item editing
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 18:56
  2. QTreeView, QSortFilterProxyModel and item expansions
    By benacler in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2008, 20:30
  3. Replies: 5
    Last Post: 6th December 2007, 14:43
  4. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.