this->setStyleSheet(“* { color: white; }“):改变界面中所有元素的文字颜色

1.改变界面中所有元素的文字颜色

这行代码是Qt框架中的一段CSS样式设置,用于改变界面中所有元素的文字颜色。具体来说:

  • this->setStyleSheet(...) 是Qt中用于设置控件样式的方法。this 指的是当前的控件或窗口对象。
  • "* { color: white; }" 是一个CSS样式字符串,这里的CSS(层叠样式表)用于定义如何显示HTML或XML(包括Qt的UI文件)元素。
    • * 是一个通配符,表示选择所有元素。
    • { color: white; } 定义了一个样式规则,即将所有选中元素(在这个例子中是所有元素)的文字颜色设置为白色。

综上所述,这行代码的作用是将当前控件(或窗口)及其内部所有子控件的文字颜色统一设置为白色。这在创建具有特定主题或风格的应用程序界面时非常有用。

2.关联知识

setStyleSheet 是 Qt 框架中用于设置窗口部件样式表的一个接口。它允许开发者使用类似 CSS(层叠样式表)的语法来定制控件的外观,包括颜色、字体、边框、背景等。以下是对 setStyleSheet 接口的详细说明:

一、接口定义

setStyleSheet 是 QWidget 类及其子类(如 QPushButtonQLabel 等)的一个成员函数。其原型如下:

void QWidget::setStyleSheet(const QString &styleSheet);
  • 参数styleSheet 是一个 QString 类型的参数,包含了定义控件外观的样式规则字符串。
  • 返回值:无返回值。

二、功能说明

  1. 样式表语法

    • 样式表使用类似 CSS 的语法,由选择器和声明组成。选择器指定哪些控件受样式规则影响,声明则指定在这些控件上应用哪些样式属性。
    • 样式属性包括颜色、字体、边框、背景等,例如 color: white;font-size: 12px;border: 1px solid black; 等。
  2. 应用范围

    • setStyleSheet 可以应用于单个控件,也可以应用于整个窗口或应用程序。
    • 当应用于单个控件时,样式表仅对该控件及其子控件生效。
    • 当应用于窗口或应用程序时,样式表对该窗口或应用程序中的所有控件生效。
  3. 层叠性

    • Qt 样式表具有层叠性,即当多个样式表规则应用于同一个控件时,后面的规则会覆盖前面的规则。
    • 控件自身的样式表优先级最高,其次是父控件的样式表,最后是应用程序级别的样式表。
  4. 动态性

    • setStyleSheet 可以在程序运行时动态地改变控件的外观。
    • 这使得开发者可以根据用户交互或应用程序状态来动态地调整控件的样式。

三、使用方法

  1. 直接设置样式表

    QPushButton *button = new QPushButton("Click Me");
    button->setStyleSheet("background-color: blue; color: white;");

    上述代码将按钮的背景颜色设置为蓝色,文字颜色设置为白色。

  2. 使用选择器

    QWidget *window = new QWidget;
    window->setStyleSheet("QPushButton { background-color: green; color: yellow; }");

    上述代码将窗口内所有 QPushButton 控件的背景颜色设置为绿色,文字颜色设置为黄色。

  3. 使用对象名称选择器

    QPushButton *button = new QPushButton("My Button");
    button->setObjectName("myButton");
    button->setStyleSheet("#myButton { background-color: red; color: white; }");

    上述代码为按钮设置了一个对象名称 myButton,并使用对象名称选择器为该按钮设置样式。

  4. 使用伪状态选择器

    QPushButton *button = new QPushButton("Hover Me");
    button->setStyleSheet(
        "QPushButton { background-color: gray; color: white; }"
        "QPushButton:hover { background-color: blue; }"
        "QPushButton:pressed { background-color: red; }"
    );

    上述代码为按钮设置了三种状态(正常、悬停、按下)的样式。

四、注意事项

  1. 性能考虑

    • 过度使用 setStyleSheet 可能会影响应用程序的性能,尤其是在处理大量控件或复杂样式时。
    • 因此,建议仅在必要时使用样式表,并尽量简化样式规则。
  2. 兼容性

    • 不同版本的 Qt 可能对样式表的支持有所不同。
    • 在使用特定样式属性时,建议查阅 Qt 的官方文档以确认其兼容性和可用性。
  3. 调试技巧

    • 在调试样式表时,可以使用 Qt Designer 或其他工具来直观地查看和编辑样式表。
    • 也可以将样式表保存在外部文件中,并在程序运行时加载该文件,以便更方便地管理和调试样式表。

总结来看,setStyleSheet 是 Qt 框架中一个强大且灵活的接口,允许开发者使用类似 CSS 的语法来定制控件的外观。通过合理使用 setStyleSheet,可以显著提升应用程序的用户体验和界面美观度。

 

