gpt4 book ai didi

c++ - QCursor 设置光标颜色的问题

转载 作者:行者123 更新时间:2023-12-02 10:17:43 25 4
gpt4 key购买 nike

C++ == 语言。

QT 库。

使用 qt 库时,我无法准确弄清楚如何使用位图或其他方式更改 UI 的光标颜色。任何建议都会很棒!

感谢您的时间。

最佳答案

要更改光标形状甚至使用自定义光标,QCursor可以使用。

它提供了多种构造函数来以不同的方式创建游标:

QCursor::QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)

Constructs a custom pixmap cursor.

pixmap is the image. It is usual to give it a mask (set using QPixmap::setMask()). hotX and hotY define the cursor's hot spot.

If hotX is negative, it is set to the pixmap().width()/2. If hotY is negative, it is set to the pixmap().height()/2.

Valid cursor sizes depend on the display hardware (or the underlying window system). We recommend using 32 x 32 cursors, because this size is supported on all platforms. Some platforms also support 16 x 16, 48 x 48, and 64 x 64 cursors.

See also QPixmap::QPixmap() and QPixmap::setMask().


QCursor::QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX = -1, int hotY = -1)

Constructs a custom bitmap cursor.

bitmap and mask make up the bitmap. hotX and hotY define the cursor's hot spot.

If hotX is negative, it is set to the bitmap().width()/2. If hotY is negative, it is set to the bitmap().height()/2.

The cursor bitmap (B) and mask (M) bits are combined like this:

  • B=1 and M=1 gives black.
  • B=0 and M=1 gives white.
  • B=0 and M=0 gives transparent.
  • B=1 and M=0 gives an XOR'd result under Windows, undefined results on all other platforms.

Use the global Qt color Qt::color0 to draw 0-pixels and Qt::color1 to draw 1-pixels in the bitmaps.

Valid cursor sizes depend on the display hardware (or the underlying window system). We recommend using 32 x 32 cursors, because this size is supported on all platforms. Some platforms also support 16 x 16, 48 x 48, and 64 x 64 cursors.

See also QBitmap::QBitmap() and QBitmap::setMask().


QCursor::QCursor(Qt::CursorShape shape)

Constructs a cursor with the specified shape.

See Qt::CursorShape for a list of shapes.



因此,对于彩色自定义光标,必须使用带有 pixmap 的构造函数。

QWidget::setCursor() ,自定义光标可以应用于特定的小部件。

我准备了一个 MCVE 来演示各种游标类型 - testQCursorShape.cc :
// Qt header:
#include <QtWidgets>

// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup data
#if 0 // bitmap bits
const quint32 bitmap[32] = {
//|0 |4 |8 |12 |16 |20 |24 |28 |32
0b00000000000000000000000000000000,//- 0
0b00000000000000000001000000000100,//
0b00000000000000000011100000001110,//
0b00000000000000000001110000011100,//
0b00000000000000000000111000111000,//- 4
0b00000000000000000000011101110000,//
0b00000000000000000000001111100000,//
0b00000000000000000000000111000000,//
0b00000000000000000000001111100000,//- 8
0b00000000000000000000011101110000,//
0b00000000000000000000111000111000,//
0b00000000000000000001110000011100,//
0b00000000000000000011100000001110,//- 12
0b00000000000000000001000000000100,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 16
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 20
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 24
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 28
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
};
const quint32 mask[32] = {
//|0 |4 |8 |12 |16 |20 |24 |28 |32
0b00000000000000000001000000000100,//- 0
0b00000000000000000011100000001110,//
0b00000000000000000111110000011111,//
0b00000000000000000011111000111110,//
0b00000000000000000001111101111100,//- 4
0b00000000000000000000111111111000,//
0b00000000000000000000011111110000,//
0b00000000000000000000001111100000,//
0b00000000000000000000011111110000,//- 8
0b00000000000000000000111111111000,//
0b00000000000000000001111101111100,//
0b00000000000000000011111000111110,//
0b00000000000000000111110000011111,//- 12
0b00000000000000000011100000001110,//
0b00000000000000000001000000000100,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 16
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 20
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 24
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//- 28
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
0b00000000000000000000000000000000,//
};
#endif // 0
const quint32 bitmap[32] = {
0x00000000,
0x00001004,
0x0000380e,
0x00001c1c,
0x00000e38,
0x00000770,
0x000003e0,
0x000001c0,
0x000003e0,
0x00000770,
0x00000e38,
0x00001c1c,
0x0000380e,
0x00001004,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};
const quint32 mask[32] = {
0x00001004,
0x0000380e,
0x00007c1f,
0x00003e3e,
0x00001f7c,
0x00000ff8,
0x000007f0,
0x000003e0,
0x000007f0,
0x00000ff8,
0x00001f7c,
0x00003e3e,
0x00007c1f,
0x0000380e,
0x00001004,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};
// setup GUI
// bit map cursor
const QBitmap qBitmap
= QBitmap::fromData(QSize(32, 32), (const uchar*)bitmap);
const QBitmap qBitmapMask
= QBitmap::fromData(QSize(32, 32), (const uchar*)mask);
const QCursor qCursorBitmap(qBitmap, qBitmapMask, 7, 7);
// pixmap cursor
const QPixmap qPixmap("pencil.png");
const QCursor qCursorPixmap(qPixmap, 2, 30);
enum {
TypeDefault, TypeShape, TypeBitmap, TypePixmap
};
// main window
QWidget qWinMain;
qWinMain.setWindowTitle(QString::fromUtf8("Test Custom Cursor Shape"));
QGridLayout qGrid;
QButtonGroup qTglGrp;
QRadioButton qTglCursorDefault("Default");
qTglCursorDefault.setChecked(true);
qTglGrp.addButton(&qTglCursorDefault, TypeDefault);
qGrid.addWidget(&qTglCursorDefault, 0, 0);
QRadioButton qTglCursorShape("Cursor with QCursorShape");
qTglGrp.addButton(&qTglCursorShape, TypeShape);
qGrid.addWidget(&qTglCursorShape, 1, 0);
QRadioButton qTglCursorBitmap("Bitmap Cursor");
qTglGrp.addButton(&qTglCursorBitmap, TypeBitmap);
qGrid.addWidget(&qTglCursorBitmap, 2, 0);
QRadioButton qTglCursorPixmap("Pixmap Cursor");
qTglGrp.addButton(&qTglCursorPixmap, TypePixmap);
qGrid.addWidget(&qTglCursorPixmap, 3, 0);
QFrame qFrmTest;
qFrmTest.setFrameStyle(QFrame::Panel | QFrame::Sunken);
qFrmTest.setFixedSize(256, 256);
qGrid.addWidget(&qFrmTest, 0, 1, 5, 1);
qWinMain.setLayout(&qGrid);
// install signal handlers
QObject::connect(&qTglGrp, QOverload<int>::of(&QButtonGroup::buttonClicked),
[&](int choice) {
switch (choice) {
default:
case TypeDefault: qFrmTest.unsetCursor(); break;
case TypeShape: qFrmTest.setCursor(Qt::PointingHandCursor); break;
case TypeBitmap: qFrmTest.setCursor(qCursorBitmap); break;
case TypePixmap: qFrmTest.setCursor(qCursorPixmap); break;
}
});
qWinMain.show();
// runtime loop
return app.exec();
}

一个 CMakeLists.txt准备VS解决方案:

project(QCursorShape)

cmake_minimum_required(VERSION 3.10.0)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(Qt5Widgets CONFIG REQUIRED)

include_directories("${CMAKE_SOURCE_DIR}")

add_executable(testQCursorShape testQCursorShape.cc)

target_link_libraries(testQCursorShape Qt5::Widgets)

和使用的图像文件 pencil.png这是用 alpha channel 准备的(用于透明度):

pencil.png

使用 VS2017 和 Qt 5.13(在 Windows 10 上)编译和测试:

animated snapshot of testQCursorShape

关于c++ - QCursor 设置光标颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61400766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com