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. How to set a Background colour for Disabled Pushbuttons
Forum Updated to NodeBB v4.3 + New Features

How to set a Background colour for Disabled Pushbuttons

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 5.7k Views
  • 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.
  • sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by sankarapandiyan
    #1

    Actually i have Accessing all buttons in the same slot ,by using list. here i am able to disable the clicked button. And i want to set color for disabled buttons ,

    and this is my code ,

    void MainWindow::ButtonClicked()
    {
    
        QPushButton *Button = qobject_cast<QPushButton *> ( sender());
        if (Button) {
            QString Text = Button->text();
            qDebug() << "check" << Text;
            FinalFile.append(Text);    // keep for later till we save ok ?
            Button->setEnabled(false); //
          Button->setStyleSheet(":enabled { color: " + foreground 
                                 + "; background-color: " + color + "}"); // i dont know this one is correct or not 
                    }
    }
    
    
    

    Say me some suggestions to set a color for disable buttons
    thanks in advance ..

    J.HilkJ 1 Reply Last reply
    0
    • sankarapandiyanS sankarapandiyan

      Actually i have Accessing all buttons in the same slot ,by using list. here i am able to disable the clicked button. And i want to set color for disabled buttons ,

      and this is my code ,

      void MainWindow::ButtonClicked()
      {
      
          QPushButton *Button = qobject_cast<QPushButton *> ( sender());
          if (Button) {
              QString Text = Button->text();
              qDebug() << "check" << Text;
              FinalFile.append(Text);    // keep for later till we save ok ?
              Button->setEnabled(false); //
            Button->setStyleSheet(":enabled { color: " + foreground 
                                   + "; background-color: " + color + "}"); // i dont know this one is correct or not 
                      }
      }
      
      
      

      Say me some suggestions to set a color for disable buttons
      thanks in advance ..

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @sankarapandiyan
      take a look at this stylesheet example from the docu
      https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton

      What is not listed, in the QPushButton section, but also applies to a QPushButton is the disabled identifier e.g

      QPushButton:disabled {
          background-color: black;
      }
      

      example

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QPushButton btn;
          btn.resize(150,40);
          btn.show();
      
          btn.setStyleSheet(QString("QPushButton:pressed{background-color: green;}"
                                    "QPushButton{background-color: red;}"
                                    "QPushButton:disabled{background-color:black;}"));
          QObject::connect(&btn, &QPushButton::clicked, [&btn]()->void{btn.setDisabled(true);});
      
          return a.exec();
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3

      • Login

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