如何在QT使用代码样式标签
How to style a label in QT using code
似乎最基本的东西都会绊倒我。
我试图在QT中设置标签的背景颜色。我知道我可以使用样式表通过右键单击它并添加:background-color: blue;
或其他东西来做到这一点。这很有效。
但是如果没有GUI视图,我怎么能做到这一点呢?
我知道我可以添加
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setStyleSheet("background-color: blue }");
}
在mainwindow.cpp中改变主窗口背景颜色,但是我如何用对象名称TestLabel来定位标签,以及在哪里放置代码?
I tried
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setStyleSheet("QLabel:TestLabel { background-color: blue }");
}
但是这打乱了我的程序。它可以编译,但不能按预期执行。
您可以使用ID选择器语法:
setStyleSheet("QLabel#TestLabel { background-color: blue }");
这将针对对象名称为TestLabel
的特定QLabel
。
标签也可以使用setStyleSheet():
ui->TestLabel->setStyleSheet("background-color: blue");
相关文章:
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 代码样式:在 switch/if 语句的分支中重用控件表达式或控制变量
- C++代码::块大括号样式
- 禁用 Qt5 代码中的旧(Qt4 样式)信号槽语法
- 此代码中的数组初始化样式是什么?这是标准的吗?
- 此代码如何启动索引为 1 的 C 样式字符串
- 需要WXLISTCTRL的代码更改为虚拟样式WXListCtrl的工作代码
- 为什么两个相同的代码给出不同的输出,而它们之间的唯一区别是不同的变量名称和写作样式
- 从旧的 C 样式指针移动到C++智能指针,代码几乎没有变化
- 设置精度应用于旧的 C 样式代码
- 如何使用 for(int i:myVector) 代码样式知道元素是否是矢量中的最后一个元素
- C++模板样式代码
- 如何从代码中删除C样式注释
- C++:模板实现(代码样式)
- 通用配置代码的 Cmake 和编码样式
- C 样式转换是添加程序集(代码)还是仅供编译器了解情况
- 在 C++ 中链接"C"样式代码
- 带有返回的If语句代码样式
- 如何在QT使用代码样式标签
- 用于检测c++代码样式问题的工具