Results 1 to 2 of 2

Thread: Unexplained Crash

  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Unexplained Crash

    Hi all, I am fairly new at QT and have downloaded the new SDK and have the latest QT libraries (which i believe is 4.5?). I wanted to make a quick widget to allow me to run a test program, showing simple LCD and displays, however whenever I run it the QT program crashes on me, with no error codes. Anyway, here is the code...

    ************************************************** **************

    //#include <QtCore/QCoreApplication>

    #include <QApplication>
    #include <QLCDNumber>
    #include <QPushButton>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QPushButton>

    #include "digitalclock.h"
    #include <stdlib.h>
    #include <stdio.h>

    #define userMinTemp 15

    char display[10];
    int newTemp = 0;

    /* Widget Class Creator */
    class MyWidget : public QWidget
    {
    public:
    MyWidget(QWidget *parent = 0);

    public slots:
    void MyWidget::newTempMain();
    };
    MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    {
    QPushButton *quit = new QPushButton(tr("Quit"));
    quit->setFont(QFont("Times", 12, QFont::Bold));

    QLCDNumber *lcd = new QLCDNumber(newTemp);
    lcd->setSegmentStyle(QLCDNumber::Filled);

    QPushButton *devDisplay = new QPushButton(display);
    devDisplay->setFont(QFont("Times",18,QFont::Bold));

    //DigitalClock clock;

    // Quit when clicked Quits...
    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    // devDisplay when clicked creates a new temp and says whether or not ac/heat
    connect(devDisplay, SIGNAL(clicked()), this, SLOT(newTemp()));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(devDisplay);
    layout->addWidget(lcd);
    layout->addWidget(quit);
    //layout->addWidget(clock);
    setLayout(layout);

    }

    void MyWidget::newTempMain()
    {
    newTemp = (rand()%101);

    if( newTemp <= userMinTemp )
    {
    strncpy("Heat", display, sizeof(display));
    //display = "Heat";
    }
    else
    {
    strncpy("AC", display, sizeof(display));
    //display = "AC";
    }
    }

    void newTempMain()
    {
    newTemp = (rand()%101);

    if( newTemp <= userMinTemp )
    {
    strncpy("Heat", display, sizeof(display));
    //display = "Heat";
    }
    else
    {
    strncpy("AC", display, sizeof(display));
    //display = "AC";
    }
    }

    int main(int argc, char *argv[])
    {
    newTempMain();

    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
    }

    ************************************************** ***************

    If there are any suggestions or anything that anyone sees out of the ordinary or that causes potential crashing, please let me know...its buggin the living hell outta me.

    Thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unexplained Crash

    first of all you need to add Q_OBJECT marco right here
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. ...
    To copy to clipboard, switch view to plain text mode 
    if you don't do this then signal/slot mechanism will not work.

    then there is no needed to use strncpy, QString already has all what you need for working with strings.

    are these variables quit, lcd & devDisplay declared as class member, ?
    if not then this can cause a crash, because in ctor you have initialized local variables.

    PS. use tag code please.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Crash: Heap corruption due to selectedRows()
    By Ankitha Varsha in forum Qt Programming
    Replies: 16
    Last Post: 1st October 2010, 00:55
  2. Replies: 28
    Last Post: 9th March 2010, 08:59
  3. Replies: 2
    Last Post: 13th August 2008, 17:46
  4. QTimer ->start(0) + OpenGL + resize/move window => crash
    By anthibug in forum Qt Programming
    Replies: 5
    Last Post: 8th July 2008, 11:01
  5. Crash handler on Win32
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 19:41

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.