How to Change the Background Color of QWidget/ja: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Add "cleanup" tag)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:snippets]]
[[Category:snippets]]
[[Category:HowTo]]
[[Category:HowTo]]

Revision as of 15:46, 3 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English | Български | 日本語 | 简体中文

QWidget の背景色を変える方法

"QWidget":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html は全てのユーザーインターフェースオブジェクトの基底クラスです。そのため、他のウィジェットでも同じ方法で背景色を変えることが出来ます。

パレットを使う

最初のサンプルでは "QPalette":http://doc.qt.io/qt-5.0/qtgui/qpalette.html を使って背景色を変える方法を示します。

m_pMyWidget = new QWidget(this);
m_pMyWidget->setGeometry(0,0,300,100);
QPalette Pal(palette());
// 背景色を黒にする
Pal.setColor(QPalette::Background, Qt::black);
m_pMyWidget->setAutoFillBackground(true);
m_pMyWidget->setPalette(Pal);
m_pMyWidget->show();

スタイルシートを使う

スタイルシートではウィジェットのスタイルをテキストの記述を用いてカスタマイズすることが出来ます。詳細は "Qt Style Sheets document":http://doc.qt.io/qt-5.0/qtwidgets/stylesheet.html を参照してください。

m_pMyWidget = new QWidget(this);
m_pMyWidget->setGeometry(0,0,300,100);
m_pMyWidget->setStyleSheet("background-color:black;");
m_pMyWidget->show();

どちらの方法でも QWidget の背景色が変わったことを Qt SDK 1.1 と Symbian^3 デバイスで確認しました。