PDA

View Full Version : QTreeWidget crash (improver use?)



csernai.csaba
18th June 2010, 07:08
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:


#ifndef TRASH2_H
#define TRASH2_H

#include <QtGui/QMainWindow>
#include <QToolButton>
#include <QStringList>
#include "ui_trash2.h"

class trash2 : public QMainWindow
{
Q_OBJECT

public:
trash2(QWidget *parent = 0, Qt::WFlags flags = 0)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QTreeWidgetItem* item = 0;
item = addTopLevelItem( 0 );
addSubLevelItem( item, 0 );
addSubLevelItem( item, 1 );
addSubLevelItem( item, 2 );

}

~trash2()
{

}

QTreeWidgetItem* addTopLevelItem(int i)
{
QTreeWidgetItem* item = 0;
QStringList list;
list << QString("Top%1").arg(i);
item = new QTreeWidgetItem( list );
ui.treeWidget->addTopLevelItem( item );

return item;
}

void addSubLevelItem(QTreeWidgetItem* parentItem, int i)
{
QTreeWidgetItem* item = 0;
QToolButton* button = 0;
QStringList list;
item = new QTreeWidgetItem( parentItem );
button = new QToolButton( ui.treeWidget );
button->setText( QString("button%1").arg(i) );
button->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
button->setAutoRaise( true );
button->setChecked( false );

connect( button, SIGNAL( pressed() ), this, SLOT( buttonPressed() ) );
parentItem->addChild( item );
vmi[button] = item;
ui.treeWidget->setItemWidget( item, 0, button );
}

public slots:
void buttonPressed()
{
QToolButton* button = dynamic_cast<QToolButton* >( sender() );
if ( button )
{
QTreeWidgetItem* item = vmi[button];
vmi.remove( button );
button->disconnect();
button->deleteLater();
QTreeWidgetItem* parent = item->parent();
if ( parent )
{
parent->removeChild( item );
}
delete item;
}
}

private:
Ui::trash2Class ui;
QMap<QToolButton* , QTreeWidgetItem* > vmi;
};

#endif // TRASH2_H

tbscope
18th June 2010, 07:51
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?


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

csernai.csaba
18th June 2010, 08:02
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.



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.

Unhandled exception at 0x65948c50 (QtGuid4.dll) in trash2.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.

It's crashes in this part ( should i post the Call Stack? ):

QVariant QTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QTreeWidgetItem *itm = item(index);
if (itm)
return itm->data(index.column(), role);
return QVariant();
}




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.

changedsoul
24th March 2013, 01:49
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?

giblit
24th March 2013, 05:09
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?

changedsoul
24th March 2013, 21:17
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