赞
踩
需求:
实现:
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);
}
}
}
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);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。