[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 0Here'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.
-
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? -
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.