赞
踩
- #include <QtWidgets>
-
- class ChildWidget : public QWidget {
- public:
- ChildWidget(QWidget *parent = nullptr) : QWidget(parent) {
- setAutoFillBackground(true);
- setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
- }
-
- protected:
- void paintEvent(QPaintEvent *event) override {
- Q_UNUSED(event);
-
- // 绘制子窗口的颜色
- QPainter painter(this);
- painter.fillRect(rect(), Qt::blue);
- }
- };
-
- class ParentWidget : public QWidget {
- public:
- ParentWidget(QWidget *parent = nullptr) : QWidget(parent) {
- setWindowTitle("Parent Widget");
-
- // 创建子窗口对象
- childWidget = new ChildWidget(this);
-
- // 设置子窗口对象为透明,以显示父窗口的背景颜色
- childWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
-
- // 创建布局管理器
- layout = new QVBoxLayout(this);
-
- // 将子窗口添加到布局中
- layout->addWidget(childWidget);
-
- // 设置布局管理器为父窗口的布局
- setLayout(layout);
- }
-
- void mousePressEvent(QMouseEvent *event) override {
- if (event->button() == Qt::LeftButton) {
- // 记录鼠标点击时父窗口的位置和子窗口的大小
- startPos = event->pos();
- startSize = childWidget->size();
- }
- }
-
- void mouseMoveEvent(QMouseEvent *event) override {
- if (event->buttons() & Qt::LeftButton) {
- // 计算鼠标拖拽的偏移量
-
- int dx = event->pos().x() - startPos.x();
- int dy = event->pos().y() - startPos.y();
-
- // 改变子窗口的大小
- childWidget->resize(startSize.width() + dx, startSize.height() + dy);
- }
- }
-
- private:
- QVBoxLayout *layout;
- ChildWidget *childWidget;
- QPoint startPos;
- QSize startSize;
- };
-
- int main(int argc, char *argv[]) {
- QApplication app(argc, argv);
-
- ParentWidget parentWidget;
- parentWidget.show();
-
- return app.exec();
- }
- #include <QtWidgets>
-
- class ResizableWidget : public QWidget
- {
- public:
- ResizableWidget(QWidget *parent = nullptr) : QWidget(parent)
- {
- setWindowFlags(Qt::FramelessWindowHint); // 设置无边框窗口
- setMouseTracking(true); // 开启鼠标跟踪
- }
-
- protected:
- void mousePressEvent(QMouseEvent *event) override
- {
- if (event->buttons() & Qt::LeftButton) {
- // 记录当前鼠标位置和窗口位置
- m_startPos = event->globalPos();
- m_startGeometry = geometry();
- event->accept();
- }
- }
-
- void mouseMoveEvent(QMouseEvent *event) override
- {
- if (event->buttons() & Qt::LeftButton) {
- // 计算鼠标移动的偏移量
- QPoint offset = event->globalPos() - m_startPos;
-
- // 判断鼠标位置,调整窗口大小
- if (m_resizeLeft) {
- QRect newGeometry = m_startGeometry.adjusted(offset.x(), 0, 0, 0);
- setGeometry(newGeometry);
- } else if (m_resizeRight) {
- QRect newGeometry = m_startGeometry.adjusted(0, 0, offset.x(), 0);
- setGeometry(newGeometry);
- } else if (m_resizeTop) {
- QRect newGeometry = m_startGeometry.adjusted(0, offset.y(), 0, 0);
- setGeometry(newGeometry);
- } else if (m_resizeBottom) {
- QRect newGeometry = m_startGeometry.adjusted(0, 0, 0, offset.y());
- setGeometry(newGeometry);
- } else if (m_resizeTopLeft) {
- QRect newGeometry = m_startGeometry.adjusted(offset.x(), offset.y(), 0, 0);
- setGeometry(newGeometry);
- } else if (m_resizeTopRight) {
- QRect newGeometry = m_startGeometry.adjusted(0, offset.y(), offset.x(), 0);
- setGeometry(newGeometry);
- } else if (m_resizeBottomLeft) {
- QRect newGeometry = m_startGeometry.adjusted(offset.x(), 0, 0, offset.y());
- setGeometry(newGeometry);
- } else if (m_resizeBottomRight) {
- QRect newGeometry = m_startGeometry.adjusted(0, 0, offset.x(), offset.y());
- setGeometry(newGeometry);
- }
-
- event->accept();
- }
- }
-
- void mouseReleaseEvent(QMouseEvent *event) override
- {
- Q_UNUSED(event);
- // 重置所有的resize标志
- m_resizeLeft = false;
- m_resizeRight = false;
- m_resizeTop = false;
- m_resizeBottom = false;
- m_resizeTopLeft = false;
- m_resizeTopRight = false;
- m_resizeBottomLeft = false;
- m_resizeBottomRight = false;
- }
-
- void resizeEvent(QResizeEvent *event) override
- {
- Q_UNUSED(event);
- // 设置子窗口的背景颜色,用以区分
- setStyleSheet("background-color: yellow;");
- }
-
- void enterEvent(QEvent *event) override
- {
- Q_UNUSED(event);
- // 根据鼠标位置设置对应的resize标志
- QPoint pos = mapFromGlobal(QCursor::pos());
- m_resizeLeft = (pos.x() <= 5);
- m_resizeRight = (pos.x() >= width() - 5);
- m_resizeTop = (pos.y() <= 5);
- m_resizeBottom = (pos.y() >= height() - 5);
- m_resizeTopLeft = (m_resizeLeft && m_resizeTop);
- m_resizeTopRight = (m_resizeRight && m_resizeTop);
- m_resizeBottomLeft = (m_resizeLeft && m_resizeBottom);
- m_resizeBottomRight = (m_resizeRight && m_resizeBottom);
- }
-
- private:
- QPoint m_startPos; // 鼠标按下时的起始位置
- QRect m_startGeometry; // 窗口的起始位置和大小
-
- bool m_resizeLeft = false; // 是否可以改变窗口的左边界
- bool m_resizeRight = false; // 是否可以改变窗口的右边界
- bool m_resizeTop = false; // 是否可以改变窗口的上边界
- bool m_resizeBottom = false; // 是否可以改变窗口的下边界
- bool m_resizeTopLeft = false; // 是否可以改变窗口的左上角
- bool m_resizeTopRight = false; // 是否可以改变窗口的右上角
- bool m_resizeBottomLeft = false; // 是否可以改变窗口的左下角
- bool m_resizeBottomRight = false; // 是否可以改变窗口的右下角
- };
-
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget parentWidget;
- parentWidget.setGeometry(100, 100, 400, 300);
- parentWidget.setStyleSheet("background-color: gray;");
-
- ResizableWidget childWidget(&parentWidget);
- childWidget.setGeometry(50, 50, 200, 150);
-
- parentWidget.show();
-
- return app.exec();
- }
- #include <QtWidgets>
-
- // 自定义的子窗口类
- class ChildWidget : public QWidget
- {
- public:
- ChildWidget(QWidget *parent = nullptr) : QWidget(parent)
- {
- setAutoFillBackground(true);
- QPalette palette;
- palette.setColor(QPalette::Background, Qt::green);
- setPalette(palette);
- }
-
- // 鼠标按下事件
- void mousePressEvent(QMouseEvent *event) override
- {
- if (event->button() == Qt::LeftButton)
- {
- // 记录鼠标按下时的位置
- m_lastPos = event->pos();
- }
- }
-
- // 鼠标移动事件
- void mouseMoveEvent(QMouseEvent *event) override
- {
- // 如果鼠标左键被按下,则进行窗口大小的改变
- if (event->buttons() & Qt::LeftButton)
- {
- // 计算鼠标移动的偏移量
- int dx = event->x() - m_lastPos.x();
- int dy = event->y() - m_lastPos.y();
-
- // 改变窗口大小
- // 这里只实现了拖拽右下角改变大小的功能,你可以根据需求调整和扩展
- QSize newSize = size() + QSize(dx, dy);
- resize(newSize);
-
- event->accept();
- }
- }
-
- private:
- QPoint m_lastPos;
- };
-
- // 自定义的主窗口类
- class MainWindow : public QMainWindow
- {
- public:
- MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
- {
- // 创建两个QWidget,一个作为主窗口,一个作为子窗口
- QWidget *mainWidget = new QWidget(this);
- ChildWidget *childWidget = new ChildWidget(mainWidget);
-
- // 使用垂直布局将子窗口添加到主窗口上
- QVBoxLayout *layout = new QVBoxLayout(mainWidget);
- layout->addWidget(childWidget);
-
- // 将主窗口设置为应用程序的主窗口
- setCentralWidget(mainWidget);
- }
- };
-
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- MainWindow mainWindow;
- mainWindow.show();
-
- return app.exec();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。