2017-01-22 4 views
0

QMenuBar 예를 들어 두 개의 QMenu 항목이 있습니다.Qt 스타일 시트 : 특정 QMenuBar :: item 배경색 설정

enter image description here

가 어떻게에만 항목이 예를 들어, 파란색으로은 "마루"를 만들 수 있습니까? 나는 모든 품목을 위해 그것을 바꾸는 방법을 안다 :

QMenuBar::item { 
    background: ...; 
} 

그러나 나는 특정한 물건을 채색 할 길을 찾을 수 없다. 나는 으로 시도한 QmenusetProperty을 사용하려고 시도했지만, 나는 아무 것도 발견하지 못했습니다. C 코드에서 특정 QMenuBar::item 속성을 설정하는 방법이 있습니까?

답변

0

드디어 찾았습니다.

  1. 예를 WidgetMenuBar를 들어, QMenuBar에서 상속, 자신의 개체를 만듭니다.

  2. 느릅 나무 항목을 식별하는 속성을 추가

    는 다르게 색칠해야합니다 위젯의

    for (int i = 0; i < this->actions().size(); i++){ 
        actions().at(i)->setProperty("selection",false); 
    } 
    // Only the first item colored 
    actions().at(0)->setProperty("selection",true); 
    
  3. 구현할 void paintEvent(QPaintEvent *e) 기능 :

    void WidgetMenuBarMapEditor::paintEvent(QPaintEvent *e){ 
        QPainter p(this); 
        QRegion emptyArea(rect()); 
    
        // Draw the items 
        for (int i = 0; i < actions().size(); ++i) { 
         QAction *action = actions().at(i); 
         QRect adjustedActionRect = this->actionGeometry(action); 
    
         // Fill by the magic color the selected item 
         if (action->property("selection") == true) 
          p.fillRect(adjustedActionRect, QColor(255,0,0)); 
    
         // Draw all the other stuff (text, special background..) 
         if (adjustedActionRect.isEmpty() || !action->isVisible()) 
          continue; 
         if(!e->rect().intersects(adjustedActionRect)) 
          continue; 
         emptyArea -= adjustedActionRect; 
         QStyleOptionMenuItem opt; 
         initStyleOption(&opt, action); 
         opt.rect = adjustedActionRect; 
         style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this); 
        } 
    } 
    

당신은 어떻게 paintEvent를 구현하는 here를 볼 수 있습니다 기능.