0

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.

4

1 回答 1

0

根据文档,没有这样的属性称为border-image:你正在搜索的是background-image.

此外,由于您还指定了其他背景的参数,因此您应该使用background.

此外,由于您的文件在磁盘上而不是在资源中,因此 url 不应以:: typeurl(" + filePath + ")而不是url(:" + filePath + ").

更正后的语法:

setStyleSheet(
    "ChatWindow{background:url(" +
        filePath +
    ") 0 0 0 0 stretch stretch;}"
);
于 2017-06-06T11:45:41.960 回答