Page 1 of 3 123 LastLast
Results 1 to 20 of 42

Thread: QTreeWidget clicked signal

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTreeWidget clicked signal

    Hi,

    I'm having problems to get a signal when I click on a QTreeWidget. The problem is that if no item is selected there is no signal emmited.

    I need to know if I clicked on an empy region of the Tree because I have actions that I have to enable or diable depending on the item that is clicked. But if I click out of any item I would all the actions disabled.

    Thanks,
    Òscar Llarch i Galán

  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: QTreeWidget clicked signal

    Try this:
    Qt Code:
    1. connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(yourSlotGoesHere()));
    To copy to clipboard, switch view to plain text mode 
    Just make sure clicking outside any item clears the selection.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    Hi,

    I have tryied it. It connects but the signal is only emmited when I click on a item.
    Òscar Llarch i Galán

  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: QTreeWidget clicked signal

    Do it lowlevel then. Reimplement the mouseReleaseEvent of the widget and use itemAt() to check the item under cursor.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    Hi,

    So, have I to inherit from QTreeWidget and reimplement the "mouseReleaseEvent" function?

    Thanks,
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    Hi,

    Anyone knows how to make it? Have I to inherit from QTreeWidget and reimplement "mouseReleaseEvent" function or there is another way?

    Thanks,
    Òscar Llarch i Galán

  7. #7
    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: QTreeWidget clicked signal

    Hmm.... Could you read my last post again?

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    Hi,

    Ok, Ok, so ther isn't another way.

    Thanks,
    Òscar Llarch i Galán

  9. #9
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    i too have a getting same problem related to treewidget item signals ,

    Qt Code:
    1. QTreeWidgetItem *plc = new QTreeWidgetItem(ui->treeWidget);
    2. plc->setText(0,QString("PLC %1").arg(i));
    3.  
    4. if(plc)
    5. {
    6. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(plcconfig()));
    7. }
    8.  
    9. QTreeWidgetItem *plcItem = new QTreeWidgetItem(plc);
    10. plcItem->setText(0, tr("CONFIGURATION"));
    11. plc->addChild(plcItem);
    12.  
    13. QTreeWidgetItem *cpuconfig = new QTreeWidgetItem(plcItem);
    14. cpuconfig->setText(0, tr("CPU CONFIGURATION"));
    15. plcItem->addChild(cpuconfig);
    16.  
    17. // if(cpuconfig)
    18. // {
    19. // connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCPU()));
    20. // }
    21.  
    22. QTreeWidgetItem *varconfig = new QTreeWidgetItem(plcItem);
    23. varconfig->setText(0, tr("VARIABLE CONFIGURATION"));
    24. plcItem->addChild(varconfig);
    25.  
    26. QTreeWidgetItem *progconfig = new QTreeWidgetItem(varconfig);
    27. progconfig->setText(0, tr("PROGRAM VARIABLE CONFIGURATION"));
    28. varconfig->addChild(progconfig);
    29.  
    30. if(varconfig)
    31. {
    32. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showpvc()));
    33. }
    To copy to clipboard, switch view to plain text mode 

    As iam using above signal when iam cliicking on PROGRAM VARIABLE CONF..... the required window has to opened as below.

    Qt Code:
    1. void ECLogic::showpvc( )
    2. {
    3. QTabBar *tab_4=new QTabBar(ui->widgetConf);
    4. PCD= new programvariableconfiguration(tab_4);
    5. PCD->setWindowTitle(QString("PVC"));
    6. tabwidget3->insertTab(0,tab_4,QString("PVC"));
    7. tab_4->setCurrentIndex(tabwidget3->indexOf(ui->widgetConf));
    8. tab_4->setTabsClosable(true);
    9. tab_4->setEnabled(true);
    10. PCD->show();
    11. }
    To copy to clipboard, switch view to plain text mode 

    but my problem is getting the connections to all the treewidggetitems not for the particular....is there any way to get my condtion ....so please suggests me the way to write the code........
    Last edited by wysota; 22nd January 2010 at 11:04. Reason: Missing [code] tags

  10. #10
    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: QTreeWidget clicked signal

    You get the pointer to the item in the signal so you can check its contents in the slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    how can i set the pointer to the signal, we can't set values in connect statements.we should use types there.

    Try this:

    connect(ui.tree, SIGNAL(itemClicked (QTreeWidgetItem*,int)), this, SLOT(showplc()))

  12. #12
    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: QTreeWidget clicked signal

    Quote Originally Posted by mkkguru View Post
    how can i set the pointer to the signal, we can't set values in connect statements.we should use types there.
    I didn't say anything about "pointers to signals" nor did I say about setting a value in a connect statement.

    Qt Code:
    1. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowpvc(QTreeWidgetItem*)));
    2. // ...
    3. void X::maybeShowpvc(QTreeWidgetItem *item){
    4. if(item) {
    5. // ...
    6. } else {
    7. // ...
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    actually i have to write many signals to QTreewidgetitems so when i clikcing on item other signals also working ,so i need to stop getting other windows...only particular window when we click on Qtreewidgetitem has to opened.......

  14. #14
    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: QTreeWidget clicked signal

    I don't understand what you mean.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    i have to get connections to particular item when we clik.......and no other connections has to established to treewidgetitems

  16. #16
    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: QTreeWidget clicked signal

    So? What's the problem? Can't you check if the item that you want was clicked and if not then simply do nothing?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    // connections written in constructor

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowpvc(QTreeWidgetItem*))) ;

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowplc(QTreeWidgetItem*))) ;

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowioc(QTreeWidgetItem*))) ;

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem ,int)),this,SLOT(maybeShowldr(QTreeWidgetItem*)));


    // slot methods

    void X::maybeShowpvc(QTreeWidgetItem *plc)
    {
    if(item){
    plc->show();
    ......
    // code
    }

    void X::maybeShowplc(QTreeWidgetItem *pvc)
    {
    //connection code
    pvc->show();
    .............

    }

    void X::maybeShowpvc(QTreeWidgetItem *ioc)
    {
    //connection code
    .....
    }

    void X::maybeShowldr(QTreeWidgetItem *ldr)
    {
    //connection code
    .....

    }

    when iam using above code all the connections and slots are working at a time...........is there any possibility to get the connections to particular item (plc,pvc,ioc,ldr) ...

  18. #18
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    You dont need separate slots, you need only one slot.
    Qt Code:
    1. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem ,int)),this,SLOT(showItem(QTreeWidgetItem*)));
    2. MyWindow::showItem(QTreeWidgetItem* item)
    3. {
    4. if(item == plcItem)
    5. plc->show();
    6. else if(item == pvcItem)
    7. pvc->show();
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    where plcItem and pvcItem are the QTreeWidgetItems you have added in your tree.

  19. #19
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    I think that mkkguru misunderstood or misused word "connection". Can you explain in other words what do you mean by "getting the connection to particular item"?

    Just to make things clear:
    connect() method only sets the signal and slot in such way, that if signal is emitted then the connected slot is called (with emitted arguments). So if you connect your QTreeWidget itemClicked(QTreeWidgetItem *, int) signal to 4 slots, then clicking on item will emit one signal ONCE but this will call every slot connected to your signal, so all your 4 slots will be called. But you did not notice that there is QTreeWidgetItem* argumet in that signal, which is there to let you know what item was clicked. So in your case there should be one slot connected to this itemClicked() signal, where depending on information retrieved from given QTreeWidgetItem*, you will know that this clicked item is pvc, plc, ioc, etc...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  20. #20
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: QTreeWidget clicked signal

    Image shows the treewidget ,when we click on item the particular item window has to opend.

    //connection
    Qt Code:
    1. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem* ,int)),this,SLOT(showItem(QTreeWidgetItem*)));
    To copy to clipboard, switch view to plain text mode 

    //slots method
    Qt Code:
    1. void xxx::showItem(QTreeWidgetItem* item)
    2. {
    3. if(item == lnx)
    4. {
    5. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    6. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    7. ECLogic_Lnx->show();
    8. }
    9. else
    10. if(item == plc)
    11. tab_3=new QTabBar(main_tab_widget);
    12. PLCconfig=new plcconfiguration(tab_3);
    13. PLCconfig->setWindowTitle(QString("PLC"));
    14. main_tab_widget->insertTab(0,tab_3,QString("PLC"));
    15. tab_3->setCurrentIndex(main_tab_widget->indexOf(tab_3));
    16. PLCconfig->show();
    17. }
    To copy to clipboard, switch view to plain text mode 


    When iam using above code the connecting to other windows are not getting ,where ECLogic_Lnx , PLCconfig,.. are window names.
    where lnx,plc,...are qtreewidgetitem obj names,so is there any method to check the condition

    [
    Attached Images Attached Images
    Last edited by wysota; 25th January 2010 at 11:27. Reason: Missing [code] tags

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.