Results 1 to 9 of 9

Thread: distinguish which qtreewidget called a slot

  1. #1
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default distinguish which qtreewidget called a slot

    Hello.

    I have a MainWindow with several QTreeWidgets.
    When you click on any of them, the contextmenu shown is the same, it just contain the QAction "add".
    If you click on "add", a new dialog appears asking you for the QTreeWidgetItem to add.

    The question is how do I know which QTreeWidget called add() and so I can add the QTreeWidgetItem to the correct QTreeWidget.

    Calculating the coordinates is too arduous

    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: distinguish which qtreewidget called a slot

    That depends, there are many ways to do that? How did you implement the add action?
    How/when do you show the context menu?

    Calculating coordinates is the thing that you must never do . If you have several tree widgets, it means that you have several objects that you can refer to and whose interface you can use...

    Regards

  3. #3
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: distinguish which qtreewidget called a slot

    Hello marcel, thanks for replying so quickly

    Add action has a private slot assigned.

    from packagestree.cpp:
    Qt Code:
    1. void PackagesTree::contextMenuEvent(QContextMenuEvent *event)
    2. {
    3. QMenu menu(this);
    4. addAct = new QAction("Add", this);
    5. connect(addAct, SIGNAL(triggered()), this, SLOT(add()));
    6. menu.addAction(addAct);
    7. menu.exec(event->globalPos());
    8. qDebug("x:%d y:%d", event->globalX(), event->globalY());
    9. }
    10.  
    11. void PackagesTree::add()
    12. {
    13. addPackageDlg win;
    14. win.exec();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    I am learning C++ at the same time I'm learning Qt, maybe here is the problem :/

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: distinguish which qtreewidget called a slot

    Well, this is not so hard...

    As far as I understand, the dialog will add rows to your tree widgets. If so, you must make then dialog aware of what tree widget started it. You can pass the "this" pointer as an argument to the dialog constructor, or you can assign it after the dialog has been created:

    Qt Code:
    1. addPackageDlg win( this );
    2. win.exec();
    To copy to clipboard, switch view to plain text mode 

    Now the dialog knows what tree widget called it and it can add a row to that tree widget.

    Regards

  5. The following user says thank you to marcel for this useful post:

    MurDoK (30th April 2007)

  6. #5
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: distinguish which qtreewidget called a slot

    Thanks, I'll try it later.
    There's still a mess in my mind with classes and objects

    I'm sure it works.

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: distinguish which qtreewidget called a slot

    If you post a minimal implementation of the dialog class I'm sure I can show you how to do it...

    Regards

  8. #7
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: distinguish which qtreewidget called a slot

    I've tried to do it of many ways but I always get a segmentation fault.
    I've started from scratch to make a proof of concept but I can't get it working yet.

    The project is attached.

    Is it bad to inherit two or more Ui's ? :
    Qt Code:
    1. class itemdialog : public QDialog, public Ui::Dialog, public Ui::MainWindow
    2. [...]
    To copy to clipboard, switch view to plain text mode 
    Many thanks
    Attached Files Attached Files

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: distinguish which qtreewidget called a slot

    Header
    Qt Code:
    1. #ifndef ITEMDIALOG_H
    2. #define ITEMDIALOG_H
    3. //
    4. #include "ui_dialog.h"
    5. #include "ui_mainwindow.h"
    6. #include "mainwindowimpl.h"
    7. //
    8.  
    9.  
    10. class itemdialog : public QDialog, public Ui::Dialog
    11. {
    12. Q_OBJECT
    13. public:
    14. itemdialog(QTreeWidget *m);
    15.  
    16. private slots:
    17. void additem();
    18.  
    19. private:
    20. QTreeWidget* mCallerTreeWidget;
    21.  
    22. };
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    Implementation:
    Qt Code:
    1. #include "itemdialog.h"
    2. #include <QTreeWidget>
    3. //
    4. itemdialog::itemdialog(QTreeWidget *m )
    5. : QDialog(m)
    6. {
    7. Ui_Dialog::setupUi(this);
    8. mCallerTreeWidget = m;
    9. connect(addButton, SIGNAL(clicked()), this, SLOT(additem()));
    10. connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    11. }
    12.  
    13. void itemdialog::additem()
    14. {
    15. // item->setText(0, nameEdit->text());
    16. // item->setText(1, yearEdit->text());
    17. item->setText(0, "abc");
    18. item->setText(1, "1888");
    19.  
    20. mCallerTreeWidget->addTopLevelItem(item);
    21. }
    22. //
    To copy to clipboard, switch view to plain text mode 

    Yes, it is bad to inherit from more than one UI. You had a QDialog andf a QMainWindow. They have to different event handlers, etc... So it is not ok .Also, you were always adding items to the same tree widget.

    I modified a little bit your code and attached it.

    You always must pass the tree widget which handled the context menu event.
    Hey, didn't you said you have three tree widgets? I only saw one in the ui's...

    Regards

  10. The following user says thank you to marcel for this useful post:

    MurDoK (1st May 2007)

  11. #9
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: distinguish which qtreewidget called a slot

    Hey, I got it working!
    Hey, didn't you said you have three tree widgets? I only saw one in the ui's...
    Yes, but I only wanted to get a working example of it. Now I have implemented it in my project

    So.. thanks marcel, you rule! This has been a nice welcome to the qtcentre forums for me. See you.

Similar Threads

  1. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32

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.