qt界面布局之重新设置放大缩小以及关闭按钮

一、新建一个widget类的qt程序,然后添加几个按钮,基本布局如下:

二、在加上按钮之后,需要做的是将之前的qt默认的放大缩小、关闭等系列的功能进行屏蔽,在构造函数中

加上如下的代码:

 setWindowFlags(Qt::FramelessWindowHint);//这边就是相当于直接将qt窗体默认的功能全部取消

三、开始进行放大、缩小、关闭的设置,可以有两种方式,但实用性较高的是第一种方式:

第一种方式:

在构造函数中加上:

   connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
   connect(ui->minButton, SIGNAL(clicked()), this, SLOT(showMinimized()));
   connect(ui->maxButton, SIGNAL(clicked()), this, SLOT(showMaximized()));

可以直接实现,以上是第一种较为实用的方式

第二种,本人认为不太实用:开始通过程序默认的ui文件进入,如图:

进入程序里,找到如下的位置:

开始进行信号和槽的绑定工作:

代码如下:

QObject::connect(closeButton, SIGNAL(clicked()), Widget, SLOT(close()));
QObject::connect(minButton, SIGNAL(clicked()), Widget, SLOT(showMinimized()));
QObject::connect(maxButton, SIGNAL(clicked()), Widget, SLOT(showMaximized())); 

这个时候可以直接编译,就可以实现放大缩小,但是我们会发现,由于在构造函数中加上了一句

setWindowFlags(Qt::FramelessWindowHint);就是将直接的代码进行了屏蔽,这样做的结果是直接导致窗体无法进行拖动了

所以必须重写鼠标事件:

以下需要重写的鼠标事件是鼠标按下,鼠标释放、鼠标移动

一、先加上头文件

#include <QMouseEvent>

在Widget.h文件中:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    bool mousepress;
    QPoint point;

protected:
    void mouseReleaseEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
};

#endif // WIDGET_H

在widget.cpp文件中:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);//这边就是相当于直接将qt窗体默认的功能全部取消
    mousepress = false;
   connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
   connect(ui->minButton, SIGNAL(clicked()), this, SLOT(showMinimized()));
   connect(ui->maxButton, SIGNAL(clicked()), this, SLOT(showMaximized()));
}

Widget::~Widget()
{
    delete ui;
}

//鼠标释放
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    mousepress = false;
}

//鼠标按下
void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)//判断左键是否按下
    {
        mousepress = true;
        point = event->pos();
    }
}

//鼠标移动
void Widget::mouseMoveEvent(QMouseEvent *event)
{
   if (mousepress)
   {
      move(event->pos() - point + pos());//移动当前窗口
   }
}

在main文件中:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

最后实现完成,结果如图所示:

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 PySide2 中,如果您想要去掉窗口边框,并添加自定义的放大缩小关闭按钮,可以使用以下代码实现: ```python from PySide2.QtCore import Qt from PySide2.QtGui import QIcon from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QPushButton class CustomWindow(QWidget): def __init__(self): super().__init__() self.setWindowFlags(Qt.FramelessWindowHint) self.initUI() def initUI(self): # 设置窗口大小和位置 self.setGeometry(100, 100, 500, 400) # 创建水平布局 layout = QHBoxLayout(self) # 创建标题栏标签 title_label = QLabel('Custom Window') # 将标题栏标签添加到布局中 layout.addWidget(title_label) # 创建放大缩小关闭按钮 maximize_button = QPushButton('□') minimize_button = QPushButton('-') close_button = QPushButton('×') # 设置按钮大小 button_size = 20 maximize_button.setFixedSize(button_size, button_size) minimize_button.setFixedSize(button_size, button_size) close_button.setFixedSize(button_size, button_size) # 将按钮添加到布局中 layout.addWidget(minimize_button) layout.addWidget(maximize_button) layout.addWidget(close_button) # 设置按钮样式 button_style = ''' QPushButton {{ border: none; background-color: transparent; color: #ffffff; }} QPushButton:hover {{ background-color: #ff0000; }} ''' maximize_button.setStyleSheet(button_style) minimize_button.setStyleSheet(button_style) close_button.setStyleSheet(button_style) # 为按钮添加功能 maximize_button.clicked.connect(self.showMaximized) minimize_button.clicked.connect(self.showMinimized) close_button.clicked.connect(self.close) if __name__ == '__main__': app = QApplication([]) window = CustomWindow() window.show() app.exec_() ``` 在上面的代码中,我们创建了一个自定义窗口类 `CustomWindow`,并在其中添加了一个水平布局。我们还创建了一个标题栏标签和三个按钮放大缩小关闭。我们将这些控件添加到布局中,并为它们设置了样式和大小。最后,我们为按钮添加了相应的功能,例如 `showMaximized`、`showMinimized` 和 `close`。 需要注意的是,在自定义窗口类中使用 `setWindowFlags(Qt.FramelessWindowHint)` 方法去掉了窗口边框。这意味着您需要手动添加相应的控件来实现窗口的移动、最小化和关闭等功能。在上面的示例中,我们为最小化、最大化和关闭按钮添加了相应的功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值