【保存版】Qt GUI プログラミングにおける QPixmap::defaultDepth() 関数のすべて


QPixmap::defaultDepth() 関数は、アプリケーションで使用されているデフォルトのピックスマップ深度を取得します。これは、ピクセルあたりのビット数 (bpp) またはピックスマップのビットプレーン数とも呼ばれます。

用途

この関数は、以下の用途で使用できます。

  • ハードウェアアクセラレーションの可能性を評価する
  • ピックスマップ形式を決定する
  • ピックスマップのメモリ割り当て量を推定する

戻り値

この関数は、現在のスクリーンの深度を返します。Windows と Mac では、デフォルトの深度が常に 32 です。X11 と埋め込みシステムでは、この関数はスクリーンの深度を返します。

int depth = QPixmap::defaultDepth();
qDebug() << "Default pixmap depth:" << depth;

注意点

  • ピックスマップの深度が 0 の場合、ピックスマップは空です。
  • QGuiApplication が作成される前にこの関数を呼び出すには、QApplication::initialize() 関数を呼び出す必要があります。
  • QColormap::depth():カラーマップの深度を取得します。
  • depth():ピックスマップの深度を取得します。
  • ほとんどの場合、32 ビットのピックスマップ深度で十分です。
  • ピックスマップの深度が大きければ大きいほど、ピックスマップの処理が遅くなります。
  • ピックスマップの深度が大きければ大きいほど、ピックスマップはより多くのメモリを消費します。


#include <QApplication>
#include <QPixmap>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // デフォルトのピックスマップ深度を取得する
    int depth = QPixmap::defaultDepth();

    // デフォルトのピックスマップ深度を出力する
    qDebug() << "Default pixmap depth:" << depth;

    return 0;
}

このコードを実行すると、以下の出力がコンソールに表示されます。

Default pixmap depth: 32

この例では、QApplication::initialize() 関数を明示的に呼び出す必要はありません。これは、main() 関数が自動的にこの関数を呼び出すためです。

#include <QApplication>
#include <QPixmap>
#include <QImage>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // デフォルトのピックスマップ深度を取得する
    int depth = QPixmap::defaultDepth();

    // QImage オブジェクトを作成する
    QImage image(100, 100, QImage::Format_RGB32);

    // QImage オブジェクトを QPixmap オブジェクトに変換する
    QPixmap pixmap = QPixmap::fromImage(image);

    // ピックスマップの深度を出力する
    qDebug() << "Pixmap depth:" << pixmap.depth();

    return 0;
}
Pixmap depth: 32

この例では、QImage::Format_RGB32 形式で 100 x 100 ピクセルの QImage オブジェクトを作成します。次に、QPixmap::fromImage() 関数を使用して、QImage オブジェクトを QPixmap オブジェクトに変換します。最後に、QPixmap::depth() 関数を使用して、ピックスマップの深度を出力します。



代替方法

  • QScreen::depth() 関数を使用する

この関数は、現在のスクリーンの深度を取得します。これは、QPixmap::defaultDepth() 関数と同じ結果を返します。

int depth = QScreen::defaultScreen()->depth();
qDebug() << "Default pixmap depth:" << depth;
  • QGuiApplication::primaryScreen() 関数と QScreen::depth() 関数を使用する

この方法は、マルチスクリーン環境で実行されているアプリケーションで使用できます。

QScreen *screen = QGuiApplication::primaryScreen();
int depth = screen->depth();
qDebug() << "Default pixmap depth:" << depth;
  • プラットフォーム固有の API を使用する

Windows、Mac、Linux などのプラットフォームには、デフォルトのピックスマップ深度を取得するためのプラットフォーム固有の API が用意されています。

Windows

#include <windows.h>

int main() {
    HDC hdc = GetDC(NULL);
    int depth = GetDeviceCaps(hdc, BITDEPTH);
    ReleaseDC(NULL, hdc);

    qDebug() << "Default pixmap depth:" << depth;

    return 0;
}

Mac

#include <Carbon/Carbon.h>

int main() {
    GDHandle gdh = GetMainDisplayGraphicsDevice();
    int depth = GetGDDeviceBitsPerPixel(gdh);

    qDebug() << "Default pixmap depth:" << depth;

    return 0;
}

Linux

#include <X11/Xlib.h>

int main() {
    Display *display = XOpenDisplay(NULL);
    int depth = XScreenDepth(XDefaultScreen(display));
    XCloseDisplay(display);

    qDebug() << "Default pixmap depth:" << depth;

    return 0;
}
  • プラットフォーム固有の API は、古いプラットフォーム ではサポートされない場合があります。
  • プラットフォーム固有の API を使用する場合、ポータビリティ が低下します。