#Qt - double label->setText(); crashes app

49 messages · Page 1 of 1 (latest)

shadow parrot
#

Hello, I'm trying to create a switch using QPushButton but double on_off_label->setText(); crashes it (if I delete one it's fine)

forest zephyrBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

shadow parrot
#

code:

void Switch::updateStyle() {
    if (isChecked()) {
        // ON
        setIcon(QIcon("D://images/switch_ON.png"));
        setIconSize(QSize(width(), height()));
        on_off_label->setText("ON");
    } else {
        // OFF
        setIcon(QIcon("D:/images/switch_OFF.png"));
        setIconSize(QSize(width(), height()));
        on_off_label->setText("OFF");
    }
}
#

!code

forest zephyrBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
mild fiber
# shadow parrot Hello, I'm trying to create a switch using QPushButton but double on_off_label->...

Hard to know with details provided. What is the nature of the "crash"? Is it possible that Switch::updateStyle is called on a deleted object or on_off_label has not been initialized properly or deleted?

Make sure on_off_label is valid before using:

if (on_off_label) {
    on_off_label->setText(isChecked() ? "ON" : "OFF");
} else {
    qDebug() << "on_off_label is null!";
}

Make sure on_off_switch is properly initialized in the constructor:

Switch::Switch(QWidget *parent) : QPushButton(parent) {
    on_off_label = new QLabel(this);
}

Check this pointer inside updateStyle():

qDebug() << "this pointer:" << this;
shadow parrot
# mild fiber Hard to know with details provided. What is the nature of the "crash"? Is it p...

by "crash" I mean the app closes itself without visible error. The Switch class is in a header file and I'm not entirely sure if it's written properly.

Hearder file:

#ifndef SWITCH_H
#define SWITCH_H

#include <QPushButton>
#include <QLabel>

class Switch : public QPushButton {
    Q_OBJECT

public:
    explicit Switch(int width = 120, int height = 60, QWidget *parent = nullptr);

private slots:
    void updateStyle();
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

private:
    QLabel* on_off_label;
};

#endif // SWITCH_H
lyric sail
#

Source file?

shadow parrot
lyric sail
#

yes

shadow parrot
#
Switch::Switch(const int width, const int height, QWidget *parent) : QPushButton(parent) {
    setCheckable(true);
    setFixedSize(width, height);
    updateStyle();

    setStyleSheet("QPushButton { background: none; border: none; }"
                  "QPushButton:hovered { background: none; border: none; }");

    connect(this, &QPushButton::toggled, this, &Switch::updateStyle);
    on_off_label = new QLabel("OFF", this);
    on_off_label->setStyleSheet("color: #efefef");

}

void Switch::updateStyle() {
    if (isChecked()) {
        // ON
        setIcon(QIcon("D:/images/switch_ON.png"));
        setIconSize(QSize(width(), height()));
        on_off_label->setText("ON");
    } else {
        // OFF
        setIcon(QIcon("D:/images/switch_OFF.png"));
        setIconSize(QSize(width(), height()));
        on_off_label->setText("OFF");
    }
}


void Switch::mousePressEvent(QMouseEvent *event) {
    QPushButton::mousePressEvent(event);  // doesnt work yet
}

void Switch::mouseReleaseEvent(QMouseEvent *event) {
    QPushButton::mouseReleaseEvent(event);  // doesnt work yet
}
lyric sail
#

In the constructor, you're calling updateStyle(); before the line

    on_off_label = new QLabel("OFF", this);
shadow parrot
#

setStyleSheet is is related to button

#

but label creates problems

lyric sail
#

?

shadow parrot
#

the problem is in on_off_label->setText("ON"); line

#

or on_off_label->setText("OFF");

lyric sail
#
    updateStyle();                                         // Here, the function uses `on_off_label` uninitialized

    setStyleSheet("QPushButton { background: none; border: none; }"
                  "QPushButton:hovered { background: none; border: none; }");

    connect(this, &QPushButton::toggled, this, &Switch::updateStyle);
    on_off_label = new QLabel("OFF", this);                // `on_off_label` initialized here
shadow parrot
#

oh

#

should it be after?

#

or better to delete it

lyric sail
#

The initialization should come first before the use, yes.

shadow parrot
#

but still error occurs in with setText

#

when I delete one of on_off_label->setText() lines program runs normally

lyric sail
#

Did you move the initialization?

shadow parrot
#

to where?

lyric sail
#

Before its use

shadow parrot
lyric sail
#

If you're using the pointer on_off_label before initialization, that's Undefined Behaviour.

#

No, that's the initialization:

on_off_label = new QLabel("OFF", this);
shadow parrot
lyric sail
#

It's just the declaration in the header.

shadow parrot
lyric sail
#

Just put the last line before the first

shadow parrot
lyric sail
#

Do you understand what a pointer is?

shadow parrot
#

you said to move cpp on_off_label->setStyleSheet("color: #efefef"); before its initialization

#

so it should crash

#

am I wrong?

lyric sail
shadow parrot
#

ok

#

THANKS

#

finally it worked

lyric sail
#

👌 Btw, these are not slots and should be protected: instead:

    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
forest zephyrBOT
#

@shadow parrot Has your question been resolved? If so, type !solved :)