我有一个在其父窗口中最小化的对话框。当我调用frameGeometry()时,结果是活动对话框(未最小化)数字。
我想知道titleBar的位置。 (当对话框最小化时,只显示对话框的titleBar)
答案 0 :(得分:1)
你能试试吗
头文件
#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()
{
}