So I'm trying to choose random image from file system of my pc and make it background in wiget. So thats why I'm opening QFileDialog and using it. qDebug gives me right path to the image, but still it doesn't work.
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;");
}
I've seen this one Unable to set the background image in Qt Stylesheet related to my problem, but in my case it doesn't work.
According to the documentation, there is no such property called border-image
: what you're searching for is background-image
.
Moreover, since you're also specifying other background's parameters, you should use background
.
Moreover, since your file is on the disk and not in the resources, the url should not start with :
: type url(" + filePath + ")
instead of url(:" + filePath + ")
.
The corrected syntax:
setStyleSheet(
"ChatWindow{background:url(" +
filePath +
") 0 0 0 0 stretch stretch;}"
);