QMovie for each button displaying a gif in the json file
-
My function
void MainWindow::populatePatternList() { QFile file(filemanager->savePath + filemanager->folderData + filemanager->fileJsonPattern); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("PATTERNLIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); if (error.error != QJsonParseError::NoError) { qDebug() << "Failed to parse JSON" << error.errorString(); return; } QJsonObject object = doc.object(); QJsonArray value = object.find("Pattern")->toArray(); QScrollArea *scrollArea = new QScrollArea(ui->framePatternMenuContainer); scrollArea->setWidgetResizable(true); scrollArea->setStyleSheet("border: none;background:transparent;"); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); QWidget *widget = new QWidget(); scrollArea->setWidget(widget); QVBoxLayout *layout = new QVBoxLayout(widget); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->setAlignment(Qt::AlignTop); for (auto v : value) { auto movie = new QMovie(this); movie->setFileName(v.toObject().value("button_image").toString()); movie->start(); QToolButton *button = new QToolButton(); button->setText(v.toObject().value("button_text").toString()); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setFixedHeight(55); button->setMinimumWidth(262); button->setToolTip(v.toObject().value("button_tooltip").toString()); connect(movie, &QMovie::frameChanged, [=]() { button->setIcon(movie->currentPixmap()); }); button->setIconSize(QSize(200,32)); button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); button->setStyleSheet("QToolButton {" "font: 25 10pt 'Bahnschrift Light';" "color:#999e98;" "text-align:center;" "background-color: rgb(24, 24, 24);" "background:none;" "border-radius: none;" "}" "QToolButton:hover {" "background:#dcdfe3;" "border-radius: none;" "}"); button->setCursor(Qt::PointingHandCursor); connect(button, &QToolButton::clicked, [=, button_id = v.toObject().value("button_id").toInt()]() { wled->setSegmentEffect(button_id);}); layout->addWidget(button); } QGridLayout *gridLayout = new QGridLayout(ui->framePatternMenuContainer); gridLayout->addWidget(scrollArea); gridLayout->setContentsMargins(0, 0, 0, 0); }
The problem here is that Button 1 does not show its gif at all but Button 2 does what am i doing wrong?
-
My function
void MainWindow::populatePatternList() { QFile file(filemanager->savePath + filemanager->folderData + filemanager->fileJsonPattern); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("PATTERNLIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); if (error.error != QJsonParseError::NoError) { qDebug() << "Failed to parse JSON" << error.errorString(); return; } QJsonObject object = doc.object(); QJsonArray value = object.find("Pattern")->toArray(); QScrollArea *scrollArea = new QScrollArea(ui->framePatternMenuContainer); scrollArea->setWidgetResizable(true); scrollArea->setStyleSheet("border: none;background:transparent;"); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); QWidget *widget = new QWidget(); scrollArea->setWidget(widget); QVBoxLayout *layout = new QVBoxLayout(widget); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->setAlignment(Qt::AlignTop); for (auto v : value) { auto movie = new QMovie(this); movie->setFileName(v.toObject().value("button_image").toString()); movie->start(); QToolButton *button = new QToolButton(); button->setText(v.toObject().value("button_text").toString()); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setFixedHeight(55); button->setMinimumWidth(262); button->setToolTip(v.toObject().value("button_tooltip").toString()); connect(movie, &QMovie::frameChanged, [=]() { button->setIcon(movie->currentPixmap()); }); button->setIconSize(QSize(200,32)); button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); button->setStyleSheet("QToolButton {" "font: 25 10pt 'Bahnschrift Light';" "color:#999e98;" "text-align:center;" "background-color: rgb(24, 24, 24);" "background:none;" "border-radius: none;" "}" "QToolButton:hover {" "background:#dcdfe3;" "border-radius: none;" "}"); button->setCursor(Qt::PointingHandCursor); connect(button, &QToolButton::clicked, [=, button_id = v.toObject().value("button_id").toInt()]() { wled->setSegmentEffect(button_id);}); layout->addWidget(button); } QGridLayout *gridLayout = new QGridLayout(ui->framePatternMenuContainer); gridLayout->addWidget(scrollArea); gridLayout->setContentsMargins(0, 0, 0, 0); }
The problem here is that Button 1 does not show its gif at all but Button 2 does what am i doing wrong?
@Kris-Revi
What "gif"??movie->setFileName(v.toObject().value("button_image").toString());
What have you done to error check this? How do you or we know what filename/path that resolves to and whether it even exists? If it does not exist for the first but does for the second one button, would that explain the behaviour you see?
-
@Kris-Revi
What "gif"??movie->setFileName(v.toObject().value("button_image").toString());
What have you done to error check this? How do you or we know what filename/path that resolves to and whether it even exists? If it does not exist for the first but does for the second one button, would that explain the behaviour you see?
@JonB the json file looks like this
{ "Pattern": [ { "button_id": "0", "button_text": "Solid", "button_image": ":/image/patterns/FX_SOLID.gif", "button_tooltip": "Solid primary color on all LEDs" }, { "button_id": "1", "button_text": "Blink", "button_image": ":/image/patterns/FX_BLINK.gif", "button_tooltip": "Blinks between primary and secondary color" } ] }
@JonB said in QMovie for each button displaying a gif in the json file:
What have you done to error check this? How do you or we know what filename/path that resolves to and whether it even exists? If it does not exist for the first but does for the second one button, would that explain the behaviour you see?
well it is weird that Button 2 (button id 1) has the image shown but button 1 (button id 0) has not
the image exsist! i have checked and they are in the .qrc file :) -