如何在QT中设置样式表以选择假发背景的随机图像
How to set up stylesheet in QT to choose random image for wiget background?
因此,我正在尝试从PC的文件系统中选择随机图像,并将其在假冒中进行背景。这就是为什么我打开qfiledialog并使用它的原因。Qdebug为我提供了正确的图像途径,但仍然不起作用。
void ChatWindow::on_actionImage_triggered()
{
QString fileName = QFileDialog::getOpenFileName(
this, tr("Open file"), "/home", tr("Images(*.jpg)")
);
QString filePath(fileName);
qDebug () << filePath;
setStyleSheet(
"ChatWindow{border-image:url(:" +
filePath +
") 0 0 0 0 stretch stretch;}"
);
QGraphicsOpacityEffect * effect1 =
new QGraphicsOpacityEffect(ui->messageHistory);
effect1->setOpacity(0.8);
ui->messageHistory->setGraphicsEffect(effect1);
ui->messageHistory->setStyleSheet("background-color: white;");
QGraphicsOpacityEffect * effect2 =
new QGraphicsOpacityEffect(ui->roomTree);
effect2->setOpacity(0.8);
ui->roomTree->setGraphicsEffect(effect2);
ui->roomTree->setStyleSheet("background-color: white;");
QGraphicsOpacityEffect * effect3 =
new QGraphicsOpacityEffect(ui->messageInput);
effect3->setOpacity(0.8);
ui->messageInput->setGraphicsEffect(effect3);
ui->sendButton->setStyleSheet("background-color: none;");
}
我已经看到这个无法在QT Stylesheet中设置与我的问题有关的背景图像,但是就我而言,它行不通。
根据文档,没有这样的属性称为 border-image
:您要搜索的是 background-image
。
此外,由于您还指定其他背景的参数,因此应使用background
。
此外,由于您的文件在磁盘上而不是在资源上,因此URL不应以:
开头:键入url(" + filePath + ")
而不是url(:" + filePath + ")
。
校正的语法:
setStyleSheet(
"ChatWindow{background:url(" +
filePath +
") 0 0 0 0 stretch stretch;}"
);
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 选择要调用的构造函数
- C++选择排序算法中的逻辑错误
- QTreeView幻灯片多选后无法使用单击选择
- 无法获取菜单选择以运行函数.C++
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在C++中,如何通过几种类型从元组中选择多个元素
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 对可变参数使用声明.如何选择正确的功能
- 选择选举获胜者的程序
- 如何选择在 csv 文件中输出的位置
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 选择和修改嵌套向量中的条目的最佳实践
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 在运行时选择父类的实现
- 数数并选择 sqlite 中的前三名
- 如何在QT中设置样式表以选择假发背景的随机图像