【Qt编程之Widgets模块】-002:QT窗体禁止缩放固定大小:使用setFixedSize方法

  • QT窗体禁止拖动缩放:使用setFixedSize方法

需求:

  • 我想实现窗体在正常状态(Qt::WindowNoState)边框不能通过鼠标拖动改变窗体大小,不影响窗体的正常最大化和还原状态(600*700)(屏幕的初始大小是可使用屏幕的大小)。
    在这里插入图片描述

实现:

  • 在构造方法里获取可以使用的最大屏幕尺寸。

CUserAddDialog .h

#include "ui/Public/BaseDialog/BaseDialog.h"
#include "ui_UserAddDialog.h"
#include "Logical/HardWareTestManager/HardWareUserManger.h"

namespace geniusclient
{
    class CUserAddDialog : public CBaseDialog
    {
        Q_OBJECT

    public:
        CUserAddDialog(HardWareUser *pInfo,bool bModify =false, QWidget *parent = Q_NULLPTR);
        ~CUserAddDialog();

    private slots:
        void onSave();

    private :
        void initial();

    private:
        Ui::CUserAddDialog ui;
        HardWareUser *m_pInfo;
        bool m_bModify{ false };
    };
}

CUserAddDialog .cpp

#include "UserAddDialog.h"
#include "Logical/HardWareTestManager/HardWareUserManger.h"
#include "Logical/HardWareTestManager/HardWareLockManger.h"
#include <QRegExpValidator>
#include <QToolTip>

namespace geniusclient
{
    CUserAddDialog::CUserAddDialog(HardWareUser *pInfo, bool bModify, QWidget *parent)
        : m_pInfo(pInfo),
        m_bModify(bModify),
        CBaseDialog(parent)
    {
        ui.setupUi(insertedWidget());
        setTitle(tr("Add user"));
        // 设置窗口固定大小
        setFixedSize(600, 700);
        connect(ui.m_pBtnSave, SIGNAL(clicked()), this, SLOT(onSave()));
        // 设置正则表达式.
        ui.m_pLineEditUser->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9]{1,16}")));
        ui.m_pLineEditPwd->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9]{1,16}")));
        // 限制输入lineEdit中最多只能输入5个字符
        ui.m_pLineEditName->setMaxLength(6);
        ui.m_pRadioBtnAdmin->setEnabled(false);
        ui.buttonGroup->setId(ui.m_pRadioBtnOper, User);
        ui.buttonGroup->setId(ui.m_pRadioBtnEngineer, Engineer);
        ui.buttonGroup->setId(ui.m_pRadioBtnAdmin, Admin);
        initial();
    }

    CUserAddDialog::~CUserAddDialog()
    {
    }

    void CUserAddDialog::onSave()
    {
        if (m_pInfo == nullptr)
        {
            return;
        }
        std::string strUserNumber = ui.m_pLineEditUser->text().trimmed().toStdString();
        std::string strUserPwd = ui.m_pLineEditPwd->text().toStdString();
        std::string strUserName = ui.m_pLineEditName->text().toStdString();
        std::string strOtherInfo =ui.plainTextEdit->toPlainText().toStdString();
        if (strUserNumber.empty())
        {
            QToolTip::showText(ui.m_pLineEditUser->mapToGlobal(QPoint(0, 0)), tr("cannot be empty."));
            return;
        }
        if (strUserPwd.empty())
        {
            QToolTip::showText(ui.m_pLineEditPwd->mapToGlobal(QPoint(0, 0)), tr("cannot be empty."));
            return;
        }

        if (strUserPwd.length()<6)
        {
            QToolTip::showText(ui.m_pLineEditPwd->mapToGlobal(QPoint(0, 0)), tr("password lenth must be more than 6."));
            return;
        }

        m_pInfo->strUserNumber = strUserNumber;
        m_pInfo->strPwd = strUserPwd;
        m_pInfo->strUserName = strUserName;
        m_pInfo->userRole = static_cast<HardWareUserRole>(ui.buttonGroup->checkedId());
        m_pInfo->bEnable = ui.radioButton_3->isChecked();
        m_pInfo->strInfo = strOtherInfo;

        if (m_pInfo->userRole == Admin)
        {
            if (strUserPwd.length() < 6)
            {
                QToolTip::showText(ui.m_pLineEditPwd->mapToGlobal(QPoint(0, 0)), tr("password lenth must be more than 6."));
                return;
            }
            HardWareLock struLockInfo;
            struLockInfo.bEnable = ui.radioButton->isChecked();
            struLockInfo.strUnlockPassword = ui.m_pLineEditUnlockPwd->text().toStdString();
            if (struLockInfo.bEnable)
            {
                if (struLockInfo.strUnlockPassword.empty())
                {
                    QToolTip::showText(ui.m_pLineEditUnlockPwd->mapToGlobal(QPoint(0, 0)), tr("Unlock password cannot be empty."));
                    return;
                }

                if (struLockInfo.strUnlockPassword.length() < 6)
                {
                    QToolTip::showText(ui.m_pLineEditUnlockPwd->mapToGlobal(QPoint(0, 0)), tr("Unlock password lenth must be more than 6."));
                    return;
                }
            }
            
            CHardWareLockManger::instance()->modify(struLockInfo);
        }
        if (!m_bModify &&CHardWareUserManger::instance()->isAlreadyExsit(*m_pInfo))
        {
            QToolTip::showText(ui.m_pLineEditUser->mapToGlobal(QPoint(0, 0)), tr("Name duplicated"));
        }
        else
        {
            accept();
        }
    }

    void CUserAddDialog::initial()
    {
        if (m_pInfo == nullptr)
        {
            return;
        }
        if (m_bModify)
        {
            ui.m_pLineEditUser->setEnabled(false);
        }
        ui.m_pLineEditUser->setText(QString::fromStdString(m_pInfo->strUserNumber));
        ui.m_pLineEditPwd->setText(QString::fromStdString(m_pInfo->strPwd));
        ui.m_pLineEditName->setText(QString::fromStdString(m_pInfo->strUserName));
        ui.plainTextEdit->setPlainText(QString::fromStdString(m_pInfo->strInfo));
        ui.label_7->setVisible(false);
        ui.m_pLineEditUnlockPwd->setVisible(false);
        ui.label_8->setVisible(false);
        ui.radioButton->setVisible(false);
        ui.radioButton_2->setVisible(false);
        switch (m_pInfo->userRole)
        {
        case User:
            ui.m_pRadioBtnOper->setChecked(true);
            break;
        case Engineer:
            ui.m_pRadioBtnEngineer->setChecked(true);
            break;
        case Admin:
        {
            ui.m_pRadioBtnAdmin->setChecked(true);
            ui.m_pRadioBtnEngineer->setChecked(false);
            ui.m_pRadioBtnOper->setChecked(false);
            ui.m_pRadioBtnEngineer->setEnabled(false);
            ui.m_pRadioBtnOper->setEnabled(false);
            ui.radioButton_3->setEnabled(false);
            ui.radioButton_4->setEnabled(false);

            ui.label_7->setVisible(true);
            ui.m_pLineEditUnlockPwd->setVisible(true);
            ui.label_8->setVisible(true);
            ui.radioButton->setVisible(true);
            ui.radioButton_2->setVisible(true);
            HardWareLock struLockInfo;
            struLockInfo = CHardWareLockManger::instance()->getLockInfo();
            ui.m_pLineEditUnlockPwd->setText(QString::fromStdString(struLockInfo.strUnlockPassword));
            if (struLockInfo.bEnable)
            {
                ui.radioButton->setChecked(true);
            }
            else
            {
                ui.radioButton_2->setChecked(true);
            }
        }
        default:
            break;
        }
        if (m_pInfo->bEnable)
        {
            ui.radioButton_3->setChecked(true);
        }
        else
        {
            ui.radioButton_4->setChecked(true);
        }
    }
}

