PDA

View Full Version : distinguish which qtreewidget called a slot



MurDoK
30th April 2007, 18:56
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 :o

Thanks in advance.

marcel
30th April 2007, 19:15
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

MurDoK
30th April 2007, 19:38
Hello marcel, thanks for replying so quickly :)

Add action has a private slot assigned.

from packagestree.cpp:


void PackagesTree::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
addAct = new QAction("Add", this);
connect(addAct, SIGNAL(triggered()), this, SLOT(add()));
menu.addAction(addAct);
menu.exec(event->globalPos());
qDebug("x:%d y:%d", event->globalX(), event->globalY());
}

void PackagesTree::add()
{
addPackageDlg win;
win.exec();

}

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

marcel
30th April 2007, 19:45
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:



addPackageDlg win( this );
win.exec();


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

Regards

MurDoK
30th April 2007, 20:14
Thanks, I'll try it later.
There's still a mess in my mind with classes and objects :o

I'm sure it works.

marcel
30th April 2007, 20:36
If you post a minimal implementation of the dialog class I'm sure I can show you how to do it...

Regards

MurDoK
1st May 2007, 15:39
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 ? :

class itemdialog : public QDialog, public Ui::Dialog, public Ui::MainWindow
[...]
Many thanks

marcel
1st May 2007, 16:51
Header


#ifndef ITEMDIALOG_H
#define ITEMDIALOG_H
//
#include "ui_dialog.h"
#include "ui_mainwindow.h"
#include "mainwindowimpl.h"
//

class QTreeWidget;

class itemdialog : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
itemdialog(QTreeWidget *m);

private slots:
void additem();

private:
QTreeWidget* mCallerTreeWidget;

};
#endif


Implementation:


#include "itemdialog.h"
#include <QTreeWidget>
//
itemdialog::itemdialog(QTreeWidget *m )
: QDialog(m)
{
Ui_Dialog::setupUi(this);
mCallerTreeWidget = m;
connect(addButton, SIGNAL(clicked()), this, SLOT(additem()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
}

void itemdialog::additem()
{
QTreeWidgetItem *item = new QTreeWidgetItem(QTreeWidgetItem::Type);
// item->setText(0, nameEdit->text());
// item->setText(1, yearEdit->text());
item->setText(0, "abc");
item->setText(1, "1888");

mCallerTreeWidget->addTopLevelItem(item);
}
//


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

MurDoK
1st May 2007, 22:35
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.