The inferior stopped because it triggered an exception. Stopped in thread 0 by: Exception at 0x7ff9033da03a, code: 0xc0000005: read access violation at: 0x6c, flags=0x0 (first chance).
-
Hello!
I am in need of some help. I am trying to get a label to periodically refresh every single second. However, when I try to create a private/public function to connect to a timer it crashes.
hud.h:
#ifndef HUD_H
#define HUD_H#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <qobject.h>class Hud : public QWidget
{
Q_OBJECTpublic:
Hud();public slots:
void update();private:
void updateText();QHBoxLayout *hbox; QVBoxLayout *vboxleft; QVBoxLayout *vboxright; QLabel *vertically; QLabel *laid; QLabel *out; QLabel *text; QLabel *verticallyright; QLabel *laidright; QLabel *outright; QLabel *textright; QTimer *timer; int num = 1;
};
#endif // HUD_H
hud.cpp:
#include "hud.h"
Hud::Hud(){
// macro structure /*QHBoxLayout *hbox = new QHBoxLayout(this); QVBoxLayout *vboxleft = new QVBoxLayout(); QVBoxLayout *vboxright = new QVBoxLayout(); // left side text wall QLabel *vertically = new QLabel(); vertically->setText("Vertically"); QLabel *laid = new QLabel(); laid->setText("Laid"); QLabel *out = new QLabel(); out->setText("Out"); QLabel *text = new QLabel(); text->setText("Text"); // right side text wall QLabel *verticallyright = new QLabel(); verticallyright->setText("Vertically right"); QLabel *laidright = new QLabel(this); laidright->setText("Laid"); QLabel *outright = new QLabel(); outright->setText("Out right"); QLabel *textright = new QLabel(); textright->setText("Text right"); // add text wall to left vbox vboxleft->addWidget(vertically); vboxleft->addWidget(laid); vboxleft->addWidget(out); vboxleft->addWidget(text); // add text wall to right vbox vboxright.addWidget(verticallyright); vboxright.addWidget(laidright); vboxright.addWidget(outright); vboxright.addWidget(textright); // add left and right vbox to master hbox hbox->addLayout(vboxleft); hbox->addLayout(vboxright); //QTimer *timer = new QTimer(this); //QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update())); //timer->start(1000); updateText();*/ QLabel *textright = new QLabel(this); textright->setText("text"); updateText();
};
void Hud::updateText(){
textright->setText(QString(num));
};
void Hud::update(){
updateText();
}Main.cpp:
#include "hud.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication app(argc, argv);Hud hud; hud.resize(240, 230); hud.setWindowTitle("hud"); hud.show(); return app.exec();
}
Thank you for your attention!
-
It crashes at line:
textright->setText(QString(num)); -
Hi
I think you hit a classic one.QLabel *textright = new QLabel(this); <<< local variable you new
textright->setText("text");void Hud::updateText(){ textright->setText(QString(num)); <<<< this variable is the one from .h . the member };
so you do not new the member textright widget, but created a new local one instead.
(so the one in Hud .h is a dangling pointer )so what you want is
textright = new QLabel(this); // new the member one.
-
That fixed it. Thank you very much!