UI界面
在这里插入图片描述

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
 
    //ui->setupUi(this);
    setWindowTitle(tr("KaiRuanSADP"));
    setWindowState(Qt::WindowMaximized);  //最大化窗体,此时默认占满可使用屏幕尺寸。
 
    //获取屏幕可使用大小尺寸
    QScreen *deskScreen = QApplication::primaryScreen();
    if(deskScreen)
    {
        QSize availableSize = deskScreen->availableVirtualSize();
        availableWidth = availableSize.width();
        availableHeight = availableSize.height();
 
        //windowMaxSize是自定义的Qsize类型变量,存储屏幕可使用尺寸大小
        //高度减去23,是因为QT系统在重新设定最大可使用尺寸高度时会自动加23
        //调试模式会提示警告,不减去23一样得到最大化效果
        windowMaxSize.setHeight(availableHeight-23);
        windowMaxSize.setWidth(availableWidth);
 
        qDebug()<<availableWidth<<"和"<<availableHeight;
    }
 
    tabWidget = new QTabWidget(this);
    QWidget *browser_tab = new QWidget;
    QWidget *users_tab = new QWidget;
 
    tabWidget->addTab(browser_tab, QIcon(":resources/browser.png"), "MAC扫描");
    tabWidget->addTab(users_tab, QIcon(":resources/users.png"), "摄像头扫描");
 
    tabWidget->setGeometry(20, 20, 300, 250);
 
    this->installEventFilter(this);
}

通过事件过滤器控制最大化和无状态窗体的尺寸显示,禁止鼠标拖动改变窗体尺寸:

bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
    Q_UNUSED(obj);
    //qDebug()<<obj;
 
    QEvent::Type e_type = e->type();
    //qDebug()<<e_type;
    //qDebug()<<windowMaxSize;
 
    //捕获QEvent::WindowStateChange事件
    //QEvent::WindowState 是窗体状态
    if (e_type == QEvent::WindowStateChange)
    {
        e->accept();
        //e->ignore();
        qDebug()<<this->windowState();
 
        //如果窗体发生改变,判定是最大化了,还是还原无状态
        if(this->windowState() != Qt::WindowMaximized)
        {
            //所有不是最大化的状态改变就通过setFixedSize发放限定窗体
            //这两句代码必须按顺序,否则将先缩放在移动
            this->move(availableWidth/2-1024/2,availableHeight/2-768/2);  //无状态显示时,默认居中显示
            this->setFixedSize(1024,768);
        }
        else
        {
            //最大化窗体状态,就通过setFixedSize方法设定最大化尺寸限定
            //再次调用最大化显示
            this->setFixedSize(windowMaxSize);
            this->showMaximized();
        }
        return true;
    }
 
    return QWidget::event(e);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隨意的風

如果你觉得有帮助,期待你的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值