Results 1 to 20 of 34

Thread: indexWidget() unexpectedly returns a NULL pointer.

Hybrid View

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

    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 69 Times in 67 Posts

    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

Similar Threads

  1. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 10:54
  2. Application stops working unexpectedly
    By baluk in forum Newbie
    Replies: 16
    Last Post: 20th November 2010, 16:06
  3. Replies: 1
    Last Post: 28th June 2010, 07:21
  4. QDBusMessage returns NULL string
    By nrabara in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2009, 07:09
  5. Dereferencing a NULL Pointer for staticMetaObject?
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2006, 01: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
  •  
Qt is a trademark of The Qt Company.