c++ - 最小化对话框的 frameGeometry (titleBar)

标签 c++ qt

我有一个在其父窗口中最小化的对话框。当我调用 frameGeometry() 时,结果是事件对话框的(未最小化)数字。

我想知道 titleBar 在哪里。 (当对话框最小化时只显示对话框的标题栏)

最佳答案

你能试试这个吗

头文件

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QtWidgets/QDialog>
#include <QtWidgets/qmainwindow.h>
class MyDialog : public QDialog
{
    Q_OBJECT

public:
    MyDialog(QWidget *parent = 0);
    ~MyDialog();

protected:
    virtual bool nativeEvent(const QByteArray & eventType, void * message, long * result);

};

class MyWindow : public QMainWindow
{
    Q_OBJECT
public: 
    MyWindow();
    ~MyWindow();

private:
    MyDialog * m_dialog;

};

#endif // MYDIALOG_H

源文件

#include "mydialog.h"
#include <windows.h>
#include <windowsx.h>
#include <QDebug>
#include <QTimer>
MyDialog::MyDialog(QWidget *parent)
    : QDialog(parent)
{
    setStyleSheet("QDialog{background-color: red}");
}

MyDialog::~MyDialog()
{

}

bool MyDialog::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
    MSG* msg = (MSG*)(message);
    if (msg->message == WM_NCLBUTTONDOWN)
    {
        if (isMinimized())
        {
            QTimer::singleShot(50, this, SLOT(showNormal()));
            *result = 0;
            return true;
        }
        else
        {
            int mouseX = GET_X_LPARAM(msg->lParam);
            int mouseY = GET_Y_LPARAM(msg->lParam);

            QRect frame = frameGeometry();
            QRect content = geometry();

            qDebug() << "frame: " << frame;
            qDebug() << "content: " << content;

            if (mouseY < content.y() && mouseY >= frame.y())
            {
                qDebug() << "Hit title bar";
                showMinimized();
            }
        }
    }

    *result = 0;
    return false;
}

MyWindow::MyWindow()
    :QMainWindow()
{
    setStyleSheet("QMainWindow{background-color: blue}");
    showMaximized();
    m_dialog = new MyDialog(this);
    m_dialog->showMinimized();
}

MyWindow::~MyWindow()
{
}

关于c++ - 最小化对话框的 frameGeometry (titleBar),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46153419/

相关文章:

c++ - Qt4 槽和信号 : Qt4 has trouble finding the signal

linux - 安装适用于 Linux 的 QODBC 驱动程序

c++ - C++ DLL 中的线程安全字符串缓冲区变量

c++ - C 和 C++11 中系统时钟的性能

c++ - 接受表示不同数量参数的函数的 lambda 的类方法

c++ - QThreads 的并发问题。接收相同信号的线程相互阻塞

c++ - 如何将 qt 应用部署到 UWP 或 Windows 10 移动版

c++ - 位板:十六进制转二进制

c++ - 派生对象的 boost 序列化不调用派生的序列化()

linux - 为 Qt 创建位图字体