Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Button loses styling!
Qt 6.11 is out! See what's new in the release blog

Button loses styling!

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 723 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by Kris Revi
    #1

    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;
    }
    
    SGaistS S 2 Replies Last reply
    0
    • K Offline
      K Offline
      Kris Revi
      wrote on last edited by
      #5

      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; }");
      
      1 Reply Last reply
      2
      • K Kris Revi

        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;
        }
        
        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You only see the text aligned to the left on these buttons ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        0
        • K Kris Revi

          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;
          }
          
          S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #3

          @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.

          1. Try to check if it works if you comment out this line.
          2. Check if button->styleSheet() returns any content.
          3. If styleSheet() returns content, then add your "text-align: left;" to that string and use the combined string to set the stylesheet.
          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You only see the text aligned to the left on these buttons ?

            K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #4

            @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.

            1. Try to check if it works if you comment out this line.
            2. Check if button->styleSheet() returns any content.
            3. 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!

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #5

              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; }");
              
              1 Reply Last reply
              2
              • K Kris Revi has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved