2010-04-20 13 views
6

QPushButton의 배경 이미지를 설정하는 데 어려움을 겪고 있습니다. 지금까지 성공하지 못했습니다. 다음은 내 코드입니다.QPushButton의 배경 이미지 설정

appsWidget::appsWidget(QWidget *parent) 
    :QWidget(parent) 
{ 
    QPushButton *button1 = new QPushButton("SETTINGS",this); 
    QPushButton *button2 = new QPushButton("TEST",this); 
    QPushButton *button3 = new QPushButton("IE",this); 

    button1->setStyleSheet("background-image:url(config.png)"); -> No success 


    qDebug("appWidget initialized."); 

    QHBoxLayout *layout = new QHBoxLayout; 
    layout->addWidget(button1); 
    layout->addWidget(button2); 
    layout->addWidget(button3); 
    this->setLayout(layout); 
    connect(button1,SIGNAL(clicked()),this,SLOT(setClickIndex1())); 
    connect(button2,SIGNAL(clicked()),this,SLOT(setClickIndex2())); 
    connect(button3,SIGNAL(clicked()),this,SLOT(setClickIndex3())); 
} 

스타일 시트에서 사용중인 이미지는 동일한 프로젝트 폴더에 있습니다. 아무도 해결책이 있습니까? -

button1->setAutoFillBackground(true);

당신은 '아무튼 QToolButton보고 할 수 있습니다 당신은 또한 autofillbackground을 설정해야

button1->setFlat(true);

:

답변

6

당신은 true로 평면 속성을 설정해야 이미지를 렌더링하려면 평평하게해야합니다. 나는이 순간에 쓰고 있어요 응용 프로그램에서 그들을 사용하고 있는데 그들은 매우 좋은 모양 :

m_showAddCommentButton = new QToolButton(); 
m_showAddCommentButton->setAutoFillBackground(true); 
palette = m_showAddCommentButton->palette(); 
palette.setColor(QPalette::Button,QColor(82,110,166)); 
m_showAddCommentButton->setPalette(palette); 
m_showAddCommentButton->setIcon(QIcon(":/uiImages/addComment_50_50.jpg")); 
m_showAddCommentButton->setIconSize(QSize(40,40)); 
m_showAddCommentButton->setToolTip("Comment"); 
connect(m_showAddCommentButton, SIGNAL(clicked()), 
     manager, SLOT(showAddComment())); 
hLayout->addWidget(m_showAddCommentButton,0); 

(내 이미지는 리소스로 저장됩니다)

+0

위의 코드에서 버튼에 맞게 이미지의 크기를 조정해야합니까, 그렇지 않으면 자동으로 처리됩니까? –

+1

어느 쪽이든 갈 수 있습니다. 원래 이미지를 외부에서 50x50 크기로 편집기에서 크기를 조정했지만 40x40 - setIconSize()가 원하는대로 아이콘의 크기를 조정하고 싶다고 결정했습니다. –

+0

정확하지 않습니다. 플랫 특성이나 자동 채우기 배경을 설정할 필요가 없습니다. 단순히'setIcon (":/path/to/image.png")'를 통해 이미지를로드 할 수 있습니다. 나는 일반적으로 이미지를'QPixmap'에로드 할 것이므로 페인트 장치로 활용하고 Pixmap에서 어떤 요소가 필요한지 전달할 수 있습니다. –

2

귀하의 CSS 선택이 올바르지 않습니다.

button1->setStyleSheet("QPushButton{ background-image: url(config.png); }"); 
1

당신은 버튼을 평평 때 작동 QPushButton을 위해, 어떤 위젯 배경을 채우기 위해 팔레트 요소로 브러시를 사용할 수 있습니다

당신은 뭔가를해야한다.

QPixmap pixmap("image.jpg"); 
QPalette palette;  
QPushButton *button= new QPushButton(this); 
palette.setBrush(button->backgroundRole(), QBrush(pixmap)); 

button->setFlat(true); 
button->setAutoFillBackground(true);  
button->setPalette(palette);