当前位置:   article > 正文

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

qt禁止窗口拖动
  • 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 };
    };
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/95161
推荐阅读
相关标签
  

闽ICP备14008679号