Qt笔记_8

来源:互联网 发布:大学生生活费的数据 编辑:程序博客网 时间:2024/05/16 11:54

界面

界面的风格

  • 调用QApplication的setStyle()函数指定窗口风格。
    添加头文件QStyleFactory
ui->progressBar->setStyle(QStyleFactory::create("windows"));//设置windows风格
  • QPalette类包含了部件各种状态的颜色组。Qt的所有部件都包含一个调色板,
    头文件QPalette
    // 获取pushButton的调色板    QPalette palette1 = ui->pushButton->palette();    // 设置按钮文本颜色为红色    palette1.setColor(QPalette::ButtonText, Qt::red);    // 设置按钮背景色为绿色    palette1.setColor(QPalette::Button, Qt::green);    // pushButton使用修改后的调色板    ui->pushButton->setPalette(palette1);    // 设置spinBox不可用    ui->spinBox->setDisabled(true);    QPalette palette2 = ui->spinBox->palette();    // 设置spinBox不可用时的背景颜色为蓝色    palette2.setColor(QPalette::Disabled,QPalette::Base,Qt::blue);    ui->spinBox->setPalette(palette2);

或者直接设置palette属性,效果是一样的。


式样表

使用代码设置
// 设置pushButton的背景为黄色ui->pushButton->setStyleSheet("background:yellow");// 设置horizontalSlider的背景为蓝色ui->horizontalSlider->setStyleSheet("background:blue");

或者

setStyleSheet("QPushButton{background:yellow}QSlider{background:blue}");
在设计模式中设置

右键->改变样式表

例:
写上
QPushButton{//光标停在这里!在选择式样
}
QPushButton是选择器,{ …… }是声明。
**ID选择器 如:QPushButton#okButton 匹配所有QPushButton中以okButton为对象名的实例。

原创粉丝点击