PDA

View Full Version : return the editor pointer from createEditor



Naahmi
21st September 2011, 12:05
Hello,


i have a problem with my LineEditDelegate.


i create dynamicly the formfields like combobox, buttonedit... in LineEditDelegate::createEditor();

Now i need the pointer from LineEditDelegate::createEditor() in another function, before the user clicked a field to fill my Combobox.
There are any ways to return the pointer from the "combobox" in this function? To add items to the combobox. If i use


if(ComboBox *cb = qobject_cast<ComboBox *> (object)){

the pointer are different.


Thanks for help.

goblincoding
21st September 2011, 18:50
Hi Naahmi,

I must admit that I'm not entirely sure what you're trying to do as I don't really understand your question. However, the only ways to access an existing object (via pointer in your case) from a different function is to (1) have a getter to return a pointer to that object, (2) have a member pointer in your class (that will obviously be accessible across all functions) pointing to the object, or (3) to pass a pointer to the object to a function as parameter.

Regarding the snippet you provided, note that you're using the assignment operator "=" and not the relational "=="

Perhaps you can provide more code if my comment doesn't help?

Naahmi
22nd September 2011, 08:46
In my example i have a tableview widget.

the server send a xml protocoll tothe client. in these protocoll are the items from the combobox.

Now i have 2 functions

addComboItems();

and createEditor();

addCcmboItems() Example:



void myclass::addComboItems(int id, QString text, QString value)
{
QWidget *wg = object;
if(LineEditDelegate *le = qobject_cast<LineEditDelegate *> (wg))
{
wg = le->qw_editor;
}
if(Combobox *cb = qobject_cast<ComboBox *> (wg))
{
cb->addItem(value, text);
}

}

Constructor from LineEditDelegate:



LineEditDelegate::LineEditDelegate(QDomElement formElement, QObject *parent)
: QStyledItemDelegate(parent)
{

qw_editor = WidgetHelper::createFormWidget(...);

}


createformWIdget creates the Combobox QCOmbobox *cb = new QCombobox(this); ...

And the LineEditDelegate::createEditor() example:



QWidget* LineEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{

QWidget *editor = WidgetHelper::createFormWidget(..., parent);
...
...
return editor;
}



And my Problem is, editor from createEditor has a different pointer as cb in addComboItems the items are not visible in the widget.

The Problem is addComboItem is calling before createEditor.

There are any ways to get the pointer from createEditor?

I Hope this question is better to understand my problem.

goblincoding
25th September 2011, 08:32
Hi Naahmi

I really don't understand why you are doing what you are doing and I especially don't understand all the qobject_cast calls.

Perhaps your problem is the following. In your addComboItems() function, you assign a QWidget pointer to some existing object. You then cast this object to a LineEditDelegate and on a successful cast, you assign the QWidget pointer to the LineEditDelegate's qw_editor (created in the LineEditDelegate's constructor). You then cast that ENTIRE qw_editor to a QComboBox, but the qw_editor already contains a QComboBox which was added in the createFormWidget function and now you're trying to add items to a QComboBox that was cast from some specialised widget instead of adding items to the QComboBox created for your widget in the createFormWidget function.

Do you see how confusing this is to keep track of?

I would suggest you find another way to solve your problem and stay away from object casting unless you are absolutely sure what you are doing (this is simply my opinion, I don't mean to offend...I personally stay away from casting as far as possible).

I'm not sure what your WidgetHelper class does exactly, but perhaps you can rather expand on it or re-design it in such a way that you have a specialised widget looking exactly the way you want it to look (already containing your combo box) and simply access the various members as usual

For example:


class WidgetHelper : public QWidget
public:
QComboBox *getComboBox();

public slots:
protected:
//etc etc etc
private:
QComboBox *cb;


void myclass::addComboItems(int id, QString text, QString value)
{
yourWidgetHelper->getComboBox()->addItem(text,value);
}

I'm sorry I can't be of more help to you than this. Good luck!