前提・実現したいこと
QtでQMenuWindowを継承したウィンドウを作っているのですが、そのメニューバーの背景色を元の白色から灰色(ちょうどQt Creatorのメニューバーの色)に変更したいです。
発生している問題
QPaletteを用いて、変更したいのですがその方法が分かりません。
試したこと
次のようにsetPalette()を使用しましたが、変化はありませんでした。
c++
1qDebug() << menuBar()->foregroundRole(); //ButtonText 2qDebug() << menuBar()->backgroundRole(); //Button 3qDebug() << menuBar()->palette(); //変更前のPalette 4 5QPalette palette; 6palette.setColor(QPalette::ColorRole::Button, Qt::black); 7 8menuBar()->setPalette(palette); 9 10qDebug() << menuBar()->palette(); //変更後のPalette
出力は以下の通りでした。
txt
1//変更前 2QPalette(resolve=0x0) 3//変更後 4QPalette(resolve=0x10000080000400,"Window:[Active:#ff000000,Disabled:#ff000000,Inactive:#ff000000]")
値は変わっていますが、メニューバーは変化がありませんでした。ほかにもQPalette::ColorRoleをWindowやBaseでやっても変化はありませんでした。
QWidgetクラスのドキュメントのQPalette項目を見てみると以下のようにありました。
The default depends on the system environment. QApplication maintains a system/theme palette which serves as a default for all widgets. There may also be special palette defaults for certain types of widgets (e.g., on Windows Vista, all classes that derive from QMenuBar have a special default palette). You can also define default palettes for widgets yourself by passing a custom palette and the name of a widget to QApplication::setPalette(). Finally, the style always has the option of polishing the palette as it's assigned (see QStyle::polish()).
QMenuBarクラスには特別にデフォルトのQPaletteが設定されているらしい??そこで書いて通りに、QMenuBarのPaletteを上書きしたく、次のようにしました。
C++
1qDebug() << QApplication::palette("QMenuBar"); //変更前のQMenuBarのPalette 2 3QPalette palette; 4palette.setColor(QPalette::ColorRole::Button, Qt::black); 5QApplication::setPalette(palette, "QMenuBar"); 6 7qDebug() << QApplication::palette("QMenuBar"); //変更後
しかし、メニューバーの色は変わりませんでした。出力は以下の通りです
txt
1//変更前 2QPalette(resolve=0x77ffffa7fffdffff," 3WindowText:[Active:#ff000000,Disabled:#ff6d6d6d,Inactive:#ff000000], 4Button:[Active:#fff0f0f0,Disabled:#fff0f0f0,Inactive:#fff0f0f0], 5Light:[Active:#ffffffff,Disabled:#ffffffff,Inactive:#ffffffff], 6Midlight:[Active:#ffe3e3e3,Disabled:#fff7f7f7,Inactive:#ffe3e3e3], 7Dark:[Active:#ffa0a0a0,Disabled:#ffa0a0a0,Inactive:#ffa0a0a0], 8Mid:[Active:#ffa0a0a0,Disabled:#ffa0a0a0,Inactive:#ffa0a0a0], 9Text:[Active:#ff000000,Disabled:#ff6d6d6d,Inactive:#ff000000], 10BrightText:[Active:#ffffffff,Disabled:#ffffffff,Inactive:#ffffffff], 11ButtonText:[Active:#ff000000,Disabled:#ff787878,Inactive:#ffa0a0a0], 12Base:[Active:#ffffffff,Disabled:#fff0f0f0,Inactive:#ffffffff], 13Window:[Active:#fff0f0f0,Disabled:#fff0f0f0,Inactive:#fff0f0f0], 14Shadow:[Active:#ff696969,Disabled:#ff000000,Inactive:#ff696969], 15Highlight:[Active:#ff0078d7,Disabled:#ff0078d7,Inactive:#ff0078d7], 16HighlightedText:[Active:#ffffffff,Disabled:#ff6d6d6d,Inactive:#ffffffff], 17Link:[Active:#ff0000ff,Inactive:#ff0000ff], 18LinkVisited:[Active:#ffff00ff,Inactive:#ffff00ff], 19AlternateBase:[Active:#ffe9e7e3,Disabled:#fff7f7f7,Inactive:#ffe9e7e3], 20ToolTipBase:[Active:#ffffffdc,Disabled:#ffffffdc,Inactive:#ffffffdc], 21ToolTipText:[Active:#ff000000,Disabled:#ff000000,Inactive:#ff000000], 22PlaceholderText:[Active:#80000000,Disabled:#80000000,Inactive:#80000000]") 23//変更後 24QPalette(resolve=0x0)
正しくセットされていない??
他にもmenuWidget()やQMenu自体のPaletteを変更したりしましたが、変更できませんでした。
補足情報(FW/ツールのバージョンなど)
Desktop Qt 6.2.0 MSVC2019 64bit
メニューバーの背景色とは下のQt Creatorでいうと、「ファイル(F)」や「編集(E)」の背景のグレーの部分のことです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/25 01:47