gpt4 book ai didi

c++ - 如何在Windows composer中集成Qt无框窗口? (系统快捷方式不起作用)

转载 作者:行者123 更新时间:2023-11-28 04:04:16 25 4
gpt4 key购买 nike

我在 Windows 平台上工作。如果我使用原生无框窗口标志,例如:

::SetWindowLongPtr((HWND)winId(), GWL_STYLE, WS_POPUP | WS_SYSMENU | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX);

我收到的无框窗口可以与默认 Windows 窗口编辑器一起正常工作 - 当我按下“WIN”键 + 箭头时它可以正确更改状态。

当我尝试使用具有以下无框窗口标志的 Qt 库时:

setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::CustomizeWindowHint);

我收到的窗口对“WIN”键 + 箭头没有反应。所以它不适用于默认的 Windows 窗口编辑器。

Qt 窗口标志的哪些组合具有与上述 native 标志类似的行为?

最佳答案

看来,这确实与 Qt 关系不大。在 Windows 窗口上只有一个完整的框架似乎禁用了“捕捉”快捷方式,即使窗口仍然可以调整大小或使用键盘箭头移动(从 Alt+Space 系统菜单)。

解决方法实际上非常简单。基本上是实现 QWidget::nativeEvent() 虚方法并忽略 WM_NCCALCSIZE 消息。

我在从一个屏幕捕捉到另一个屏幕时遇到了一些绘画问题,但使用 mask 解决了这个问题(代码注释中的注释)。很高兴找到一个更干净的解决方案(实际上它可能符合 Qt 错误)。

作为奖励,这还允许圆角。绘画/样式基于我为 another answer 制作的圆形消息框,绘画代码在那边有更完整的记录。

系统菜单和所有与键的交互(移动/调整大小/捕捉/等)工作。可以通过在 nativeEvent() 中实现 WM_NCHITTEST 消息来添加鼠标处理(请参阅下面的引用资料)。

只在 Win7 上测试过,想知道它在 Win10 上的表现如何。

FramelessWidget

#include <QPainter>
#include <QPalette>
#include <QStyle>
#include <QStyleOption>
#include <QWidget>
#include <qt_windows.h>

class FramelessWidget : public QWidget
{
Q_OBJECT
public:
explicit FramelessWidget(QWidget *p = nullptr, Qt::WindowFlags f = Qt::Window) :
// the flags set here should "match" the GWL_STYLE flags set below
QWidget(p, f | Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::FramelessWindowHint)
{
setAttribute(Qt::WA_TranslucentBackground); // for rounded corners
// set flags which will override what Qt does, especially with the Qt::FramelessWindowHint which essentially disables WS_SIZEBOX/WS_THICKFRAME
SetWindowLongPtr(HWND(winId()), GWL_STYLE, WS_POPUPWINDOW | WS_CAPTION | WS_SIZEBOX | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN);
}

// these settings are only used when no styleSheet is set
qreal radius = 0.0; // desired radius in absolute pixels
qreal borderWidth = -1.0; // -1: use style hint frame width; 0: no border; > 0: use this width.

protected:
bool nativeEvent(const QByteArray &eventType, void *message, long *result) override
{
MSG *msg = static_cast<MSG*>(message);
if (msg && msg->message == WM_NCCALCSIZE) {
// Just return 0 and mark event as handled. This will draw the widget contents
// into the full size of the frame, instead of leaving room for it.
*result = 0;
return true;
}
return QWidget::nativeEvent(eventType, message, result);
}

// Override paint event because of transparent background.
// Can be styled using CSS or QPalette with backgroundRole()/foregroundRole().
void paintEvent(QPaintEvent *) override
{
QPainter p(this);
p.setRenderHints(QPainter::Antialiasing);
QStyleOption opt;
opt.initFrom(this);
// be sure to use the full frame size, not the default rect() which is inside frame.
opt.rect.setSize(frameSize());

if (testAttribute(Qt::WA_StyleSheetTarget)) {
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
setMask(QRegion(opt.rect)); // see notes below
return;
}

QRectF rect(opt.rect);
qreal penWidth = borderWidth;
if (penWidth < 0.0)
penWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this);
if (penWidth > 0.0) {
p.setPen(QPen(palette().brush(foregroundRole()), penWidth));
const qreal dlta = (penWidth * 0.5);
rect.adjust(dlta, dlta, -dlta, -dlta);
}
else {
p.setPen(Qt::NoPen);
}
p.setBrush(palette().brush(backgroundRole()));
if (radius > 0.0)
p.drawRoundedRect(rect, radius, radius, Qt::AbsoluteSize);
else
p.drawRect(rect);

// Setting a mask works around an issue with artifacts when switching screens with Win+arrow
// keys. I don't think it's the actual mask which does it, rather it triggers the region
// around the widget to be polished but I'm not sure. As support for my theory, the mask
// doesn't even have to follow the border radius.
setMask(QRegion(opt.rect));
}

}; // FramelessWidget

测试实现


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//QLoggingCategory::setFilterRules(QStringLiteral("qt.qpa.windows = true\n"));
FramelessWidget msgBox;
msgBox.setWindowTitle("Frameless window test");
msgBox.setLayout(new QVBoxLayout);
msgBox.layout()->addWidget(new QLabel(QStringLiteral("<h3>Frameless rounded widget.</h3>"), &msgBox));
QLabel *lbl = new QLabel(QStringLiteral(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean fermentum erat rhoncus, "
"scelerisque eros ac, hendrerit metus. Nunc ac lorem id tortor porttitor mollis. Nunc "
"tristique orci vel risus convallis, non hendrerit sapien condimentum. Phasellus lorem tortor, "
"mollis luctus efficitur id, consequat eget nulla. Nam ac magna quis elit tristique hendrerit id "
"at erat. Integer id tortor elementum, dictum urna sed, tincidunt metus. Proin ultrices tempus "
"lacinia. Integer sit amet fringilla nunc."
), &msgBox);
lbl->setWordWrap(true);
msgBox.layout()->addWidget(lbl);
QPushButton *pb = new QPushButton(QStringLiteral("Close"), &msgBox);
QObject::connect(pb, &QPushButton::clicked, qApp, &QApplication::quit);
msgBox.layout()->addItem(new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Expanding));
msgBox.layout()->addWidget(pb);

msgBox.setStyleSheet(QStringLiteral(
"FramelessWidget { "
"border-radius: 12px; "
"border: 3px solid palette(shadow); "
"background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, stop: 0 #ffeb7f, stop: 1 #d09d1e); "
"}"
));

msgBox.show();
return a.exec();
}

enter image description here

引用资料:

关于c++ - 如何在Windows composer中集成Qt无框窗口? (系统快捷方式不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59017362/

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