Results 1 to 4 of 4

Thread: return the editor pointer from createEditor

  1. #1
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default return the editor pointer from createEditor

    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

    Qt Code:
    1. if(ComboBox *cb = qobject_cast<ComboBox *> (object)){
    To copy to clipboard, switch view to plain text mode 

    the pointer are different.


    Thanks for help.

  2. #2
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: return the editor pointer from createEditor

    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?
    Last edited by goblincoding; 21st September 2011 at 18:56.

  3. #3
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: return the editor pointer from createEditor

    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:

    Qt Code:
    1. void myclass::addComboItems(int id, QString text, QString value)
    2. {
    3. QWidget *wg = object;
    4. if(LineEditDelegate *le = qobject_cast<LineEditDelegate *> (wg))
    5. {
    6. wg = le->qw_editor;
    7. }
    8. if(Combobox *cb = qobject_cast<ComboBox *> (wg))
    9. {
    10. cb->addItem(value, text);
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Constructor from LineEditDelegate:
    Qt Code:
    1. LineEditDelegate::LineEditDelegate(QDomElement formElement, QObject *parent)
    2. : QStyledItemDelegate(parent)
    3. {
    4.  
    5. qw_editor = WidgetHelper::createFormWidget(...);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    And the LineEditDelegate::createEditor() example:

    Qt Code:
    1. QWidget* LineEditDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &/* option */,
    3. const QModelIndex &/* index */) const
    4. {
    5.  
    6. QWidget *editor = WidgetHelper::createFormWidget(..., parent);
    7. ...
    8. ...
    9. return editor;
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Smile Re: return the editor pointer from createEditor

    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:

    Qt Code:
    1. class WidgetHelper : public QWidget
    2. public:
    3. QComboBox *getComboBox();
    4.  
    5. public slots:
    6. protected:
    7. //etc etc etc
    8. private:
    9. QComboBox *cb;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void myclass::addComboItems(int id, QString text, QString value)
    2. {
    3. yourWidgetHelper->getComboBox()->addItem(text,value);
    4. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. QStyledItemDelegate and editor pointer
    By Nero in forum Qt Programming
    Replies: 10
    Last Post: 20th September 2010, 18:42
  2. Pointing Two Dimensional Pointer to return value
    By babu198649 in forum General Programming
    Replies: 5
    Last Post: 24th April 2009, 19:16
  3. Qt script return pointer
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 30th December 2008, 21:54
  4. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  5. Replies: 6
    Last Post: 22nd June 2007, 22:20

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.