Вопрос

//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)}");
}

way (1) can display toolbarBg2x picture, but way (2) display nothing. why?


My expected result is to apply the picture as background of the toolbar.
However the toolbar's size actually is in 100x30 via method 1.
Additional information : background picture's resolution is 800x60, MainWindow size is 800x600.

enter image description here


Qt5.1 clang 64 bit, MacOSX10.8


Это было полезно?

Решение

Working Sample:

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")
}

The following line doesn't work,

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

Add border : 0px to enforce drawing, and then it shows picture.

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

to apply style,

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

or

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

Both of them work well.

Tested under MacOS 10.9, Qt5.1.1

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top