#Qt - double label->setText(); crashes app
49 messages · Page 1 of 1 (latest)
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.
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
```cpp
int main() {}
```
int main() {}
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;
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
Source file?
cpp?
yes
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
}
In the constructor, you're calling updateStyle(); before the line
on_off_label = new QLabel("OFF", this);
?
the problem is in on_off_label->setText("ON"); line
or on_off_label->setText("OFF");
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
The initialization should come first before the use, yes.
but still error occurs in with setText
when I delete one of on_off_label->setText() lines program runs normally
Did you move the initialization?
to where?
Before its use
but i initializatize on_off_label in header file
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);
If I comment on_off_label->setText("OFF"); it works fine so I dont thisk that's the point
It's just the declaration in the header.
my bad
I'm pretty sure Switch::Switch must run before other Switch class functions
This is your constructor ^
Just put the last line before the first
and it should crash right?
Do you understand what a pointer is?
you said to move cpp on_off_label->setStyleSheet("color: #efefef"); before its initialization
so it should crash
am I wrong?
From MY code
👌 Btw, these are not slots and should be protected: instead:
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
@shadow parrot Has your question been resolved? If so, type !solved :)
ok thx
!solved