Colouring a button in Qt - qt

I'm trying to design a simple button (QPushButton or QToolButton, either can work) that would essentially be a rectangle of the colour it represents. Clicking it opens a QColorDialog, and selecting a colour in it repaints the button.
So, basically, something that will look like one of these:
I made a few attempts, none of which brought me the functionality I wanted.
Slot:
void MainWindow::OnButtonColorClick()
{
QColor initialColor = ui->buttonColor->palette().color(QPalette::Background);
QColor colorSelected = QColorDialog::getColor(initialColor, this);
if(colorSelected.isValid())
{
ui->buttonColor->setPalette(QPalette(colorSelected));
ui->buttonColor->setAutoFillBackground(true);
}
}
Attempt #1:
Set the Palette in the constructor:
ui->buttonCoulor->setPalette(QPalette(Qt::black));
Result: ordinary button before click, thin coloured contour after selection.
Attempt #2:
Add stylesheet:
background-color: rgb(0, 0, 0);
Result: black rectangle before click, black rectangle after selection.
I feel like I'm circling the drain. Essentially, how do I achieve:
?

Here is one way to accomplish the desired effect:
// Slot for the button
void MainWindow::on_button()
{
QColor color = QColorDialog::getColor();
QString s("background: #"
+ QString(color.red() < 16? "0" : "") + QString::number(color.red(),16)
+ QString(color.green() < 16? "0" : "") + QString::number(color.green(),16)
+ QString(color.blue() < 16? "0" : "") + QString::number(color.blue(),16) + ";");
button->setStyleSheet(s);
button->update();
}
Hope that helps.

use setStyleSheet with this style sheet:
border: 1px solid black;
background-color: #XXXXXX;
where XXXXXX is the value returned by QString::number(myColor.rgb(), 16).toUpper();
No need to set any other properties on the button. Leave them all as default and this will work.

This seems to be working just fine for me:
if(colorSelected.isValid())
{
ui->buttonColor->setPalette(QPalette(colorSelected));
}

As a follow-up to phyatt's answer, there's a shortcut to generate the XML-formatted color string:
QString s("background: " + color.name + ";");
button->setStyleSheet(s);

Related

Set a button of custom color to a disabled style

I have a class that inherits QWidget. Under certain circumstances, I wish to set it disabled.
The widget has some color buttons, that are set from a list of colors:
void MyWidget::colorUpdate(QString color)
{
if(!color.isEmpty())
{
QString foreground = (QColor(color).lightness() < 125 ? "white" : "black");
m_colorButton->setStyleSheet("color: " + foreground + "; background-color: " + color);
}
}
Calling myWidget.setEnabled(enabledOption); disables the widget, grays out all text and every other items in the widget - except for these color buttons.
So I am thinking of making my own action:
void MyWidget::setWidgetEnabled(bool enabled)
{
this->setEnabled(enabled);
// what else ?
}
How can I make my buttons - of a background color and text color that I don't know, but the button does - have that "disabled look" ?
(Note - the color update works on disabled items too - that is not really complicated though - whatever style sheet I apply on setting widget disabled can be applied in the colorUpdate function).
I just don't know how to apply a stylesheet with that gray shade - or possibly have a "disabled" option in the stylesheet even...
What do colors look like in "disabled" ?
To set your own style for the disable state.
You can set a special style for disabled state in the stylesheet :
m_colorButton->setStyleSheet(":enabled { color: " + foreground
+ "; background-color: " + color
+ " } :disabled { color: " + disabledForeground
+ "; background-color: " + disabledColor + " }");
edit: changed code for the widget instead of a global stylesheet.
To keep the default disabled style. You can set your custom style only for the enabled state, then when the widget is disabled the style does not apply :
m_colorButton->setStyleSheet(":enabled { color: " + foreground
+ "; background-color: " + color + "}");

Setting Background of TextArea

I would like to provide for user possiblity to select color of TextArea:
private void updateTextArea(){
textArea.setStyle("-fx-text-fill: #" + textColor + "; -fx-background-color: #" + backgroundColor);
}
however this doesnt change color of whole background. Ive found on the Internet that to change backgroud of text Area I need to do something like this in external CSS file.
.text-area .content {
-fx-background-color: black ;
}
how Can I do this with setStyle()?
You can do this by fetching the content node out of the TextArea and applying the style to it. But it works only after the TextArea is shown on the stage.
Usage :
Node node = textArea.lookup(".content");
node.setStyle("-fx-background-color: black;");

Qt: change color part of text

This is my code:
QTextCursor cursor = ui->editor->textCursor(); // editor is QTextEdit
cursor.select(QTextCursor::WordUnderCursor);
QString c = cursor.selectedText();
if (c == keywords[i])
{
cursor.removeSelectedText();
cursor.insertHtml("<font color=\"DeepPink\">" + keywords[i] + "</font>");
}
So, if the keyword is "new", this word is colored pink. The problem is that everything that is inserted after "new" is also colored red, and not the standard black.
Anybody? :)
UPDATE:
Stupid me. Just added ui->editor->setTextColor("#000000");
ui->editor->setTextColor("#000000");

How do you remove the border from a QPushButton?

I have some QPushButtons in the rows of a QTreeView, and they're showing up with these black borders around them that I can't seem to modify. Currently I can grey out the buttons with this code:
for (int i = 0; i < QPalette::NColorRoles; i++){
QPalette::ColorRole thisRole = static_cast<QPalette::ColorRole>(i);
QColor newColor = commitPalette.color(QPalette::Disabled,thisRole);
int grayColor = qGray(newColor.rgb());
newColor.setRgb(grayColor,grayColor,grayColor,50);
commitPalette.setColor(QPalette::Disabled, thisRole, newColor);
}
But it doesn't do anything to the border. I'd prefer to avoid using stylesheets, as I like the automatic color generation provided by QPalette's constructor
If you are using Qt creator, right click the QPushButton and setStyleSheet as border: none; Thats it.
If you set the QButton property isFlat = true it should disable the border unless it's being clicked.
I suggest using a stylesheet. From the code you can make it to a function:
void setFlatStyle(QPushButton *btn)
{
btn->setStyleSheet(QString("QPushButton {border: 0px;}"));
}
Just pass the button in there and get your result.
button.setStyleSheet("QPushButton { border: none; }")
As said #RajaRaviVarma

How to make cxgrid row color transparent

I'm changing grid's row color to red according to my needs and when it gets colored the text is invisible, under backroung color...
How to change the text color or make it transparent ???
void __fastcall TfRegular::tvFillOutCustomDrawCell(TcxCustomGridTableView *Sender,
TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
bool &ADone)
{
TRect ARec;
ARec=AViewInfo->Bounds;
//AViewInfo->GridRecord->DisplayTexts[7]="+" + AViewInfo->GridRecord->DisplayTexts[7];
// ShowMessage(AViewInfo->GridRecord->Values[13]);
if (AViewInfo->GridRecord->Values[13]>1) {
ACanvas->Canvas->Brush->Color = clRed;
ACanvas->Canvas->Font->Color = clBlack;
ACanvas->Canvas->Pen->Color = clRed;
ACanvas->Canvas->FillRect(ARec);
SetBkMode(ACanvas->Canvas->Handle,TransparentColor);
//InflateRect(AViewInfo->Bounds,2,2);
//ACanvas->DrawTexT("",AViewInfo->Bounds,0);
ADone=true;
}
}
It was really easy than I thought.
I solved this problem using OnGetContentStyle event.

Resources