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. crash at QLabel::setText
Forum Updated to NodeBB v4.3 + New Features

crash at QLabel::setText

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.7k Views 2 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.
  • D Offline
    D Offline
    django.Reinhard
    wrote on last edited by
    #1

    Hi,

    I want to get into that signal/slot thingi and so I created a little prototype with a simple form containing varios labels and entryfields.
    I created a model class with a signal "valueChanged" and an adapterclass, that takes a label pointer and adds a public slot called "setValue". Implementation of setValue transforms a given value to String and then calls QLabel::setText with that String.
    Modelclass as well as Adapterclass are subclasses of QObject and contain the Q_Object macro.

    I debugged the app with qtcreator and stepped until the call of setText. Everything is fine and the String contains the expected value. Connection between Model and Adapter works so far.
    Then at setText the app crashes.

    What am I doing wrong?

    artwawA 1 Reply Last reply
    0
    • D django.Reinhard

      Hi,

      I want to get into that signal/slot thingi and so I created a little prototype with a simple form containing varios labels and entryfields.
      I created a model class with a signal "valueChanged" and an adapterclass, that takes a label pointer and adds a public slot called "setValue". Implementation of setValue transforms a given value to String and then calls QLabel::setText with that String.
      Modelclass as well as Adapterclass are subclasses of QObject and contain the Q_Object macro.

      I debugged the app with qtcreator and stepped until the call of setText. Everything is fine and the String contains the expected value. Connection between Model and Adapter works so far.
      Then at setText the app crashes.

      What am I doing wrong?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @django-Reinhard can you show your code please? The part you suspect of failing.

      For more information please re-read.

      Kind Regards,
      Artur

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

        Hi
        My best guess would be the label pointer is invalid.
        Are you storing it in a member variable for later use ?

        1 Reply Last reply
        2
        • S Offline
          S Offline
          stretchthebits
          wrote on last edited by
          #4

          How is your QLabel created? What is it called... are you using the designer or do you create the QLabel and other GUI elements yourself?

          eyllanescE 1 Reply Last reply
          0
          • S stretchthebits

            How is your QLabel created? What is it called... are you using the designer or do you create the QLabel and other GUI elements yourself?

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @stretchthebits please provide a minimal and verifiable example.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              django.Reinhard
              wrote on last edited by
              #6

              @artwaw said in crash at QLabel::setText:

              can you show your code please? The part you suspect of failing.

              The point is, I don't have any suspicion.

              @mrjj said in crash at QLabel::setText:

              My best guess would be the label pointer is invalid.
              Are you storing it in a member variable for later use ?

              That sounds interesting!
              Yes, your right - I store the pointer in a member var.
              Could you please explain, what's wrong with that?

              #include <QObject>
              #include <QLabel>
              #include <QVariant>
              
              
              class LabelAdapter : public QObject
              {
                Q_OBJECT
              
              public:
                explicit LabelAdapter(QLabel* label, int realDigits = 3);
              
              public slots:
                void setValue(QVariant value);
              
              private:
                QLabel* lbl;
                int     digits;
                };
              
              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              #include <QTimer>
              
              
              MainWindow::MainWindow(QWidget *parent)
               : QMainWindow(parent)
               , ui(new Ui::MainWindow)
               , counter(0)
               , la0(ui->label_0)
               , la1(ui->label_1)
               , la2(ui->label_2)
               , la3(ui->label_3)
               , la4(ui->label_4)
               , la5(ui->label_5) {
                ui->setupUi(this);
              
                connect(&counter, &ValueModel::valueChanged, &la0, &LabelAdapter::setValue);
                connect(&counter, &ValueModel::valueChanged, &la1, &LabelAdapter::setValue);
                connect(&counter, &ValueModel::valueChanged, &la2, &LabelAdapter::setValue);
                connect(&counter, &ValueModel::valueChanged, &la3, &LabelAdapter::setValue);
                connect(&counter, &ValueModel::valueChanged, &la4, &LabelAdapter::setValue);
                connect(&counter, &ValueModel::valueChanged, &la5, &LabelAdapter::setValue);
                QTimer* t = new QTimer(this);
              
                connect(t, &QTimer::timeout, this, &MainWindow::count);
                t->start(1000);
                }
              
              
              #include "labeladapter.h"
              #include <QTextStream>
              
              LabelAdapter::LabelAdapter(QLabel* label, int realDigits)
               : QObject(nullptr)
               , lbl(label)
               , digits(realDigits) {
                }
              
              
              void LabelAdapter::setValue(QVariant value) {
                if (!lbl) return;
                QString tv;
                QTextStream ts(&tv);
              
                ts.setRealNumberPrecision(digits);
                ts.setRealNumberNotation(QTextStream::FixedNotation);
              
                ts << value.toDouble();
                lbl->setText(tv);
                }
              
              
              eyllanescE 1 Reply Last reply
              0
              • D django.Reinhard

                @artwaw said in crash at QLabel::setText:

                can you show your code please? The part you suspect of failing.

                The point is, I don't have any suspicion.

                @mrjj said in crash at QLabel::setText:

                My best guess would be the label pointer is invalid.
                Are you storing it in a member variable for later use ?

                That sounds interesting!
                Yes, your right - I store the pointer in a member var.
                Could you please explain, what's wrong with that?

                #include <QObject>
                #include <QLabel>
                #include <QVariant>
                
                
                class LabelAdapter : public QObject
                {
                  Q_OBJECT
                
                public:
                  explicit LabelAdapter(QLabel* label, int realDigits = 3);
                
                public slots:
                  void setValue(QVariant value);
                
                private:
                  QLabel* lbl;
                  int     digits;
                  };
                
                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <QTimer>
                
                
                MainWindow::MainWindow(QWidget *parent)
                 : QMainWindow(parent)
                 , ui(new Ui::MainWindow)
                 , counter(0)
                 , la0(ui->label_0)
                 , la1(ui->label_1)
                 , la2(ui->label_2)
                 , la3(ui->label_3)
                 , la4(ui->label_4)
                 , la5(ui->label_5) {
                  ui->setupUi(this);
                
                  connect(&counter, &ValueModel::valueChanged, &la0, &LabelAdapter::setValue);
                  connect(&counter, &ValueModel::valueChanged, &la1, &LabelAdapter::setValue);
                  connect(&counter, &ValueModel::valueChanged, &la2, &LabelAdapter::setValue);
                  connect(&counter, &ValueModel::valueChanged, &la3, &LabelAdapter::setValue);
                  connect(&counter, &ValueModel::valueChanged, &la4, &LabelAdapter::setValue);
                  connect(&counter, &ValueModel::valueChanged, &la5, &LabelAdapter::setValue);
                  QTimer* t = new QTimer(this);
                
                  connect(t, &QTimer::timeout, this, &MainWindow::count);
                  t->start(1000);
                  }
                
                
                #include "labeladapter.h"
                #include <QTextStream>
                
                LabelAdapter::LabelAdapter(QLabel* label, int realDigits)
                 : QObject(nullptr)
                 , lbl(label)
                 , digits(realDigits) {
                  }
                
                
                void LabelAdapter::setValue(QVariant value) {
                  if (!lbl) return;
                  QString tv;
                  QTextStream ts(&tv);
                
                  ts.setRealNumberPrecision(digits);
                  ts.setRealNumberNotation(QTextStream::FixedNotation);
                
                  ts << value.toDouble();
                  lbl->setText(tv);
                  }
                
                
                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #7

                @django-Reinhard all the QWidget implemented by QtDesigner are null until setupUI is invoked, in your case you access label_X when they are null causing the error. The solution is to initialize laX after setupUi.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                1 Reply Last reply
                2
                • D Offline
                  D Offline
                  django.Reinhard
                  wrote on last edited by
                  #8

                  @mrjj - Thank you very much for your questions. They leaded me to the right solution.

                  @eyllanesc said in crash at QLabel::setText:

                  all the QWidget implemented by QtDesigner are null until setupUI is invoked

                  That's wrong!
                  I verified the pointer are not null - during debug session.
                  If they where null, I had a path to follow ...

                  @eyllanesc said in crash at QLabel::setText:

                  The solution is to initialize laX after setupUi.

                  That's right. Thank you.
                  I changed Adapter instances to member pointers instead of member vars and so I had to initialise them after setupUI.
                  Now all is working.

                  Thank you all for your attention

                  eyllanescE 1 Reply Last reply
                  0
                  • D django.Reinhard

                    @mrjj - Thank you very much for your questions. They leaded me to the right solution.

                    @eyllanesc said in crash at QLabel::setText:

                    all the QWidget implemented by QtDesigner are null until setupUI is invoked

                    That's wrong!
                    I verified the pointer are not null - during debug session.
                    If they where null, I had a path to follow ...

                    @eyllanesc said in crash at QLabel::setText:

                    The solution is to initialize laX after setupUi.

                    That's right. Thank you.
                    I changed Adapter instances to member pointers instead of member vars and so I had to initialise them after setupUI.
                    Now all is working.

                    Thank you all for your attention

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #9

                    @django-Reinhard I think I was wrong, what I should have said is that it is not initialized, you are right that it is not null. In conclusion in your code you are accessing uninitialized pointers, the logic is something like:

                    // before setupUI
                    QLabel *label;
                    // after setupUi
                    label = new QLabel;
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    3

                    • Login

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