Button loses styling!
-
So this is my code
void MainWindow::populateDeviceList(bool bUpdate) { scrollAreaStrip = new QScrollArea(ui->LedStripDevicesContent); scrollAreaStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); scrollAreaStrip->setWidgetResizable(true); scrollAreaStrip->setStyleSheet("border: none;background:transparent;"); scrollAreaStrip->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); scrollAreaStrip->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); widgetStrip = new QWidget(); widgetStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); widgetStrip->setFixedWidth(250); scrollAreaStrip->setWidget(widgetStrip); layoutStrip = new QVBoxLayout(widgetStrip); layoutStrip->setContentsMargins(0, 0, 0, 0); layoutStrip->setSpacing(0); layoutStrip->setAlignment(Qt::AlignTop); gridLayoutStrip = new QGridLayout(ui->LedStripDevicesContent); gridLayoutStrip->addWidget(scrollAreaStrip); gridLayoutStrip->setContentsMargins(0, 0, 0, 0); QFile file(filemanager->savePath + filemanager->folderData + filemanager->fileJsonDevice); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("DEVICELIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); QJsonObject object = doc.object(); QJsonArray value = object.find("Device")->toArray(); int cc=0; for (auto v : value) { QPushButton *button = new QPushButton; QMetaEnum metaEnum; metaEnum = QMetaEnum::fromType<Fa5::Glyph>(); QString string = v.toObject().value("Icon").toString(); std::string str = string.toStdString(); const char* p = str.c_str(); button->setObjectName("DeviceButton" + QString::number(cc++)); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setStyleSheet("text-align: left;"); button->setIcon(ZFontIcon::icon(Fa5::FAMILY, Fa5::SOLID, metaEnum.keyToValue(p), QColor(255, 255, 255), 0.50)); button->setIconSize(QSize(32, 32)); button->setCursor(Qt::PointingHandCursor); button->setCheckable(true); button->setAutoExclusive(true); button->setText(v.toObject().value("Name").toString()); button->setProperty( "orgtext", button->text() ); #if defined(Q_OS_ANDROID) button->setFont( QFont( QStringList("Bahnschrift Light"), 14, -1, false ) ); #elif defined(Q_OS_WIN32) button->setFont( QFont( QStringList("Bahnschrift Light"), 10, -1, false ) ); #endif deviceButtonList.append(button); connect(button, &QPushButton::clicked, [=, ipa = v.toObject().value("IPA").toString(), name = v.toObject().value("Name").toString()]() { buttonDeviceName = button->objectName(); ui->connectedDeviceName->setText(name); qDebug() << "Button that was last clicked : " << buttonDeviceName; }); if (v.toObject().value("Type").toString() == "Strip") { layoutStrip->addWidget(button); connectedDeviceType = "Strip"; } } }
why does my buttons lose their styling!! i have a .qss file with css every other button that i have inserted via Qt Design has the stylings from the Qss... but the buttons created in code does not... and i have NO CLUE as to why....
from Qss file :
QPushButton { background-color: #2F58CD; border: none; } QPushButton:hover { background-color: #2b0c5a; border: none; }
-
omg i am stupid for not noticing this sooner...
scrollAreaMatrix->setStyleSheet("border: none;background: transparent; ");
you see it when you see it...
it needs to be
scrollAreaMatrix->setStyleSheet("QScrollArea { border: none;background: transparent; }");
-
So this is my code
void MainWindow::populateDeviceList(bool bUpdate) { scrollAreaStrip = new QScrollArea(ui->LedStripDevicesContent); scrollAreaStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); scrollAreaStrip->setWidgetResizable(true); scrollAreaStrip->setStyleSheet("border: none;background:transparent;"); scrollAreaStrip->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); scrollAreaStrip->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); widgetStrip = new QWidget(); widgetStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); widgetStrip->setFixedWidth(250); scrollAreaStrip->setWidget(widgetStrip); layoutStrip = new QVBoxLayout(widgetStrip); layoutStrip->setContentsMargins(0, 0, 0, 0); layoutStrip->setSpacing(0); layoutStrip->setAlignment(Qt::AlignTop); gridLayoutStrip = new QGridLayout(ui->LedStripDevicesContent); gridLayoutStrip->addWidget(scrollAreaStrip); gridLayoutStrip->setContentsMargins(0, 0, 0, 0); QFile file(filemanager->savePath + filemanager->folderData + filemanager->fileJsonDevice); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("DEVICELIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); QJsonObject object = doc.object(); QJsonArray value = object.find("Device")->toArray(); int cc=0; for (auto v : value) { QPushButton *button = new QPushButton; QMetaEnum metaEnum; metaEnum = QMetaEnum::fromType<Fa5::Glyph>(); QString string = v.toObject().value("Icon").toString(); std::string str = string.toStdString(); const char* p = str.c_str(); button->setObjectName("DeviceButton" + QString::number(cc++)); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setStyleSheet("text-align: left;"); button->setIcon(ZFontIcon::icon(Fa5::FAMILY, Fa5::SOLID, metaEnum.keyToValue(p), QColor(255, 255, 255), 0.50)); button->setIconSize(QSize(32, 32)); button->setCursor(Qt::PointingHandCursor); button->setCheckable(true); button->setAutoExclusive(true); button->setText(v.toObject().value("Name").toString()); button->setProperty( "orgtext", button->text() ); #if defined(Q_OS_ANDROID) button->setFont( QFont( QStringList("Bahnschrift Light"), 14, -1, false ) ); #elif defined(Q_OS_WIN32) button->setFont( QFont( QStringList("Bahnschrift Light"), 10, -1, false ) ); #endif deviceButtonList.append(button); connect(button, &QPushButton::clicked, [=, ipa = v.toObject().value("IPA").toString(), name = v.toObject().value("Name").toString()]() { buttonDeviceName = button->objectName(); ui->connectedDeviceName->setText(name); qDebug() << "Button that was last clicked : " << buttonDeviceName; }); if (v.toObject().value("Type").toString() == "Strip") { layoutStrip->addWidget(button); connectedDeviceType = "Strip"; } } }
why does my buttons lose their styling!! i have a .qss file with css every other button that i have inserted via Qt Design has the stylings from the Qss... but the buttons created in code does not... and i have NO CLUE as to why....
from Qss file :
QPushButton { background-color: #2F58CD; border: none; } QPushButton:hover { background-color: #2b0c5a; border: none; }
Hi,
You only see the text aligned to the left on these buttons ?
-
So this is my code
void MainWindow::populateDeviceList(bool bUpdate) { scrollAreaStrip = new QScrollArea(ui->LedStripDevicesContent); scrollAreaStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); scrollAreaStrip->setWidgetResizable(true); scrollAreaStrip->setStyleSheet("border: none;background:transparent;"); scrollAreaStrip->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); scrollAreaStrip->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); widgetStrip = new QWidget(); widgetStrip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); widgetStrip->setFixedWidth(250); scrollAreaStrip->setWidget(widgetStrip); layoutStrip = new QVBoxLayout(widgetStrip); layoutStrip->setContentsMargins(0, 0, 0, 0); layoutStrip->setSpacing(0); layoutStrip->setAlignment(Qt::AlignTop); gridLayoutStrip = new QGridLayout(ui->LedStripDevicesContent); gridLayoutStrip->addWidget(scrollAreaStrip); gridLayoutStrip->setContentsMargins(0, 0, 0, 0); QFile file(filemanager->savePath + filemanager->folderData + filemanager->fileJsonDevice); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("DEVICELIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); QJsonObject object = doc.object(); QJsonArray value = object.find("Device")->toArray(); int cc=0; for (auto v : value) { QPushButton *button = new QPushButton; QMetaEnum metaEnum; metaEnum = QMetaEnum::fromType<Fa5::Glyph>(); QString string = v.toObject().value("Icon").toString(); std::string str = string.toStdString(); const char* p = str.c_str(); button->setObjectName("DeviceButton" + QString::number(cc++)); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setStyleSheet("text-align: left;"); button->setIcon(ZFontIcon::icon(Fa5::FAMILY, Fa5::SOLID, metaEnum.keyToValue(p), QColor(255, 255, 255), 0.50)); button->setIconSize(QSize(32, 32)); button->setCursor(Qt::PointingHandCursor); button->setCheckable(true); button->setAutoExclusive(true); button->setText(v.toObject().value("Name").toString()); button->setProperty( "orgtext", button->text() ); #if defined(Q_OS_ANDROID) button->setFont( QFont( QStringList("Bahnschrift Light"), 14, -1, false ) ); #elif defined(Q_OS_WIN32) button->setFont( QFont( QStringList("Bahnschrift Light"), 10, -1, false ) ); #endif deviceButtonList.append(button); connect(button, &QPushButton::clicked, [=, ipa = v.toObject().value("IPA").toString(), name = v.toObject().value("Name").toString()]() { buttonDeviceName = button->objectName(); ui->connectedDeviceName->setText(name); qDebug() << "Button that was last clicked : " << buttonDeviceName; }); if (v.toObject().value("Type").toString() == "Strip") { layoutStrip->addWidget(button); connectedDeviceType = "Strip"; } } }
why does my buttons lose their styling!! i have a .qss file with css every other button that i have inserted via Qt Design has the stylings from the Qss... but the buttons created in code does not... and i have NO CLUE as to why....
from Qss file :
QPushButton { background-color: #2F58CD; border: none; } QPushButton:hover { background-color: #2b0c5a; border: none; }
@Kris-Revi said in Button loses styling!:
button->setStyleSheet("text-align: left;");
You are changing the stylesheet. I think Qt does not merge the different parts of the stylesheet automatically.
- Try to check if it works if you comment out this line.
- Check if
button->styleSheet()
returns any content. - If
styleSheet()
returns content, then add your"text-align: left;"
to that string and use the combined string to set the stylesheet.
-
@SGaist that is correct! but all other buttons (the once i have added via Qt Designer gets the styling from .qss file)
@SimonSchroeder said in Button loses styling!:
@Kris-Revi said in Button loses styling!:
button->setStyleSheet("text-align: left;");
You are changing the stylesheet. I think Qt does not merge the different parts of the stylesheet automatically.
- Try to check if it works if you comment out this line.
- Check if
button->styleSheet()
returns any content. - If
styleSheet()
returns content, then add your"text-align: left;"
to that string and use the combined string to set the stylesheet.
i have tried to comment it out! did not work!
-
omg i am stupid for not noticing this sooner...
scrollAreaMatrix->setStyleSheet("border: none;background: transparent; ");
you see it when you see it...
it needs to be
scrollAreaMatrix->setStyleSheet("QScrollArea { border: none;background: transparent; }");
-