[SOLVED] QLabel won't update pixmap from inside function
-
HI and welcome to devnet,
What would be the problem with triangle and whitex on the stack rather that the heap ? By the way you are shadowing circle in your constructor.
-
How are you calling changeImage ?
Are you callingsetUpdatesEnabled(false);
somewhere ? -
updatesEnabled is a getter that will tell you whether updates are enabled or not. They are by default.
If you don't call changeImage anywhere, how can you expect your labels to get updated with whitex ?
-
It's the
void myWidget::changeImage()
that you wrote in your first post -
Shoot, I'm sorry, my brain is all over the place today, and the function names are changed from the originals. In another class I have a connection:
connect(this, SIGNAL(changeImageBtnClicked()),[myWidget pointer], SLOT(changeImage()));
I know this connection works, because if I press the button enough times it starts printing the "end of array" repeatedly.
Again, apologies for the confusion. Thank you for your help and patience.
-
Are you doing anything else with these labels ? e.g. configuration ?
-
Just to rule out a QPixmap problem.
What happens if you do
QImage img(120, 120, QImage::Format_ARGB32); img.fill(Qt::red); pointerArray[activePointer]->setPixmap(QPixmap::fromImage(img)); activePointer++;
?
-
@SGaist Thank you for your continued help.
I tried that, no dice. When I debug, after running the line with setPixmap, I get
pixmap (120x120) QVariant (QPixmap)
as a property of the label that would be changed.I'm also noticing that in a different part of a project, the layout doesn't want to update when I run a function. The only similarity is that both of them are labels in QLayouts.
-
Can you try with the following widget:
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class QLabel; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); public slots: void onClicked(); private: QVector<QLabel*> _labels; int _currentLabel; QColor _color; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include <QString> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> Widget::Widget(QWidget *parent) : QWidget(parent) , _currentLabel(0) , _color(Qt::blue) { QHBoxLayout *buttonLayout = new QHBoxLayout; QImage img(120, 120, QImage::Format_ARGB32); img.fill(Qt::red); for (int i = 0 ; i < 4 ; ++i) { QLabel *label = new QLabel; label->setPixmap(QPixmap::fromImage(img)); buttonLayout->addWidget(label); _labels << label; } QPushButton *button = new QPushButton(tr("Test")); QVBoxLayout *layout = new QVBoxLayout(this); layout->addLayout(buttonLayout); layout->addWidget(button); connect(button, &QPushButton::clicked, this, &Widget::onClicked); } Widget::~Widget() { } void Widget::onClicked() { QImage img(120, 120, QImage::Format_ARGB32); img.fill(_color); _labels[_currentLabel]->setPixmap(QPixmap::fromImage(img)); ++_currentLabel; if (_currentLabel == _labels.count()) { _currentLabel = 0; _color = QColor(qrand() % 255, qrand() % 255, qrand() % 255); } }
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
-
Ah, that one works because it is from within the same widget. I added a push button to myWidget and it was working. Unfortunately, the button that I need to push is in a different window.
~~ EDIT: The new button on myWidget works perfectly. I thought maybe I could circumvent the problems of the changeImage button by making it a slot for "emit buttonclicked," which I connected to the new button on myWidget. It had the same problem as before, where it does nothing and prints "end of array" after 11 clicks. BUT strangely, if I press the button on myWidget, it resets the value of activePointer. I'm not sure why this is.
Essentially, I can push the changeImage button 11 times until it prints "end of array," but then I can push the myWidget pushbutton 11 times and it will change the image 10 times and start printing "end of array" after that. Curious. ~~
EDIT EDIT: It looks like right now it's only changing the value of activePointer locally. Whoops. Problem still exists, however.
-
The fact that the button is inside another widget should not have any impact here. Can you reproduce your bug using may sample as base ?
-
I'll post the full code, just to be thorough. The error is exactly the same. When I debugged, it showed that my signal had reached the slot...But there was no change to the image.
widget1.h
#ifndef WIDGET1_H #define WIDGET1_H #include <QWidget> #include "/absolute/path/to/widget.h" namespace Ui { class widget1; } class widget1 : public QWidget { Q_OBJECT Widget* widget; public: explicit widget1(QWidget *parent = 0); ~widget1(); signals: void clicked(); void buttonPush(); private slots: void on_pushButton_clicked(); private: Ui::widget1 *ui; }; #endif // WIDGET1_H
widget1.cpp
#include "widget1.h" #include "ui_widget1.h" widget1::widget1(QWidget *parent) : QWidget(parent), ui(new Ui::widget1) { ui->setupUi(this); widget = new Widget; connect(this, SIGNAL(buttonPush()),widget,SLOT(onClicked())); } widget1::~widget1() { delete ui; } void widget1::on_pushButton_clicked() { emit buttonPush(); }
An instance of widget1 was created in main, then shown. I created a ui file in designer, with all default values.
-
Is it me or are you not showing widget at all ?