Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 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).
Forum Updated to NodeBB v4.3 + New Features

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).

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tague Carlyon
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tague Carlyon
      wrote on last edited by
      #2

      It crashes at line:
      textright->setText(QString(num));

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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.

        1 Reply Last reply
        4
        • T Offline
          T Offline
          Tague Carlyon
          wrote on last edited by
          #4

          That fixed it. Thank you very much!

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved