我有無法更改按鈕背景的問題。只改變了邊框。我認為某些屬性阻止了這一點。但我認為我在這一點上是錯誤的。我已經瀏覽了 Qt 檔案,但找不到任何東西。我只能在互聯網上找到給我相同結果的例子。有沒有辦法改變背景?
這是代碼:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->pushButton->setAttribute(Qt::WA_SetPalette);
//ui->pushButton->setAttribute(Qt::WA_SetStyle);
ui->pushButton->setAutoFillBackground(true);
ui->pushButton->setAttribute(Qt::WA_ShowModal);
QPalette pal = ui->pushButton->palette();
//pal.setColor(QPalette::Base, Qt::cyan);
//pal.setBrush(QPalette::Base, Qt::cyan);
pal.setColor(QPalette::Button, Qt::cyan);
pal.setBrush(QPalette::Button, Qt::cyan);
ui->pushButton->setPalette(pal);
ui->pushButton->update();
//ui->pushButton->setAttribute(Qt::WA_NoSystemBackground, true);
// setAutoFillBackground(true);
// QPalette pal2 = palette();
// pal2.setBrush(QPalette::Button, Qt::cyan);
// pal2.setColor(QPalette::ButtonText, Qt::cyan);
// QApplication::setPalette(pal2);
}

uj5u.com熱心網友回復:
這是我用來設定 a QPushButton(或 any QAbstractButton)背景顏色的代碼:
// @param btn button to change colors of
// @param bc new background color for button
// @param optTextColor if non-NULL, the new color to use for the
// button's text label. If NULL, this function
// will choose either black or white (whichever
// it thinks will be more readable)
void SetButtonColor(QAbstractButton * btn, const QColor & bc, const QColor * optTextColor = NULL)
{
QPalette p = btn->QWidget::palette();
p.setColor(QPalette::Button, bc);
const QColor fc = GetContrastingTextColor(bc);
p.setColor(QPalette::Active, QPalette::ButtonText, optTextColor?*optTextColor:fc);
p.setColor(QPalette::Inactive, QPalette::ButtonText, optTextColor?*optTextColor:fc);
p.setColor(QPalette::Disabled, QPalette::ButtonText, optTextColor?*optTextColor:MixColors(fc, Qt::lightGray, 0.5f));
btn->setPalette(p);
}
// Returns either Qt::white or Qt::black, whichever will make for more readable text
// in front of the passed-in background color
QColor GetContrastingTextColor(const QColor & c)
{
const int darkThresh = 64;
const int loneDelta = 129;
const int loneRed = ((c.green()<darkThresh)&&(c.blue() <darkThresh)) ? loneDelta : 0;
const int loneGreen = 0;
const int loneBlue = ((c.red() <darkThresh)&&(c.green()<darkThresh)) ? loneDelta : 0;
return (std::max(c.red()-loneRed, std::max(c.green()-loneGreen, c.blue()-loneBlue)) >= 128) ? Qt::black : Qt::white;
}
// Returns a weighted average value between (v1) and (v2)
static int Mix(int v1, const int v2, float p)
{
return (int) ((((float)v2)*p) (((float)v1)*(1.0f-p)));
}
// Returns a weighted average value between (c1) and (c2)
QColor MixColors(const QColor & c1, const QColor & c2, float p)
{
return QColor(Mix(c1.red(), c2.red(), p), Mix(c1.green(), c2.green(), p), Mix(c1.blue(), c2.blue(), p));
}
uj5u.com熱心網友回復:
當我測驗您的代碼時,這是我的結果:

但是您可以通過使用StyleSheet而不是QPalette來更改它。
ui->pushButton->setStyleSheet(QString::fromUtf8("background-color: cyan;"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/509854.html
標籤:C qt调色板
