Results 1 to 10 of 10

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

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Program crash when I use ui.pushButton->setEnabled(true); or false

    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:

    Qt Code:
    1. #ifndef MESSAGEPOPUPS_H
    2. #define MESSAGEPOPUPS_H
    3.  
    4. #include <QtGui>
    5. #include "ui_messagepopups.h"
    6. #include <QTreeWidgetItem>
    7. #include <QList>
    8.  
    9. class messagePopups : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. messagePopups(QWidget *parent = 0);
    15. ~messagePopups();
    16.  
    17. void set_new_message(QByteArray qba);
    18.  
    19. qint16 number_of_messages;
    20. QTreeWidgetItem *messageText[];
    21.  
    22. private:
    23. Ui::messagePopupsClass ui;
    24.  
    25. private slots:
    26. void treeItemClicked(QTreeWidgetItem* test,int item);
    27. };
    28.  
    29. #endif // MESSAGEPOPUPS_H
    To copy to clipboard, switch view to plain text mode 


    cpp:

    Qt Code:
    1. #include "messagepopups.h"
    2.  
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. messagePopups::messagePopups(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. ui.setupUi(this);
    10. number_of_messages = 1;
    11.  
    12. }
    13. messagePopups::~messagePopups()
    14. {
    15.  
    16. }
    17.  
    18. void messagePopups::set_new_message(QByteArray qba)
    19. {
    20.  
    21. double _Flags, _MessageType, Ticks;
    22. QString _MessageText;
    23. QString Message_type;
    24. QDataStream data(&qba, QIODevice::ReadOnly);
    25. data >> _Flags;
    26. data >> _MessageText;
    27. data >> _MessageType;
    28. data >> Ticks;
    29.  
    30. int i = _MessageType;
    31. switch (i){
    32. case 0: Message_type = "mTypeFatal";
    33. break;
    34. case 1: Message_type = "mTypeError";
    35. break;
    36. case 2: Message_type = "mTypeStep";
    37. break;
    38. case 3: Message_type = "mTypeWarning";
    39. break;
    40. case 4: Message_type = "mTypeInfo";
    41. break;
    42. }
    43.  
    44.  
    45. messageText[number_of_messages] = new QTreeWidgetItem();//ui.treeWidget->columnAt(0));
    46.  
    47. ui.treeWidget->addTopLevelItem(messageText[number_of_messages]);
    48.  
    49. QStringList HeaderLabels;
    50. HeaderLabels << "ID" << "Message Text" << "Message ID" << "Message Type" << "Ticks" << "Task" << "Task ID";
    51. ui.treeWidget->setHeaderLabels(HeaderLabels);
    52.  
    53. messageText[number_of_messages]->setText(0,QString::number(number_of_messages)); //Message Number
    54. messageText[number_of_messages]->setText(1,_MessageText); //Message Text
    55. messageText[number_of_messages]->setText(2,""); //Message ID
    56. messageText[number_of_messages]->setText(3,Message_type); //Message Type
    57. messageText[number_of_messages]->setText(4,QString::number(Ticks)); //Ticks
    58. messageText[number_of_messages]->setText(5,"test4"); //Task
    59. messageText[number_of_messages]->setText(6,"test5"); //Task ID
    60. messageText[number_of_messages]->setText(7,QString::number(_Flags)); //Flag/mode
    61.  
    62. connect(ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(treeItemClicked(QTreeWidgetItem*,int)));
    63. number_of_messages = number_of_messages + 1;
    64. }
    65.  
    66. void messagePopups::treeItemClicked(QTreeWidgetItem* test,int item)
    67. {
    68. int RowID = test->text(0).toInt();
    69.  
    70. int Flag = messageText[RowID]->text(7).toInt();
    71.  
    72. switch (Flag)
    73. {
    74. case 4: cout << "@4" << endl;
    75. ui.pushButton->setEnabled(true);
    76. ui.pushButton_2->setEnabled(false);
    77. ui.pushButton_3->setEnabled(false);
    78.  
    79.  
    80. case 5: cout << "@5" << endl;
    81. ui.pushButton->setEnabled(true);
    82. ui.pushButton_2->setEnabled(false);
    83. ui.pushButton_3->setEnabled(true);
    84.  
    85.  
    86. case 7: cout << "@7" << endl;
    87. ui.pushButton->setEnabled(true);
    88. ui.pushButton_2->setEnabled(true);
    89. ui.pushButton_3->setEnabled(true);
    90.  
    91. }
    92. cout << "Ended" << endl;
    93. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    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.

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    Quote Originally Posted by Zlatomir View Post
    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..

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    There is no error?

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    Quote Originally Posted by Zlatomir View Post
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    could you post the backtrace?

  7. #7
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    Quote Originally Posted by Lykurg View Post
    could you post the backtrace?
    Where do I find the backtrace on eclipse? Is it this 0xb6fee391: test %eax,0xd0(%edx,%esi,4)

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    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

    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
    Qt Code:
    1. int RowID = test->text(0).toInt();
    2. int Flag = messageText[RowID]->text(7).toInt();
    To copy to clipboard, switch view to plain text mode 
    Wont messageText[RowID] be equal to test itself ??
    so you can simply have
    int Flag = test->text(7).toInt();

    Hope I didnt miss something

  9. #9
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    Quote Originally Posted by aamer4yu View Post
    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

    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
    Qt Code:
    1. int RowID = test->text(0).toInt();
    2. int Flag = messageText[RowID]->text(7).toInt();
    To copy to clipboard, switch view to plain text mode 
    Wont messageText[RowID] be equal to test itself ??
    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 My bad...
    but the problem remains...even if I set the flag manualy it crashes..

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Program crash when I use ui.pushButton->setEnabled(true); or false

    Whats your updated code now ?

Similar Threads

  1. Qt for S60, compiler warning regarding TRUE/FALSE
    By Archimedes in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 22nd October 2009, 15:09
  2. Program Crash
    By jonmatteo in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2009, 15:47
  3. Replies: 5
    Last Post: 28th April 2009, 00:29
  4. Replies: 3
    Last Post: 29th May 2008, 14:50
  5. Program crash
    By Pragya in forum Qt Programming
    Replies: 7
    Last Post: 29th June 2007, 08:37

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.