Results 1 to 4 of 4

Thread: is it possible to get current item selected from itemClicked(QmodelIndex temp)

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default is it possible to get current item selected from itemClicked(QmodelIndex temp)

    Hi i have a problem...In my tree widget i wanted to know the selected (actually checked) item index
    for that i used signal
    Qt Code:
    1. connect(ui->treeWidget_FilesSelected,SIGNAL(clicked(QModelIndex)),this,SLOT(GetSelectedSignal(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    Now i wanted along with index i needed current item too, for that one more signal i got
    Qt Code:
    1. connect(ui->treeWidget_FilesSelected,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(GetSelectedSignalItm(QTreeWidgetItem*,int)));
    To copy to clipboard, switch view to plain text mode 

    but i wanted a signal to get index and current item at a same time....how can i do that?
    please help me...i tried in manual but didnt understand how can i get both index and current item at same time...

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: is it possible to get current item selected from itemClicked(QmodelIndex temp)

    To be clear, the current item, which is generally changed as the result of clicking, and the selection are two different things. Whether a checkable item is checked or not has no relationship to either the current item or the selection.

    In your clicked signal handling slot:
    • If you want the value of the item clicked on, or the state of its check box, then just use the index to fetch it using QModelIndex::data(). Use Qt::CheckStateRole, Qt::DisplayRole etc.
    • If you want a pointer to the item clicked on then use QTreeWidget::itemFromIndex(). From this you can inspect the item's checkState().
    • If you want the current list of items selected (not checked) in the widget then you can call QTreeWidget::selectedItems().

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: is it possible to get current item selected from itemClicked(QmodelIndex temp)

    Thank u Chris for your reply....
    sorry to say that i didnt get exactly how should i have to try to get the check state...
    i tried as below....i think as i'm new to Qt i'm doing some silly mistake... Please help me to sort it out...

    The signal i declared...
    Qt Code:
    1. connect(ui->treeWidget_FilesSelected,SIGNAL(clicked(QModelIndex)),this,SLOT(GetSelectedSignal(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 


    In the slot i'm trying to get the index of checked child of the tree....


    Qt Code:
    1. void MainWindow::GetSelectedSignal(QModelIndex temp)
    2. {
    3.  
    4. bool check;
    5. check=temp(Qt::CheckState());
    6. int t=temp.row();
    7.  
    8. if (check)
    9. {
    10.  
    11. LSignalForScript.append(QString::number(t));
    12. LSignalForScript.removeDuplicates();
    13.  
    14. }
    15. else
    16. {
    17. LSignalForScript.removeOne(QString::number(t));
    18.  
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 


    I'm getting compilation error saying that...
    "no match for call to (QModelIndex)(Qt::CheckState)"

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: is it possible to get current item selected from itemClicked(QmodelIndex temp)

    Qt Code:
    1. Qt::CheckState state = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
    2. switch (state) {
    3. case Qt::Checked:
    4. // do stuff
    5. break;
    6. case Qt::Unchecked:
    7. // do other stuff
    8. break;
    9. case Qt::PartiallyChecked:
    10. // you might have these too
    11. break;
    12. }
    To copy to clipboard, switch view to plain text mode 
    This only tells you the check state of the item the user clicked on, not any others in the tree. If you want the check state of everything in the tree then you need to iterate over the entire tree to look at each.

Similar Threads

  1. Replies: 0
    Last Post: 10th March 2011, 11:44
  2. Disable menu item based to current control selected
    By Suppaman in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2010, 07:33
  3. Replies: 1
    Last Post: 20th January 2010, 08:38
  4. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  5. How to know the current tab selected
    By Mrdata in forum Newbie
    Replies: 11
    Last Post: 6th February 2007, 19:46

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.