如何设置QToolBar的背景图片?

时间:2013-07-30 10:01:25

标签: qt qtoolbar

//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    //...

    QToolBar * toolbar = new QToolBar(this);

    this->addToolBar(Qt::BottomToolBarArea, toolbar);
    toolbar->setMovable(false);

    QToolButton * button = new QToolButton(this);
    toolbar->addWidget(button);

    //way 1: It display a picture
    toolbar->setStyleSheet("background-image: url(:/images/toolbarBg)");

    //way 2: It doesn't display picture
    //qApp->setStyleSheet("QToolBar {background-image: url(:/images/toolbarBg)}");
}

方式(1)可以显示toolbarBg2x图片,但方式(2)不显示任何内容。为什么呢?


我的预期结果是将图片应用为工具栏的背景 但是,工具栏的大小实际上是通过方法1在100x30 附加信息:背景图片的分辨率为800x60,MainWindow尺寸为800x600。

enter image description here


Qt5.1 clang 64 bit,MacOSX10.8


1 个答案:

答案 0 :(得分:2)

工作样本:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_toolbar( new QToolBar )
{
    this->addToolBar(Qt::BottomToolBarArea, m_toolbar);
    //m_toolbar->setStyleSheet("background-image: url(:/image/toolbar.png);");   
    m_toolbar->setStyleSheet("background-image: url(:/images/toolbar.png); border: 0px")
}

以下行不起作用,

m_toolbar->setStyleSheet("background-image: url(:/image/toolbar.png);"); 

添加border : 0px以强制执行绘图,然后显示图片。

m_toolbar->setStyleSheet("background-image: url(:/images/toolbar.png); border: 0px")

应用样式,

m_toolbar->setStyleSheet("background-image: url(:/images/toolbar.png); border: 0px")

this->setStyleSheet("QToolBar{background-image: url(:/images/toolbar.png); border: 0px;}");

两者都运作良好。

在MacOS 10.9,Qt5.1.1

下测试
相关问题