void AlarmInfoDialog::InitUI() { ui->tableWidget->setColumnCount(TableColumnIndexTotal); QStringList header; header << QString("告警类型") << QString(_("告警级别")) << QString(_("告警时间")) << QString(_("告警通道名称")) << QString(_("告警设备IP")) << QString(_(" 操作")); ui->tableWidget->setHorizontalHeaderLabels(header); ui->tableWidget->verticalHeader()->setVisible(false); ui->tableWidget->setShowGrid(false); ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableWidget->horizontalHeader()->setStretchLastSection(true); ui->tableWidget->setFrameShape(QFrame::NoFrame); ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); ui->tableWidget->setFocusPolicy(Qt::NoFocus); ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView{background-color:#E9F2FC;color:#262626;line-height:14px;font-size:14px;font-family:Microsoft YaHei; }" "QHeaderView::section::horizontal{background-color:#E9F2FC;width:888px;height:48px;border:none;border-bottom:1px solid #C7D7EA;" "padding-left:32px;}" ); ui->tableWidget->setAlternatingRowColors(true);//背景色交替 ui->tableWidget->setStyleSheet(StyleSheetMaker::TableWidget()); ui->tableWidget->horizontalHeader()->setSectionResizeMode(TableColumnIndex_Channel, QHeaderView::Stretch); ui->tableWidget->setColumnWidth(TableColumnIndex_Type,250); ui->tableWidget->setColumnWidth(TableColumnIndex_Level,100); ui->tableWidget->setColumnWidth(TableColumnIndex_Time,180); ui->tableWidget->setColumnWidth(TableColumnIndex_Ip,160); ui->tableWidget->setColumnWidth(TableColumnIndex_Operate,140); ui->horizontalWidget_2->setStyleSheet("#horizontalWidget_2{background-color:#EBEEF5;}"); ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter); //label样式 QString strLabelStyle = "QLabel{width: 70px;height: 22px;font-size: 14px;font-family: MicrosoftYaHei;color: rgba(0, 0, 0, 0.65);;line-height: 22px;}"; ui->label_2->setStyleSheet(strLabelStyle); ui->label_3->setStyleSheet(strLabelStyle); ui->label_4->setStyleSheet(strLabelStyle); ui->label->setStyleSheet("QLabel{background: transparent;" "font-family: Microsoft YaHei;" "font-size: 16px;line-height: 20px;" "font-weight: normal;color:#262626;}"); //下拉框 ui->alarmType->setStyleSheet(StyleSheetMaker::ComboBox()); ui->alarmSource->setStyleSheet(StyleSheetMaker::ComboBox()); ui->groupBox->setStyleSheet("QGroupBox{border:none}"); //按钮样式 QString strBtnStyle = "QPushButton{background: #%1;font-family: Microsoft YaHei;font-size: 14px;" "font-weight: normal;line-height: 22px;color:rgba(0,0,0,0.65);" "border-radius: 2px;border-width: 1px;border-style: solid;border-color:rgba(0,0,0,0.15);}"; //当前路径 QString strAppPath = QApplication::applicationDirPath(); //搜索 ui->searchPushButton->setStyleSheet(strBtnStyle.arg("3D84EE") + "QPushButton::pressed{background: rgb(245,246,251);font-family: Microsoft YaHei;font-size: 14px;" "font-weight: normal;line-height: 22px;color:#3D84EE;" "border-radius: 2px;border-width: 1px;border-style: solid;border-color:#3D84EE;}" ); //重置 ui->resetPushButton->setStyleSheet(StyleSheetMaker::TextButton()); //关闭 ui->pushButton->setStyleSheet("QPushButton{border-image: url("+QApplication::applicationDirPath()+"/image/Icon/Close.svg);border:none;}"); //普通 QIcon normal_ico(strAppPath+ICON_ALARM_NORMAL); ui->btn_normal->setIcon(normal_ico); ui->btn_normal->setStyleSheet(strBtnStyle.arg("FFFFFF")); //重要 QIcon important_ico(strAppPath+ICON_ALARM_IMPORTANT); ui->btn_important->setIcon(important_ico); ui->btn_important->setStyleSheet( strBtnStyle.arg("FFFFFF") + "QPushButton::pressed{border-image:url("+strAppPath+BORDER_ALARM_CLICKED+");border:none;}"); //紧急 QIcon pressing_ico(strAppPath+ICON_ALARM_PRESSING); ui->btn_pressing->setIcon(pressing_ico); ui->btn_pressing->setStyleSheet(strBtnStyle.arg("FFFFFF")); //严重 QIcon serious_ico(strAppPath+ICON_ALARM_SERIOUS); ui->btn_serious->setIcon(serious_ico); ui->btn_serious->setStyleSheet(strBtnStyle.arg("FFFFFF")); //未知 QIcon unknown_ico(strAppPath+ICON_ALARM_UNKONOWN); ui->btn_unknown->setIcon(unknown_ico); ui->btn_unknown->setStyleSheet(strBtnStyle.arg("FFFFFF")); ui->all_check->setStyleSheet(StyleSheetMaker::CheckBox()); ui->pageWidget->setStyleSheet("background-color:white;"); connect(ui->searchPushButton,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnSearch); connect(ui->btn_normal,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnNormal); connect(ui->btn_important,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnImportant); connect(ui->btn_pressing,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnPressing); connect(ui->btn_serious,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnSerious); connect(ui->btn_unknown,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnUnknown); connect(ui->pushButton,&QPushButton::clicked,this,&AlarmInfoDialog::onBtnClose); connect(this,&AlarmInfoDialog::TestAlarmReceive,this,&AlarmInfoDialog::receiveAlarm); }
11-20
QWidget* MainWindow::createAlarmQueryPage() { QWidget *page = new QWidget(); // 使用垂直布局作为主布局 QVBoxLayout *mainLayout = new QVBoxLayout(page); mainLayout->setContentsMargins(5, 5, 5, 5); // 设置边距 // 标题 QLabel *titleLabel = new QLabel("报警信息>报警查询"); titleLabel->setStyleSheet("color: white; font-size: 16px; font-weight: bold;"); titleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 左对齐 mainLayout->addWidget(titleLabel); // 创建筛选条件容器 QWidget *filterWidget = new QWidget(); QGridLayout *filterLayout = new QGridLayout(filterWidget); filterLayout->setContentsMargins(0, 0, 0, 0); // 去除内边距 // 时间范围控件 QLabel *timeLabel = new QLabel("时间范围:"); timeLabel->setStyleSheet("color: white; min-width: 60px;"); dateTimeStart = new QDateTimeEdit(); dateTimeEnd = new QDateTimeEdit(); setupDateTimeEdit(dateTimeStart); setupDateTimeEdit(dateTimeEnd); // 报警等级 QLabel *levelLabel = new QLabel("报警等级:"); levelLabel->setStyleSheet("color: white; min-width: 60px;"); comboHistoryLevel = new QComboBox(); comboHistoryLevel->addItems({"", "1级", "2级", "3级"}); comboHistoryLevel->setStyleSheet("background-color: #1a2b3c; color: white;"); // 关键字 QLabel *keywordLabel = new QLabel("关键字:"); keywordLabel->setStyleSheet("color: white; min-width: 60px;"); keywordHistory = new QLineEdit(); keywordHistory->setStyleSheet("background-color: #35475a; color: white; border: 1px solid #56789a;"); // 按钮 QPushButton *btnFilter = new QPushButton("筛 选"); QPushButton *btnExport = new QPushButton("导 出"); btnFilter->setStyleSheet(buttonStyle() + "min-width: 80px;"); btnExport->setStyleSheet(buttonStyle() + "min-width: 80px;"); // 将控件添加到筛选布局 filterLayout->addWidget(timeLabel, 0, 0); filterLayout->addWidget(dateTimeStart, 0, 1); filterLayout->addWidget(new QLabel("至"), 0, 2); filterLayout->addWidget(dateTimeEnd, 0, 3); filterLayout->addWidget(levelLabel, 1, 0); filterLayout->addWidget(comboHistoryLevel, 1, 1, 1, 3); // 跨3列 filterLayout->addWidget(keywordLabel, 2, 0); filterLayout->addWidget(keywordHistory, 2, 1, 1, 3); // 跨3列 QHBoxLayout *btnLayout = new QHBoxLayout(); btnLayout->addStretch(); btnLayout->addWidget(btnFilter); btnLayout->addWidget(btnExport); filterLayout->addLayout(btnLayout, 3, 0, 1, 4); // 设置列宽比例 filterLayout->setColumnStretch(0, 1); // 标签列 filterLayout->setColumnStretch(1, 3); // 输入框列 filterLayout->setColumnStretch(2, 1); // "至"标签 filterLayout->setColumnStretch(3, 3); // 结束时间 // 表格 tableAlarmHistory = new QTableView(); setupTableView(tableAlarmHistory, modelHistory); // 将筛选条件和表格添加到主布局 mainLayout->addWidget(filterWidget); mainLayout->addWidget(tableAlarmHistory, 1); // 第二个参数是拉伸因子 // 信号连接 connect(btnFilter, &QPushButton::clicked, this, &MainWindow::onFilterHistory); connect(btnExport, &QPushButton::clicked, this, &MainWindow::onExportHistory); return page; }我想把时间控件和报警等级关键字筛选位于一行,时间控件在左边,另外两个在右边
03-14
#include "widget1.h" #include <QPushButton> #include <QLabel> #include <QMovie> #include <QVBoxLayout> #include <QHBoxLayout> #include <QMessageBox> #include <QDebug> Widget1::Widget1(QWidget *parent) : QWidget(parent) { setWindowTitle("用户登录/注册"); resize(400, 300); setWindowIcon(QIcon(":/picture/mimi1.png")); // 创建按钮 loginButton = new QPushButton("登录", this); registerButton = new QPushButton("注册", this); QString btnStyle = R"( QPushButton { background-color: pink; color: white; font-size: 16px; font-weight: bold; border-radius: 8px; padding: 10px; min-width: 100px; } QPushButton:hover { background-color: #FFB6C1; } QPushButton:pressed { background-color: white; color: black; } )"; loginButton->setStyleSheet(btnStyle); registerButton->setStyleSheet(btnStyle); // 动图 movieLabel = new QLabel(this); movieLabel->setAlignment(Qt::AlignCenter); movie = new QMovie(":/gif/mimi3.gif"); if (!movie->isValid()) { qDebug() << "❌ Failed to load GIF:" << movie->fileName(); movieLabel->setText("GIF 加载失败"); movieLabel->setStyleSheet("color: red; font-size: 14px;"); } else { qDebug() << "✅ GIF loaded:" << movie->fileName(); movieLabel->setMovie(movie); movie->start(); movieLabel->setMinimumSize(200, 150); } // 按钮布局 QHBoxLayout *buttonLayout = new QHBoxLayout(); buttonLayout->addStretch(); buttonLayout->addWidget(loginButton); buttonLayout->addSpacing(20); buttonLayout->addWidget(registerButton); buttonLayout->addStretch(); // 主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addLayout(buttonLayout); mainLayout->addSpacing(20); mainLayout->addWidget(movieLabel); mainLayout->addStretch(); // 连接信号与槽 connect(loginButton, &QPushButton::clicked, this, &Widget1::onLoginClicked); connect(registerButton, &QPushButton::clicked, this, &Widget1::onRegisterClicked); } void Widget1::onLoginClicked() { QMessageBox::information(this, "登录", "正在处理登录..."); } void Widget1::onRegisterClicked() { QMessageBox::information(this, "注册", "进入注册流程..."); } 帮我把动图换成文字
09-18
class CustomCalendarWidget : public QWidget { Q_OBJECT public: CustomCalendarWidget(QWidget *parent = nullptr) : QWidget(parent) { // 创建主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); // 创建顶部控制栏 QWidget *controlBar = new QWidget(this); QHBoxLayout *controlLayout = new QHBoxLayout(controlBar); prevButton = new QPushButton("←", this); nextButton = new QPushButton("→", this); yearCombo = new QComboBox(this); monthCombo = new QComboBox(this); // 填充年份组合框 (1940-2040) for (int year = 1940; year <= 2040; ++year) { yearCombo->addItem(QString::number(year), year); } // 填充月份组合框 for (int month = 1; month <= 12; ++month) { monthCombo->addItem(QString::number(month), month); } controlLayout->addWidget(prevButton); controlLayout->addWidget(nextButton); controlLayout->addWidget(new QLabel("年份:", this)); controlLayout->addWidget(yearCombo); controlLayout->addWidget(new QLabel("月份:", this)); controlLayout->addWidget(monthCombo); controlLayout->addStretch(); // 创建日历表格 table = new QTableWidget(6, 7, this); // 6行 x 7列 table->verticalHeader()->setVisible(false); table->horizontalHeader()->setVisible(true); table->setEditTriggers(QAbstractItemView::NoEditTriggers); table->setSelectionMode(QAbstractItemView::SingleSelection); table->setSelectionBehavior(QAbstractItemView::SelectItems); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 设置表头 QStringList headers; headers << "周一" << "周二" << "周三" << "周四" << "周五" << "周六" << "周日"; table->setHorizontalHeaderLabels(headers); // 设置表格样式 table->setStyleSheet( "QTableWidget {" " background-color: white;" " gridline-color: #e0e0e0;" "height:700px;" "}" "QHeaderView::section {" " background-color: #f0f0f0;" " padding: 4px;" " border: 1px solid #d0d0d0;" " font-weight: bold;" "}" ); mainLayout->addWidget(controlBar); mainLayout->addWidget(table); // 设置初始日期为当前日期 QDate currentDate = QDate::currentDate(); setDate(currentDate.year(), currentDate.month()); // 连接信号 connect(prevButton, &QPushButton::clicked, this, &CustomCalendarWidget::prevMonth); connect(nextButton, &QPushButton::clicked, this, &CustomCalendarWidget::nextMonth); connect(yearCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CustomCalendarWidget::updateCalendar); connect(monthCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CustomCalendarWidget::updateCalendar); connect(table, &QTableWidget::cellClicked, this, &CustomCalendarWidget::handleCellClick); } 单元格高度不够
07-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值