Results 1 to 6 of 6

Thread: QTreeWidget crash (improver use?)

  1. #1
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeWidget crash (improver use?)

    Hello!

    I created a QTreeWidget with QToolButtons on sub level items, and by clicking on the button it should be removed from the treewidget with it's QTreeWidgetItem.
    The problem is when i expand the first top level item, it's crashes.

    May i use QTreeWidget improperly?
    Please help!

    Here is the code representing my problem:

    Qt Code:
    1. #ifndef TRASH2_H
    2. #define TRASH2_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QToolButton>
    6. #include <QStringList>
    7. #include "ui_trash2.h"
    8.  
    9. class trash2 : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. trash2(QWidget *parent = 0, Qt::WFlags flags = 0)
    15. : QMainWindow(parent, flags)
    16. {
    17. ui.setupUi(this);
    18.  
    19. QTreeWidgetItem* item = 0;
    20. item = addTopLevelItem( 0 );
    21. addSubLevelItem( item, 0 );
    22. addSubLevelItem( item, 1 );
    23. addSubLevelItem( item, 2 );
    24.  
    25. }
    26.  
    27. ~trash2()
    28. {
    29.  
    30. }
    31.  
    32. QTreeWidgetItem* addTopLevelItem(int i)
    33. {
    34. QTreeWidgetItem* item = 0;
    35. list << QString("Top%1").arg(i);
    36. item = new QTreeWidgetItem( list );
    37. ui.treeWidget->addTopLevelItem( item );
    38.  
    39. return item;
    40. }
    41.  
    42. void addSubLevelItem(QTreeWidgetItem* parentItem, int i)
    43. {
    44. QTreeWidgetItem* item = 0;
    45. QToolButton* button = 0;
    46. item = new QTreeWidgetItem( parentItem );
    47. button = new QToolButton( ui.treeWidget );
    48. button->setText( QString("button%1").arg(i) );
    49. button->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
    50. button->setAutoRaise( true );
    51. button->setChecked( false );
    52.  
    53. connect( button, SIGNAL( pressed() ), this, SLOT( buttonPressed() ) );
    54. parentItem->addChild( item );
    55. vmi[button] = item;
    56. ui.treeWidget->setItemWidget( item, 0, button );
    57. }
    58.  
    59. public slots:
    60. void buttonPressed()
    61. {
    62. QToolButton* button = dynamic_cast<QToolButton* >( sender() );
    63. if ( button )
    64. {
    65. QTreeWidgetItem* item = vmi[button];
    66. vmi.remove( button );
    67. button->disconnect();
    68. button->deleteLater();
    69. QTreeWidgetItem* parent = item->parent();
    70. if ( parent )
    71. {
    72. parent->removeChild( item );
    73. }
    74. delete item;
    75. }
    76. }
    77.  
    78. private:
    79. Ui::trash2Class ui;
    80. QMap<QToolButton* , QTreeWidgetItem* > vmi;
    81. };
    82.  
    83. #endif // TRASH2_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTreeWidget crash (improver use?)

    First of all, the code you posted is very weird.
    Why do you code the implementation in the header? Are you coming from a C# world?

    Quote Originally Posted by csernai.csaba View Post
    The problem is when i expand the first top level item, it's crashes.
    I copied your code and tried it for myself. It doesn't crash for me.
    Post the error you get please.

    May i use QTreeWidget improperly?
    The use of ui is a little bit strange to me.
    And you should include QTreeWidgetItem

  3. #3
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget crash (improver use?)

    Quote Originally Posted by tbscope View Post
    First of all, the code you posted is very weird.
    Why do you code the implementation in the header? Are you coming from a C# world?
    Nope. It's easier to post here. The source and the header is separated in my solution.
    And it is a samle code.

    Quote Originally Posted by tbscope View Post
    I copied your code and tried it for myself. It doesn't crash for me.
    Post the error you get please.
    I don't know if it matters, i compile it with VS 2008 x64 and run it on a Windows 7 x64 system.
    Qt Code:
    1. Unhandled exception at 0x65948c50 (QtGuid4.dll) in trash2.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.
    To copy to clipboard, switch view to plain text mode 

    It's crashes in this part ( should i post the Call Stack? ):
    Qt Code:
    1. QVariant QTreeModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5. QTreeWidgetItem *itm = item(index);
    6. if (itm)
    7. return itm->data(index.column(), role);
    8. return QVariant();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by tbscope View Post
    The use of ui is a little bit strange to me.
    And you should include QTreeWidgetItem
    Iit was generated using the default Qt Application project settings in VS2008.
    I think the QTreeWidget.h includes QTreeWidgetItem and QTreeWidget is included in the ui's header.
    Attached Files Attached Files
    Last edited by csernai.csaba; 18th June 2010 at 09:27. Reason: Added .ui form

  4. #4
    Join Date
    May 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget crash (improver use?)

    I know this is an old thread, but its fits by problem. Im using Qt5, and am trying to learn about QTreeWidgets. I am getting a crash each time the app runs. I narrowed it down to when I create the QTreeWidget variable.

    int main()
    {
    .....

    QTreeWidget treeWidget; // I crash here

    .....
    }

    The error I get is:
    "The inferior stopped because it triggered an exception.
    Stopped in thread 0....... Read access violation.

    Anyone know what might be wrong?

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QTreeWidget crash (improver use?)

    not sure what your problem is, but why add all the things on the header file?

    EDIT:
    Sorry it didnt even show any of the otehr comments can this be deleted?

  6. #6
    Join Date
    May 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget crash (improver use?)

    Nevermind. I found the error of my ways. I was not doing it as a GUI app. DOH!! Anyways, I started a GUI app and the QTreeWidget works just fine

Similar Threads

  1. QComboBox crash
    By sfilez in forum Newbie
    Replies: 4
    Last Post: 8th July 2009, 00:31
  2. Replies: 2
    Last Post: 17th March 2008, 13:53
  3. QDevelop 0.25 crash
    By peppino in forum Qt-based Software
    Replies: 7
    Last Post: 17th December 2007, 22:02
  4. Replies: 1
    Last Post: 19th October 2007, 02:29
  5. QTreeWidget->clear() causing crash after sort
    By Chuk in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2007, 10:59

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.