Dynamically created buttons and connecting signal to slot
-
@Kris-Revi said in Dynamically created buttons and connecting signal to slot:
connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(filename)));
To the best of my knowledge you need the parameter type not name in these macros?
connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(QString)));However, if you would please move over to the new style signal/slot syntax instead of these macros your QoL (& ours) would be better!
Even when you have done that, you will find you can't do it like you're trying. You are going to need to use a C++ lambda here for the slot so as to pass the
filenameparameter from the local variable, because theclicked()signal does not pass such a string. Start from e.g.
https://forum.qt.io/topic/96163/lambda-that-uses-signal-argument-to-connect-to-a-slot
https://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/amp/
https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f -
@JonB ooh noo... lambda in C++ is a pain compared to python version :|
btw i'll switch to the new
connect()@Kris-Revi
I have just appended some reference links for the lambda stuff to my previous post. -
@Kris-Revi
I have just appended some reference links for the lambda stuff to my previous post.@JonB so i got it working
connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });having 1 problem tho
void MainWindow::selectedImageDisplay(QString img) { imageObject = new QImage(); // Make a new imageObject imageObject->load(img); // Load the image from path QJsonArray RGB565; for(int y = 0; y < imageObject->height(); y++) { const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y)); for(int x = 0; x < imageObject->width(); x++) RGB565 << *(line++); } socket.sendCommandStrip(QString("pixArt"), RGB565); }when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S
-
@JonB so i got it working
connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });having 1 problem tho
void MainWindow::selectedImageDisplay(QString img) { imageObject = new QImage(); // Make a new imageObject imageObject->load(img); // Load the image from path QJsonArray RGB565; for(int y = 0; y < imageObject->height(); y++) { const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y)); for(int x = 0; x < imageObject->width(); x++) RGB565 << *(line++); } socket.sendCommandStrip(QString("pixArt"), RGB565); }when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S
@Kris-Revi
I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.
[P.S. You had a
QSignalMapperearlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.] -
@Kris-Revi
I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.
[P.S. You had a
QSignalMapperearlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.] -
@Kris-Revi
I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.
[P.S. You had a
QSignalMapperearlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.]@JonB i fixed it! so i mentioned that the colors was off and pixel order so i thought why not just try
*imageObject = imageObject->convertToFormat(QImage::Format_RGB16);and that was the problem :) even if i saved the image as RGB16 upen uploading it and converting it it was not saved as RGB16 apparently
-
@JonB so i got it working
connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });having 1 problem tho
void MainWindow::selectedImageDisplay(QString img) { imageObject = new QImage(); // Make a new imageObject imageObject->load(img); // Load the image from path QJsonArray RGB565; for(int y = 0; y < imageObject->height(); y++) { const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y)); for(int x = 0; x < imageObject->width(); x++) RGB565 << *(line++); } socket.sendCommandStrip(QString("pixArt"), RGB565); }when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S
@Kris-Revi said in Dynamically created buttons and connecting signal to slot:
imageObject = new QImage();
You leak this image object. Better create it on the stack.
-
@Kris-Revi said in Dynamically created buttons and connecting signal to slot:
imageObject = new QImage();
You leak this image object. Better create it on the stack.
@Christian-Ehrlicher leak? stack what now?
-
@Christian-Ehrlicher leak? stack what now?
@Kris-Revi
"leak" means the memory used by yournew QImage()is never returned to the pool. Eitherdeleteit at the end, or @Christian-Ehrlicher is suggesting easiest is make it aQImage imageObject;variable here, which means it is on the "stack" (instead of the "heap" fornewed objects). Then it gets thrown away at end of the method for sure. -
@Kris-Revi
"leak" means the memory used by yournew QImage()is never returned to the pool. Eitherdeleteit at the end, or @Christian-Ehrlicher is suggesting easiest is make it aQImage imageObject;variable here, which means it is on the "stack" (instead of the "heap" fornewed objects). Then it gets thrown away at end of the method for sure. -
@Kris-Revi
Yes exactly.