stdware / qwindowkit

Cross-platform frameless window framework for Qt. Support Windows, macOS, Linux.
Apache License 2.0
442 stars 67 forks source link

QDialog使用QWK的windowBar无法拖动 #78

Closed RedFree closed 2 months ago

RedFree commented 2 months ago

你好,我使用QDialog作为子窗口,也想使用和主窗口一样的效果。主窗口使用QWK可以实现自定义的标题栏样式,我想实现QDialog子窗口也有同样的样式,但是使用之后发现QDialog无法拖动,设置样式背景颜色之后,会将关闭按钮的hover效果屏蔽掉。我的使用方法如下:

  1. 主窗口调用

    void MainWindow::onCommonSetting() {
    CommonSettingWidget* commonDlg = new CommonSettingWidget(this);
    commonDlg->exec();
    }
  2. QDialog窗口中初始化代码

    CommonSettingWidget::CommonSettingWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CommonSettingWidget)
    {
    ui->setupUi(this);
    
    // 窗口代理
    windowAgent = new QWK::WidgetWindowAgent(this);
    windowAgent->setup(this);
    
    // 关闭按钮
    auto closeButton = new QWK::WindowButton();
    closeButton->setObjectName(QStringLiteral("close-button"));
    closeButton->setProperty("system-button", true);
    closeButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    
    // 创建窗口标题栏
    auto windowBar = new QWK::WindowBar(this);
    windowBar->setCloseButton(closeButton);
    
    auto titleLabel = new QLabel();
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setObjectName(QStringLiteral("win-title-label"));
    
    windowBar->setTitleLabel(titleLabel);
    windowBar->setHostWidget(this);
    windowAgent->setSystemButton(QWK::WindowAgentBase::Close, windowBar->closeButton());
    
    connect(windowBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
    this->layout()->setMenuBar(windowBar);
    }

    还请大佬帮忙看下。 下图是实现的QDialog子窗口: 1111

感觉是不是我的

this->layout()->setMenuBar(windowBar);

这个代码有问题。

SineStriker commented 2 months ago

在main函数里有没有加入初始化代码:

QGuiApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

没有的话加一下,仔细看README。

RedFree commented 2 months ago

我是照着ReadMe文件添加的,主函数也是加了这个的 选区_316 我是这样加的,应该没问题吧, 我是在windows11系统上

SineStriker commented 2 months ago

我看错了,我看成MainWindow无法拖动了。Dialog无法拖动是因为你少写了一句

windowAgent->setTitleBar(windowBar);

建议仔细看README。

RedFree commented 2 months ago

感谢大佬,确实是漏了这一个,加上之后可以了