[solved] Qt4 - Program written in QtCreator segfaults



  • The following code is suppose to change a property of a progress bar when a button is clicked.
    I tried to do the same in Qt3Designer and everything worked fine.
    What happens is as soon as the button is clicked, the program crashes with the following message:

    Starting /home/user/qt4/test1/test1...
    The program has unexpectedly finished.
    /home/user/qt4/test1/test1 exited with code 0

    Here's mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "kled.h"
    #include <QLCDNumber>
    #include <QProgressBar>

    QProgressBar *qprogressbar;
    QLCDNumber *lcdnumber;
    KLed *kled;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::changeEvent(QEvent *e)
    {

    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
    

    }

    void MainWindow::on_pushButton_clicked()
    {
    qprogressbar->setValue(30);
    }

    @

    Thank you.


  • Moderators

    Where are you instantiating qprogressbar?



  • Thanks for your quick reply.
    Disclaimer: I am not a C++ programmer. I just create frontends for my C based console programs that drive my instruments.

    Back to my post now, I thought that QtCreator did it automatically (pretty much like in Qt3-Designer).
    I mean since all widgets already were placed on the form, then they should already have been instantiated, right?


  • Moderators

    Based on your comments, I'm assuming you have a widget in the Ui named qprogressbar?

    If so, you need to get rid of line 7 in your code above ( (It doesn't refer to the progressbar in your ui. It's just an unused global pointer) , and replace line 42 with
    @
    ui->qprogressbar->setValue(30)
    @
    as anything that is created in the Ui file will be available through that pointer. There's no need to redeclare pointers for the Ui widgets in your code.



  • That worked. Next stop: get the fundamentals of C++.

    Thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.