Is there a way to prevent Qt's setStyleSheet function from automatically reverting back to default colors on my form and buttons?

In my program, I have a total of 152 QPushButtons, with each button representing an item and having a color that indicates its status. The issue I'm facing is that when the code below colors a specific button based on user input, it also resets all other styles on my form, including buttons that were previously colored by the code. How can I prevent this from happening?

Below is the simplified version of the code:

QString input = QString(ui -> lineEdit -> text());
ui->lineEdit->clear();
int number = input.toInt();

if(status[number] == 1)
{
QString styleString = QString("#shelf"+input+"{background-color: rgb(0, 150, 255);}"); 
this->setStyleSheet(styleString);
}
else if(status[number] == 2)
{
QString styleString = QString("#shelf"+input+"{background-color: rgb(255, 0, 0);}"); 
this->setStyleSheet(styleString);
}
else if(status[number] == 3)
{
QString styleString = QString("#shelf"+input+"{background-color: rgb(0, 255, 0);}"); 
this->setStyleSheet(styleString);
}

Answer №1

It is recommended to assign the stylesheet directly to the specific button that requires customization, rather than applying the style to its parent form:

QString targetButton = QString("shelf%1").arg(input);
QPushButton* buttonToStyle = this->findChild<QPushButton*>(targetButton);
buttonToStyle->setStylesSheet(customStyle)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am encountering an issue while running npm run webpack in production mode where the CSS file is not being generated separately in the dist folder as expected. The file is not appearing as it should

Here is the code snippet from my webpack configuration file: const currentTask = process.env.nmp_lifecycle_event const path = require("path") const MiniCssExtractPlugin = require('mini-css-extract-plugin') const config = { entry: './ ...

Experience the sleek transparency when black hues grace the interface on iOS 14 and above

When implementing the code snippet below, .packages > .ulWrapper::after { background: linear-gradient( to left, var(--edge-color) 5%, rgba(0, 0, 0, 0) 95% ); /* option 1 - red */ background: linear-gradient( to left, var(--edg ...

The operation of ReleaseSemaphore does not result in the release of the semaphore

(To summarize: the main function's WaitForSingleObject is getting stuck in the provided program). I am attempting to write a piece of code that creates threads and waits for them to finish before proceeding. Instead of creating new threads each time, ...

Centering a div vertically within another div

Can you help me insert the flight duration in the middle-left section of each blue line? I've tried some methods but haven't been successful so far. This is the current layout: https://i.sstatic.net/edOfg.png Example on CodePen HTML structure ...

Using CSS to automatically adjust the width of a child div based on its content, rather than stretching it to fill the entire

I'm currently facing an issue with my CSS. Apologies for the vague title, but I'll do my best to explain it here. My problem lies within a parent wrapper div that is centered on the page. Within this wrapper, there is another child div containing ...

Tips for determining what elements are being updated in terms of style

I need assistance with modifying the functionality of a webpage's dropdown menu. Currently, it displays when the mouse hovers over it, but I want to change it to appear on click using my own JavaScript. Despite setting the onmouseout and onmouseover e ...

Is it true that a vector duplicates every object on a push_back()?

Upon observing that each time an object is push_back() into a vector, all existing objects in the vector appear to be "re-created", I decided to conduct a test program. This program involves pushing several instances of MyClass into a vector and then print ...

Real estate for a targeted audience

1. Overview I have a list of selectors that should always apply certain properties. Some selectors require additional properties to be added. I'm struggling to find a way to achieve this without repeating code. 2. Minimal CSS Example (MCVE) 2.1 ...

Solving the Size Problem with Bootstrap 5 Off Canvas

I am utilizing the bootstrap 5 off-canvas feature for collapsing, while also having an announcement bar that appears as a dismissible alert on top of my navbar. The issue I'm facing is that the top size of my off-canvas is not adjusting properly whe ...

What could be causing my Styled Components not to display the :before and :after pseudo elements?

When attempting to use the content property in a :before or :after pseudo element, I am encountering a rendering issue. Here is a simplified version of the component: import React from 'react'; import PropTypes from 'prop-types'; import ...

Replace the pixel measurements with percentage values

I have a vision for creating a dynamic full-width slider using a flex-wrapper and multiple child sections. Each section spans the width of the viewport and is aligned in a row. The goal is to move the flex-wrapper by 100% margin-left every time a button ...

Is it necessary for a click handler to be triggered when clicking on a scrollbar?

Check out these HTML snippets: Jsfiddle <style> div { margin:20px; border: 30px red solid; padding: 20px; background-color:green; overflow-y:scroll; } </style> <div onclick="alert('div clicked');"> ...

Looking to create an anchor tag that navigates to a specific ID on the page while accommodating a fixed header placement

In my application, the homepage's carousel displays multiple images with dynamically generated anchor tags that link to different routes. When clicking on the anchor tag, the page scrolls to the linked image but is obstructed by a fixed header. I want ...

Is there a way to load and play different sounds on multiple audio players based on the length of an array?

I am attempting to load various sounds (.mp3 audio) on separate audio players that are displayed on a single HTML page. The number of players displayed on the screen is determined by the length of the array. In this specific example, I have 3 elements in t ...

The system cannot locate the program "g++" within the PATH directories

Having trouble adding OpenCV native C to my project. Initially, the error was "Program 'g++' not found in PATH" and "Program 'gcc' not found in PATH". When I attempted to add native code by right-clicking on my project, then going to An ...

Can anyone suggest a way to change the orientation of mapped items from column to row?

In my React app, I am creating a keyboard using the following component: Keypad.js const Keypad = () => { const letters = [ 'Q', 'W', 'E', 'R', 'T', ...

Fiddle demonstrates HTML functionality, while local testing is unsuccessful

Currently, I am in the process of creating an image slider and attempting to run it on my personal computer. However, upon doing so, I have encountered a problem where the page is not rendering correctly. Additionally, I receive an ActiveX warning message ...

Tips for customizing the focus style of a Text Field in Material-UI

My current Search box design is represented in the following image: https://i.sstatic.net/VuKIp.png I am looking for ways to customize the background color, border, and icon of the search box when it is focused. Additionally, I would like to know how to m ...

Troubleshooting a hover problem in CSS navigation bar

Having some trouble with the CSS menu on my website click here. I've tried adjusting the code, but can't seem to get all the menu levels to line up properly. As a beginner in CSS, I'm sure I'm overlooking something simple. When hovering ...

Ending the Overlay

I am using an overlay: <div id="overlayer" class="overlayer"> <div id="board" class="board"></div> </div> Here are the CSS properties I have applied to it: #overlayer { position:fixed; display:none; top:0; left:0; width:100%; hei ...