Page 1 of 2 12 LastLast
Results 1 to 20 of 34

Thread: indexWidget() unexpectedly returns a NULL pointer.

  1. #1
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default indexWidget() unexpectedly returns a NULL pointer.

    Hi All,

    I am attempting to create a model/view application in Qt 4.7.1. I am a very new Qt developer.

    Summary of what I am attempting to do:

    I have a treeview that is organized as a rectangular table of rows and columns. One column of items contains a button. By default this button is to be transparent and disabled. A given button is to become visible and enabled when the mouse is hovering over its row.

    The approach I am pursuing is to

    1. find the model index for the cell that the mouse is hovering over, and
    2. obtain a pointer to the widget associated with the widget, and
    3. using this pointer manipulate the visibility of the button within said widget.

    I cannot get a valid pointer to the widget.

    my current code looks like this:

    void HistoryTreeView::mouseMoveEvent(QMouseEvent *event)
    {
    QAbstractItemModel *m(model());

    // Only do something when a model is set.
    if (m)
    {
    QModelIndex index = indexAt(event->pos());
    if (index.isValid())
    {
    // if the mouse has moved to another row
    if (index.row() != m_currentRow)
    {
    m_currentRow = index.row();

    QMessageBox::information( this, "HistoryTreeView", QString("index(%1)").arg(index.row()));

    QWidget * item = indexWidget(index);
    Q_ASSERT(item != NULL );
    }
    }
    else // model is invalid
    {
    m_currentRow = -1;
    }
    }
    }

    The symptoms:

    I expected the call to indexWidget() to return a valid pointer to the widget the mouse is over. Instead it unexpectedly returns a NULL pointer.

    Commentary:

    The variable named 'index' is acting as I expected because the QMessageBox shows the correct row value. Consequently I do not think there is anything wrong with the value I am providing to indexWidget().

    This is just debug code. It is missing things like code that selects the column that holds the buttons.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    From your description it is not entirely clear how many columns the view has. If there is more than one column and you have a widget at (0,1) while hovering (0,0) you will get a null pointer. You should explicitly check for the sibling() at the column of the button. If that doesn't do it for you, could you provide a fully compilable example, so that we know more of the code context?

    Please use code tags when putting code into your post.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    Hi Franz,

    Thank you so much for your quick response.

    Sorry about not using the code tag, I didn't notice them until I had already posted. This is my first time using this forum. I will be sure to use code tags in the future.

    Regarding your question about which column will hold the button: the buttons are located in column zero.

    At this point in the debug I was just trying to get a pointer to any widget in the treeview. My current objective is to simply turn the cell my mouse is hovering over some contrasting color. I reason that achieving this would be a reasonable midway point toward my ultimate goal; I would have to obtain the correct QWidget pointer, etc., before I could, say, change the item's background color.

    Once I master this step my next step would be to obtain the QWidget pointer for the cell containing the button, where said cell is on the same row that the mouse is hovering over.

    OK, you recommended that I "explicitly check for the sibling() at the column of the button". I was previously unaware of sibling() so I just looked into it. After reading the documentation I think that sibling() would do the job. But my problem is that I need to obtain a pointer to a QWidget for a given index. sibling() returns an index but when I attempt to use a valid index, say in a call to indexWidget() I get a NULL pointer instead of a pointer to a valid widget.


    Added after 14 minutes:


    Hi Franz, I just modified my code to incorporate your suggestion that I use sibling(). I encountered the same problem as before, indexWidget() returned a NULL pointer when I fed it the index returned by sibling(). The following is what I did:

    Qt Code:
    1. void HistoryTreeView::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QAbstractItemModel *m(model());
    4.  
    5. // Only do something when a model is set.
    6. if (m)
    7. {
    8. QModelIndex index = this->indexAt(event->pos());
    9. if (index.isValid())
    10. {
    11. // if the mouse has moved to another row
    12. if (index.row() != m_currentRow)
    13. {
    14. m_currentRow = index.row();
    15.  
    16. QMessageBox::information( this, "HistoryTreeView", QString("index(%1)").arg(index.row()));
    17. //this->setCurrentIndex(index);
    18. QModelIndex itemIndex = m->sibling(m_currentRow, 0, index );
    19.  
    20. QWidget * item = indexWidget(itemIndex);
    21. Q_ASSERT(item != NULL );
    22. }
    23. }
    24. else // model is invalid
    25. {
    26. m_currentRow = -1;
    27. }
    28. }
    29.  
    30. QTreeView::mouseMoveEvent(event);
    31. }
    To copy to clipboard, switch view to plain text mode 

    You meant the sibling() member of QAbstractItemModel, right?
    Last edited by ajax; 29th November 2010 at 20:19.

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    And (just to be sure) you did use setIndexWidget() to set the widgets?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    Uh....no. I'm looking up setIndexWidget() right now. It looks like I need to understand what that does.

  6. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    It sets the widget you were expecting to get... It is paired up with indexWidget() by name (and functionality).
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  7. #7
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    OK, I have googled around and I still do not understand the usage of setIndexWidget().

    Some more background on what I am trying to do: the items in column zero of the treeview are delegates. The delegate, named ButtonDelegate, inherits from QStyledItemDelegate and is comprised of a QPushButton and a QLabel.

    In the window's constructor I assign this delegate to column zero:

    Qt Code:
    1. ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
    To copy to clipboard, switch view to plain text mode 

    The documentation gives as an example for using setIndexWidget():

    Qt Code:
    1. setIndexWidget(index, new QLineEdit);
    To copy to clipboard, switch view to plain text mode 

    To my mind this doesn't make sense to me. I understand that setIndexWidget() is associating the index with the newly created QLineEditor, but that is not what I think I want. I think I want a pointer to the widget that is located in column zero so I can do things like make the button located there visible or invisible. Obtaining a pointer to a freshly created object doesn't seem to have anything to do with accessing a widget that is part of the tree view.

  8. #8
    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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    the items in column zero of the treeview are delegates.
    No, they are not. The delegate is a helper class, it doesn't "sit" inside a cell.

    The delegate, named ButtonDelegate, inherits from QStyledItemDelegate and is comprised of a QPushButton and a QLabel.
    That's the editor, not the delegate.

    To my mind this doesn't make sense to me.
    And you are correct as using this method usually doesn't make much sense...

    I understand that setIndexWidget() is associating the index with the newly created QLineEditor,
    Let's be strict - the index of the view but not the index of the model. The widget has no relation to the model at all and no relation to the delegate handling that index of the view.

    I think I want a pointer to the widget that is located in column zero
    There is no widget in column 0.

    so I can do things like make the button located there visible or invisible. Obtaining a pointer to a freshly created object doesn't seem to have anything to do with accessing a widget that is part of the tree view.
    Use QAbstractItemView::openPersistentEditor() on your items in column 0 of the view. Note that if you open too many of those, your program will become dead slow (that's the case for setIndexWidget as well, by the way).
    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.


  9. The following user says thank you to wysota for this useful post:

    ajax (30th November 2010)

  10. #9
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.


    Added after 6 minutes:


    I think I want a pointer to the widget that is located in column zero
    There is no widget in column 0.
    I suspect you are thinking I meant something like column zero of the model. When I said
    think I want a pointer to the widget that is located in column zero
    I was referring to column zero of the treeview.

    Correct me if I am wrong, but since views are comprised of widgets there must be a widget in column zero.
    Last edited by ajax; 29th November 2010 at 23:43. Reason: updated contents

  11. #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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    I suspect you are thinking I meant something like column zero of the model. When I said

    I was referring to column zero of the treeview.
    No, I mean the delegate doesn't put any widgets in your column 0 (of the view, model or the kitchen sink).

    Correct me if I am wrong, but since views are comprised of widgets there must be a widget in column zero.
    You are wrong, the views are not made of widgets.
    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.


  12. The following user says thank you to wysota for this useful post:

    ajax (30th November 2010)

  13. #11
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    Thank you for your commentary, wysota. I am endeavoring to get up to speed.

    I am getting the message that I am approaching my task the wrong way. Let me describe what I am trying to do and you tell me if I am doing something inappropriate or losing.

    The spec calls for the application to present a rectangular table of cells to the user. Conceptually the cells are organized as rows of data. In column zero I am to have a push-button that is to be invisible unless the mouse is hovering on the same row as the button's cell.

    wysota, I am understanding you to recommended that I use openPersistentEditor() so as to control the pushbutton's visiblity. I am quite surprised that you recommended an editor for this task. I had intended to either (1) hide the button by making it transparent and disabling it or (2) revealing the button by setting the alpha so it can be seen again and enabling it. This seemed like it would be simple and effective. Are you sure openPersistentEditor() is the best way to go about this?

  14. #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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    I am quite surprised that you recommended an editor for this task.
    Actually I don't. I only meant this is a way to have a persistant widget in a table cell.

    Are you sure openPersistentEditor() is the best way to go about this?
    In my opinion the best way is to fake the button in the delegate using QStyle::drawControl() when the mouse is hovering the item and handle clicks in the editorEvent() of the delegate. Or to get rid of the button idea completely and simply react on clicks on the item in the view or do something I did some time ago - use the horizontal header as the button block. The latter is probably the best approach as it makes the user intuitively know he can click the header instead of discovering by chance that if he hovers the mouse over the item some button will appear and he'll be able to click it.

    There is also one more possibility which is likely to be the closest to what you want. Subclass the view, reimplement proper events and make it that if the mouse cursor enters the area occupied by some item, you create a real button as a child of the view, position the button over the area of the cell you want and sync the button's position in case the user scrolls the view. Then you'll have one real button you can connect to and handle its clicked() signal directly in the view. Just remember to implement the delegate in such a way that it adds extra space in the column to allow the view to fit the button.
    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. #13
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    Thank you for you commentary, wysota.

    Regarding you statements:

    There is also one more possibility which is likely to be the closest to what you want. Subclass the view, reimplement proper events and make it that if the mouse cursor enters the area occupied by some item, you create a real button as a child of the view, position the button over the area of the cell you want and sync the button's position in case the user scrolls the view.
    I might be doing something like this already. I have subclassed QStyledItemDelegate as something I called ButtonDelegate. This class has both a QLabel and a QPushButton. I then did the following:

    Qt Code:
    1. ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
    To copy to clipboard, switch view to plain text mode 

    Shouldn't I be able to affect the button aspect of ButtonDelegate? Say, make it invisible or disabled?

  16. #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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    I might be doing something like this already. I have subclassed QStyledItemDelegate as something I called ButtonDelegate.
    Then you are not doing something like what I said. The only role of the delegate in my solution is to make space for the button. The button itself is handled entirely in the view class.
    This class has both a QLabel and a QPushButton.
    What do you mean it "has" a label and a button? Show us the code and I'll show you why your approach is wrong.
    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. #15
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    Here is the code.

    I am attempted to adapt the code I found in the "Implementing Custom Delegates" section of C++ GUI Programming with Qt 4, pp 266-271. In that code a QTimeEditor was used in a QTableWidget's cell.

    In contrast, I need to have a pushbutton in a cell of a treeView.

    After reflecting on your comments I reviewed that section of the book and I noticed that the book's example instantiated a QTimeEditor in the openEditor() function. I do not desire for my pushbutton's state to be affected by the openEditor() function because:

    1. by design, the items of this treeview are read-only, the user cannot edit them, and
    2. the concept of 'editing' a pushbutton makes no sense


    What do you mean it "has" a label and a button? Show us the code and I'll show you why your approach is wrong.
    It is the ButtonDelegate class that has a label and a button.

    Qt Code:
    1. // ButtonDelegate.h
    2. class ButtonDelegate : public QStyledItemDelegate
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit ButtonDelegate(QObject *parent=0);
    8. ~ButtonDelegate();
    9.  
    10. void paint( QPainter *painter,
    11. const QStyleOptionViewItem &option,
    12. const QModelIndex &index) const;
    13.  
    14. QSize ButtonDelegate::sizeHint( const QStyleOptionViewItem &option,
    15. const QModelIndex &index) const;
    16. void unpolish(QWidget *widget);
    17.  
    18. void polish(QWidget *widget);
    19.  
    20. void paintWidget( QPainter *painter,
    21. const QRect &rect,
    22. const QString &cacheKey,
    23. QWidget *widget ) const;
    24. protected:
    25.  
    26. void enterEvent ( QEvent * );
    27. void leaveEvent ( QEvent * );
    28.  
    29. private:
    30.  
    31. mutable QTextDocument document;
    32. QPushButton *button;
    33. QLabel *label;
    34.  
    35. };
    36.  
    37. // ButtonDelegate.cpp
    38. ButtonDelegate::ButtonDelegate(QObject *parent) : QStyledItemDelegate(parent)
    39. {
    40. button = new QPushButton( "Wild!");
    41. button->setFixedWidth( 50); // I'm setting this in pixels. There has to be a better way.
    42.  
    43. QPalette pal = button->palette();
    44. pal.setColor(QPalette::ButtonText, QColor(255, 0, 0));
    45. pal.setColor(QPalette::Button, QColor(255, 255, 0));
    46. button->setPalette(pal);
    47.  
    48. label = new QLabel;
    49. label->setTextFormat(Qt::RichText);
    50. label->setWordWrap(false);
    51. }
    52.  
    53.  
    54. //HistoryTreeView.h
    55. class HistoryTreeView : public QTreeView
    56. {
    57. public:
    58. HistoryTreeView(QWidget *parent = 0);
    59.  
    60. protected:
    61. virtual void mouseMoveEvent(QMouseEvent *event);
    62. void enterEvent( QEvent * event );
    63. void leaveEvent( QEvent * event );
    64.  
    65. private:
    66. int m_currentRow;
    67. };
    68.  
    69. //HistoryTreeView.cpp
    70. void HistoryTreeView::mouseMoveEvent(QMouseEvent *event)
    71. {
    72. QAbstractItemModel *m(model());
    73.  
    74. // Only do something when a model is set.
    75. if (m)
    76. {
    77. QModelIndex index = this->indexAt(event->pos());
    78. if (index.isValid())
    79. {
    80. // if the mouse has moved to another row
    81. if (index.row() != m_currentRow)
    82. {
    83. m_currentRow = index.row();
    84.  
    85. // TODO: clear all rows
    86.  
    87. QModelIndex itemIndex = m->sibling(m_currentRow, 0, index );
    88. QWidget * item = indexWidget(itemIndex);
    89. Q_ASSERT(item != NULL );
    90. }
    91. }
    92. else // model is invalid
    93. {
    94. m_currentRow = -1;
    95. }
    96. }
    97. QTreeView::mouseMoveEvent(event);
    98. }
    99.  
    100. //HistoryWindow.h
    101. class HistoryWindow : public QMainWindow
    102. {
    103. Q_OBJECT
    104.  
    105. public:
    106. explicit HistoryWindow(QWidget *parent = 0);
    107. ~HistoryWindow();
    108.  
    109. void setupModel();
    110. void addFile( QStandardItemModel *model,
    111. const QString &anAttribute,
    112. const QString &anotherAttrib,
    113. const QString &path,
    114. const QString &datestamp,
    115. const QString &text );
    116.  
    117. void SetMouseTransitSignals(QWidget *parent);
    118.  
    119. protected:
    120. void enterEvent(QEvent*);
    121. void leaveEvent(QEvent*);
    122.  
    123. private:
    124. Ui::HistoryWindow *ui;
    125. HistoryItemModel *model;
    126. HistoryTreeView *treeView;
    127. void setupData();
    128. void createModelAndView();
    129.  
    130. };
    131.  
    132. //HistoryWindow.cpp
    133. void HistoryWindow::createModelAndView()
    134. {
    135. setupModel();
    136. ui->treeView->setItemDelegateForColumn(0, new ButtonDelegate);
    137. ui->treeView->setAllColumnsShowFocus(true);
    138. ui->treeView->setModel(model);
    139. SetMouseTransitSignals(this);
    140. }
    141.  
    142.  
    143. void HistoryWindow::SetMouseTransitSignals(QWidget *parent)
    144. {
    145. // enable mouse tracking for the treeview.
    146. ui->treeView->setMouseTracking(true);
    147.  
    148. }
    149.  
    150. void HistoryWindow::enterEvent(QEvent*e)
    151. {
    152. QWidget::enterEvent(e);
    153. }
    To copy to clipboard, switch view to plain text mode 

  18. #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: indexWidget() unexpectedly returns a NULL pointer.

    Look into your code. The two widgets you create have nothing to do with the model-view architecture - none of QAbstractItemDelegate methods do anything with them. If you look closely you will notice your delegate class doesn't customize anything from its baseclass nor does it access any of its base class fields or methods - your "ButtonDelegate" class might not have a base class at all - it doesn't provide any delegate functionality its base class wouldn't already be providing. The delegate has three basic functions: 1) it paints the item, 2) it handles editing the item, 3) it handles input events for the item (which is a special case of editing the item). All this is done to bridge the view and the model whereas your "button" has no relation to the model at all and the "label" is completely useless as the item is painted by the delegate's paint() method.
    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.


  19. The following user says thank you to wysota for this useful post:

    ajax (30th November 2010)

  20. #17
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    When do you sleep? You were active pretty late last night. I'm impressed.

    - your "ButtonDelegate" class might not have a base class at all
    Uh, ButtonDelegate is derived from QStyledItemDelegate, wouldn't that be its base class?

    your "button" has no relation to the model at all
    True. The intent of the button is to pop a context sensitive menu. The content of this context sensitive menu would be affected by the data in the other cells on the same row. As of this writing I have not worked out how I am going to access this data; I am still trying to get the button-appearing-on-hover functionality working.

    the "label" is completely useless as the item is painted by the delegate's paint() method.
    The label's text is appearing so it looks like the paint() is doing the job.

    Ok, all of this is just a quick response to your post. I am going to look into QAbstractItemDelegate a see if what it has that I should be using.

  21. #18
    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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    Uh, ButtonDelegate is derived from QStyledItemDelegate, wouldn't that be its base class?
    It is but nothing of its functionality will change if you derive it from... say... QPoint or std::vector. Your delegate class may be derived from a delegate class but it doesn't provide any delegate functionality.

    The label's text is appearing so it looks like the paint() is doing the job.
    Remove the label and it will still be appearing because that's what QStyleDelegate::paint() provides. There is one delegate (hence one label of yours) and say.... 1000 items in the model so is your label showing one text or 1000 different texts?

    Ok, all of this is just a quick response to your post. I am going to look into QAbstractItemDelegate a see if what it has that I should be using.
    The only use for a delegate in your situation that I see is either to reimplement sizeHint() to push the contents of the item aside for the button or to draw the button in the delegate's paint() method using QStyle::drawControl().
    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.


  22. The following user says thank you to wysota for this useful post:

    ajax (30th November 2010)

  23. #19
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: indexWidget() unexpectedly returns a NULL pointer.

    The label's text is appearing so it looks like the paint() is doing the job.
    Remove the label and it will still be appearing because that's what QStyleDelegate:aint() provides. There is one delegate (hence one label of yours) and say.... 1000 items in the model so is your label showing one text or 1000 different texts?
    The buttonDelegate is only used in column 0. Each label for each buttonDelegate shows different text when I run the application because every element of the model is populated with distinct data.

    When I comment-out the QLabel from ButtonDelegate and comment-out all references to it in the code none of the text appears in column 0 when I run the app.


    Added after 6 minutes:


    for what it is worth, this thread touches on some of my concerns.
    Last edited by ajax; 30th November 2010 at 17:44.

  24. #20
    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: indexWidget() unexpectedly returns a NULL pointer.

    Quote Originally Posted by ajax View Post
    The buttonDelegate is only used in column 0. Each label for each buttonDelegate shows different text when I run the application because every element of the model is populated with distinct data.
    Yet you only have one label because you have only one delegate and this single label shows multiple texts simoultaneously in different places. Wow... I wonder what are the values of its "text" and "geometry" properties in this situation. How exactly do you display data on this label?

    When I comment-out the QLabel from ButtonDelegate and comment-out all references to it in the code none of the text appears in column 0 when I run the app.
    What if you also comment out existance of the paint() reimplementation in your delegate or call the base class implementation?
    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.


Similar Threads

  1. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 09:54
  2. Application stops working unexpectedly
    By baluk in forum Newbie
    Replies: 16
    Last Post: 20th November 2010, 15:06
  3. Replies: 1
    Last Post: 28th June 2010, 06:21
  4. QDBusMessage returns NULL string
    By nrabara in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2009, 06:09
  5. Dereferencing a NULL Pointer for staticMetaObject?
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2006, 00:29

Tags for this Thread

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.