要實現如圖所示的選單按鈕,有預設,懸停,點選三種狀態;發現用Qss統一設定樣式的時候,按下狀態無效;
QMenu::item { background: rgb(77, 77, 77); font-family: Microsoft YaHei UI; font-size: 14px; height:32px; color: rgba(255, 255, 255, 0.7); } QMenu::item:pressed//無效 { background-color: rgb(32,128,247); color:#FFFFFF } QMenu::item:selected//實際上是hover狀態 { color: rgba(255, 255, 255, 1); background: rgb(59,59,60); }
為了解決這個問題需要在程式碼裡設定樣式才有用
void LibItem::SlotOpenCtrl() { QMenu menu(this); QIcon iconEdit = QIcon(":/analysisCenter/Resources/analysisCenter/editNameNor.svg"); QIcon iconDelete = QIcon(":/preview/Resources/previewanalyze/delete.svg"); QAction *editAction= menu.addAction(iconEdit," 編輯"); QAction *deleteAction= menu.addAction(iconDelete," 刪除"); //用Qss檔案設定無效,但是用程式碼直接設定有效 menu.setStyleSheet("QMenu::item:pressed { background-color: rgb(32,128,247);color:#FFFFFF }"); connect(editAction, &QAction::triggered, this, [=]() {emit signalProcessLib("edit", m_info); }); connect(deleteAction, &QAction::triggered, this, [=]() {emit signalProcessLib("delete", m_info); }); menu.exec(QCursor::pos()); }