PDA

View Full Version : Program crash when I use ui.pushButton->setEnabled(true); or false



ruben.rodrigues
21st July 2010, 13:38
Hi all!

I have a program that reads messages from a machine and places the messages in a QTreeWidget as QTreeWidgetItems.

The messages have multiple properties like: ID, Text, Type etc.
when the user clicks on the message (QTreeWidgetItem) it activates a slot and according to the message flags it should set the buttons enable or disable them but it crashes!!


Header:


#ifndef MESSAGEPOPUPS_H
#define MESSAGEPOPUPS_H

#include <QtGui>
#include "ui_messagepopups.h"
#include <QTreeWidgetItem>
#include <QList>

class messagePopups : public QWidget
{
Q_OBJECT

public:
messagePopups(QWidget *parent = 0);
~messagePopups();

void set_new_message(QByteArray qba);

qint16 number_of_messages;
QTreeWidgetItem *messageText[];

private:
Ui::messagePopupsClass ui;

private slots:
void treeItemClicked(QTreeWidgetItem* test,int item);
};

#endif // MESSAGEPOPUPS_H



cpp:


#include "messagepopups.h"

#include <iostream>
using namespace std;

messagePopups::messagePopups(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
number_of_messages = 1;

}
messagePopups::~messagePopups()
{

}

void messagePopups::set_new_message(QByteArray qba)
{

double _Flags, _MessageType, Ticks;
QString _MessageText;
QString Message_type;
QDataStream data(&qba, QIODevice::ReadOnly);
data >> _Flags;
data >> _MessageText;
data >> _MessageType;
data >> Ticks;

int i = _MessageType;
switch (i){
case 0: Message_type = "mTypeFatal";
break;
case 1: Message_type = "mTypeError";
break;
case 2: Message_type = "mTypeStep";
break;
case 3: Message_type = "mTypeWarning";
break;
case 4: Message_type = "mTypeInfo";
break;
}


messageText[number_of_messages] = new QTreeWidgetItem();//ui.treeWidget->columnAt(0));

ui.treeWidget->addTopLevelItem(messageText[number_of_messages]);

QStringList HeaderLabels;
HeaderLabels << "ID" << "Message Text" << "Message ID" << "Message Type" << "Ticks" << "Task" << "Task ID";
ui.treeWidget->setHeaderLabels(HeaderLabels);

messageText[number_of_messages]->setText(0,QString::number(number_of_messages)); //Message Number
messageText[number_of_messages]->setText(1,_MessageText); //Message Text
messageText[number_of_messages]->setText(2,""); //Message ID
messageText[number_of_messages]->setText(3,Message_type); //Message Type
messageText[number_of_messages]->setText(4,QString::number(Ticks)); //Ticks
messageText[number_of_messages]->setText(5,"test4"); //Task
messageText[number_of_messages]->setText(6,"test5"); //Task ID
messageText[number_of_messages]->setText(7,QString::number(_Flags)); //Flag/mode

connect(ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(treeItemClicked(QTreeWidgetItem*,int)));
number_of_messages = number_of_messages + 1;
}

void messagePopups::treeItemClicked(QTreeWidgetItem* test,int item)
{
int RowID = test->text(0).toInt();

int Flag = messageText[RowID]->text(7).toInt();

switch (Flag)
{
case 4: cout << "@4" << endl;
ui.pushButton->setEnabled(true);
ui.pushButton_2->setEnabled(false);
ui.pushButton_3->setEnabled(false);


case 5: cout << "@5" << endl;
ui.pushButton->setEnabled(true);
ui.pushButton_2->setEnabled(false);
ui.pushButton_3->setEnabled(true);


case 7: cout << "@7" << endl;
ui.pushButton->setEnabled(true);
ui.pushButton_2->setEnabled(true);
ui.pushButton_3->setEnabled(true);

}
cout << "Ended" << endl;
}



I can see where the crash happens because of the cout's. Like I see @7 when the flag is 7 but the cout << "Ended" never get executed and the program closes.

What can be wrong?

Thank in advance

Zlatomir
21st July 2010, 15:02
I don't see any break; statement in your switch, this is the way you wanted it?

And use cerr instead of cout, for your purpose it's better because it's not buffered, so in you case, you can't know if cout << "End"; executed or not, you just know that it didn't make it to the screen,
or you can use qDebug(),
but the best way to do this is a debugger.

ruben.rodrigues
21st July 2010, 15:19
I don't see any break; statement in your switch, this is the way you wanted it?

And use cerr instead of cout, for your purpose it's better because it's not buffered, so in you case, you can't know if cout << "End"; executed or not, you just know that it didn't make it to the screen,
or you can use qDebug(),
but the best way to do this is a debugger.

Good point. I forgot the break but that was not the problem.
I used the debuger but I don't see nothing wrong. It reaches the cycle, the flag is ok but then it closes everything..

Zlatomir
21st July 2010, 15:21
There is no error?

ruben.rodrigues
21st July 2010, 15:31
There is no error?


humm.. there is. Just saw now!

No source available for "QWidget::testAttribute_helper() at 0xb6e82391"

test is the name of the QTreeWidgetItem. But what is wrong with it?

Lykurg
21st July 2010, 15:33
could you post the backtrace?

ruben.rodrigues
22nd July 2010, 06:39
could you post the backtrace?

Where do I find the backtrace on eclipse? Is it this 0xb6fee391: test %eax,0xd0(%edx,%esi,4)

aamer4yu
22nd July 2010, 07:09
I guess problem is with QTreeWidgetItem *messageText[];
You are declaring array of pointers, creating new item messageText[number_of_messages] = new QTreeWidgetItem();
Am not sure you can access that without knowing how big the array is , isnt it :confused:

I dont think you even need to have messageText. You can simply create a new QTreeWidget item, and add it to the QTreeWidget.
instead of - messageText[number_of_messages] = new QTreeWidgetItem();
you simply need QTreeWidgetItem *item = new QTreeWidgetItem();

then in treeItemClicked

int RowID = test->text(0).toInt();
int Flag = messageText[RowID]->text(7).toInt();
Wont messageText[RowID] be equal to test itself ?? :rolleyes:
so you can simply have
int Flag = test->text(7).toInt();

Hope I didnt miss something ;)

ruben.rodrigues
22nd July 2010, 07:19
I guess problem is with QTreeWidgetItem *messageText[];
You are declaring array of pointers, creating new item messageText[number_of_messages] = new QTreeWidgetItem();
Am not sure you can access that without knowing how big the array is , isnt it :confused:

I dont think you even need to have messageText. You can simply create a new QTreeWidget item, and add it to the QTreeWidget.
instead of - messageText[number_of_messages] = new QTreeWidgetItem();
you simply need QTreeWidgetItem *item = new QTreeWidgetItem();

then in treeItemClicked

int RowID = test->text(0).toInt();
int Flag = messageText[RowID]->text(7).toInt();
Wont messageText[RowID] be equal to test itself ?? :rolleyes:
so you can simply have
int Flag = test->text(7).toInt();

Hope I didnt miss something ;)

Hi!

You were right when you said that messageText[RowID] is the same as test :D My bad...
but the problem remains...even if I set the flag manualy it crashes..

aamer4yu
22nd July 2010, 08:40
Whats your updated code now ?