0
//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


CCC
  • 2,164
  • 8
  • 27
  • 47
  • 1
    what you don't understand? I have made the comments in the code. the two questions are related, and point 2 is tell the actual result based on the code, and you remove it....don't pretend to be expert – CCC Jul 30 '13 at 10:35
  • What I mean is, your question is badly written, difficult to understand, it starts with a wall of code, without saying anything first, and you don't capitalize your sentences. That's unprofessional. Your title says "How to setup QToolBar's background image?" but in your question you admit you already know how to do it (your method 1). Your question is really "why method 2 doesn't work", which is unrelated to your title. – sashoalm Jul 30 '13 at 10:57
  • of course not, the method 1 is not correct, so I tried to use method 2. Both ways are failed, this is why I asked, and point 2 gave additional context. Sorry dude, coder like starting with code at beginning. Don't bullshit here, just waste time. And you think you are professional, you can edit and give suggestion and don't try to twist my idea. thanks anyway – CCC Jul 30 '13 at 11:06
  • please. Respect is from yourself first. If I am lazy, I don't need to keep modifying to make it clear. – CCC Jul 30 '13 at 11:18
  • #update: The guy is not honest, deleted his previous comments. – CCC Aug 27 '13 at 10:14
  • @sashoalm: I agree. iAsk, the person is honest, but does not like the noise. Comments are not about to generate them. – László Papp Sep 08 '13 at 08:45

1 Answers1

2

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

CCC
  • 2,164
  • 8
  • 27
  • 47