Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 设置Qt::FramelessWindowHint时隐藏放置阴影_C++_Qt - Fatal编程技术网

C++ 设置Qt::FramelessWindowHint时隐藏放置阴影

C++ 设置Qt::FramelessWindowHint时隐藏放置阴影,c++,qt,C++,Qt,我曾多次尝试创建一个没有操作系统提供边界的应用程序,但经过数小时的努力,我放弃了这个想法。问题是,当我将Qt::FramelessWindowHint设置到小部件时,创建一个实例并显示它,它隐藏了它的阴影。这是我正在研究的一个例子 在本例中,您可以看到小部件周围没有阴影。但洞里有阴影。下面是小部件的构造函数提供的代码片段,它在孔中形成阴影 MyWidget::MyWidget(// some params) { ... setAttribute(Qt::WA_NoSystemBack

我曾多次尝试创建一个没有操作系统提供边界的应用程序,但经过数小时的努力,我放弃了这个想法。问题是,当我将
Qt::FramelessWindowHint
设置到小部件时,创建一个实例并显示它,它隐藏了它的阴影。这是我正在研究的一个例子

在本例中,您可以看到小部件周围没有阴影。但洞里有阴影。下面是小部件的构造函数提供的代码片段,它在孔中形成阴影

MyWidget::MyWidget(// some params)
{
  ...

  setAttribute(Qt::WA_NoSystemBackground);
  setAttribute(Qt::WA_TranslucentBackground);
  setWindowFlags(Qt::FramelessWindowHint);
  QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
  effect->setBlurRadius(5);
  effect->setOffset(0, 0);
  setGraphicsEffect(effect);

  ...
}

// main.cpp

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  MyWidget w;
  w.show();
  return app.exec();
}

但是如何才能在小部件周围绘制阴影呢

缺少完整且最小的示例,但可能是您刚刚将属性设置为错误的属性。如果你是继承而不是继承,这也可能是你所经历的奇怪行为的原因。例如,在实现一些自定义的基于视图的对话框时,最好为默认的中心容器提供一些布局,并将样式放在容器上,而不是对话框上,以避免操作系统相关或Qt问题。此外,如果希望引入自定义对话框,建议继承它自己,因为Qt在幕后为您做了大量与定位、设置模态等相关的工作。因此,应首选标准Qt行为

下面是一个示例:

// ShadowedDialog.h
#pragma once
#include <QDialog>

class ShadowedDialog
    : public QDialog
{
    Q_OBJECT
public:
    ShadowedDialog(QWidget* parent = nullptr);
    virtual ~ShadowedDialog();
};


// ShadowedDialog.cpp
#include <QGraphicsDropShadowEffect>
#include <QHBoxLayout>

#include "ShadowedDialog.h"

ShadowedDialog::ShadowedDialog(QWidget* parent)
    : QDialog(parent)
{
    // Set your own window flags, but don't forget to keep the default ones for the dialog.
    this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);

    // Translucent background is also needed in order for the shadow to work.
    this->setAttribute(Qt::WA_TranslucentBackground, true);

    // You would never need to do that, but this will make sure example will be visible enough.
    this->setFixedSize(400, 400);

    // Then, UI is setup.
    auto container = new QWidget();
    container->setObjectName("container");
    container->setStyleSheet("#container{background-color:#ffffff;border:1px solid #000000;}");
    auto layout = new QHBoxLayout();
    layout->addWidget(container);
    this->setLayout(layout);

    // And finally, QGraphicsDropShadowEffect is put with red drop shadow.
    auto effect = new QGraphicsDropShadowEffect();
    effect->setBlurRadius(5);
    effect->setXOffset(5);
    effect->setYOffset(5);
    effect->setColor(QColor("#ff0000"));

    container->setGraphicsEffect(effect);
}

ShadowedDialog::~ShadowedDialog()
{
}
//ShadowedDialog.h
#布拉格语一次
#包括
类阴影对话
:公共QDialog
{
Q_对象
公众:
ShadowedDialog(QWidget*parent=nullptr);
虚拟~ShadowedDialog();
};
//ShadowedDialog.cpp
#包括
#包括
#包括“ShadowedDialog.h”
ShadowedDialog::ShadowedDialog(QWidget*父项)
:QDialog(父级)
{
//设置您自己的窗口标志,但不要忘记保留对话框的默认标志。
this->setWindowFlags(this->windowFlags()| Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
//半透明背景也是阴影工作所必需的。
此->setAttribute(Qt::WA_半透明背景,true);
//您永远不需要这样做,但这将确保示例足够可见。
此->设置固定大小(400400);
//然后,设置UI。
自动容器=新的QWidget();
容器->设置对象名(“容器”);
容器->设置样式表(#容器{背景色:#ffffff;边框:1px实心#000000;});
自动布局=新的QHBoxLayout();
布局->添加小部件(容器);
此->设置布局(布局);
//最后,QGraphicsDropShadowEffect使用红色阴影放置。
自动效果=新的QGraphicsDropShadowEffect();
效应半径(5);
效果->设置偏移(5);
效果->设置偏移(5);
效果->设置颜色(QColor(#ff0000”);
容器->设置图形效果(效果);
}
ShadowedDialog::~ShadowedDialog()
{
}
该示例非常简单,基本上执行以下操作: 1.它设置了一些具有黑色边框的白色背景的容器。 2.然后,设置。很可能,这就是你想在对话中看到的阴影

结果如下(在Windows 7上测试):


一般来说,这里的主要任务是在对话框的子对象上设置样式,而不是在对话框本身上设置样式。希望给出的示例能让您明白,您将能够实现您想要的目标。

请提供一个完整且最小的示例。@RomhaKorev您所期望的缺少了什么?如何绘制绿色背景:在
MyWidget
类或它的子类中(使用pixmap、paintEvent等)?你如何画洞(用遮罩,它是背景图像的一部分,等等)?如果您提供一个完整的示例来重现您的提供(一个带有孔和背景的最小小部件),我可以为您提供正确的答案。所有内容都是使用
QPainter
paintEvent
函数中绘制的。没有使用位图和图像。这些孔是使用
qPaint::setCompositionMode(qPaint::CompositionMode_Clear)
制作的。我不认为有更多的细节需要回答我的问题。我可以通过在另一个较大的
QWidget
内部添加
QWidget
并在第二个上应用
QGraphicsDropShadowEffect
得到相同的结果。