Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RedFree opened this issue May 10, 2024 · 4 comments
Closed

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

RedFree opened this issue May 10, 2024 · 4 comments

Comments

@RedFree
Copy link

RedFree commented May 10, 2024

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

  1. 主窗口调用
void MainWindow::onCommonSetting() {
    CommonSettingWidget* commonDlg = new CommonSettingWidget(this);
    commonDlg->exec();
}
  1. 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
Copy link
Contributor

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

QGuiApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

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

@RedFree
Copy link
Author

RedFree commented May 11, 2024

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

@SineStriker
Copy link
Contributor

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

windowAgent->setTitleBar(windowBar);

建议仔细看README。

@RedFree
Copy link
Author

RedFree commented May 11, 2024

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

@RedFree RedFree closed this as completed May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants