本程序包含以下部分
1 设置界面背景图片及相关部件设置
2 整个界面拖动的实现
3 网络实现
效果图:
(需要完整代码,可加Q)
1 设置界面背景图片及相关部件设置
背景图片是提前制作好的,并优化成圆角,主要代码:
/***************设置背景图片(两种方式都可以)****************/
QPalette pal;
//pal.setColor( QPalette::Background,QColor(255,245,225) );
pal.setBrush( QPalette::Window,QBrush(QPixmap(":/imgs/login")) );
this->setPalette(pal);
this->setAutoFillBackground(true);
/*******************设置成圆角的*********************************/
QBitmap objBitmap(size());//生成一张位图
QPainter painter(&objBitmap);//QPainter用于在位图上绘画
painter.fillRect(rect(),Qt::transparent);//填充位图矩形框(用白色填充)
painter.setBrush(QColor(0,0,0));
painter.drawRoundedRect(this->rect(),10,10);//在位图上画圆角矩形(用黑色填充)
setMask(objBitmap);//使用setmask过滤即可
登陆界面主要部件的美化:
m_lblUser = new QLabel(this);
m_lblUser->setText("用户名");
m_lblUser->setGeometry( m_lblLogo->geometry().right()+40,m_lblLogo->geometry().top()+10,60,25 );
m_lblUser->setStyleSheet( "QLabel{font-size:20px;color:rgb(50,50,100)};" );
m_lblUser->hide();
m_lblPassword = new QLabel(this);
m_lblPassword->setText("密 码");
m_lblPassword->setGeometry( m_lblUser->geometry().left(),m_lblUser->geometry().bottom()+20,
m_lblUser->width(),m_lblUser->height() );
m_lblPassword->setStyleSheet( "QLabel{font-size:20px;color:rgb(50,50,100)};" );
m_lblPassword->hide();
m_txtUser = new QLineEdit(this);
m_txtUser->setGeometry( 260,90,200,25 );
m_txtUser->setStyleSheet( "QLineEdit{"
"font-size:15px;"
//"font-weight:1000;"
"font-style:black;"
"background-color:rgb(230,230,230);"
"border:1px;"
"border-style:dashed double;"
"border-radius:4px;"
"border-color:rgb(150,150,150);"
"}"
"QLineEdit:focus{"
"background:rgb(250,255,250);"
"border-color:rgb(250,0,0);"
"}"
);
m_txtPassword = new QLineEdit(this);
m_txtPassword->setEchoMode(QLineEdit::Password);
m_txtPassword->setGeometry( 260,m_txtUser->geometry().bottom()+15,200,25 );
m_txtPassword->setStyleSheet( "QLineEdit{"
"font-size:15px;"
//"font-weight:1000;"
"font-style:black;"
"background-color:rgb(230,230,230);"
"border:1px;"
"border-style:dashed double;"
"border-radius:4px;"
"border-color:rgb(150,150,150);"
"}"
"QLineEdit:focus{"
"background:rgb(250,255,250);"
"border-color:rgb(250,0,0);"
"}"
);
m_btnLogin = new QPushButton(this);
//m_btnLogin->setAttribute(Qt::WA_TranslucentBackground);//透明
connect( m_btnLogin,SIGNAL(clicked()),this,SLOT(ReplyLogin()) );
m_btnLogin->setText( "登 陆" );
m_btnLogin->setGeometry( m_txtPassword->geometry().left()-25,m_txtPassword->geometry().bottom()+18,
233,28 );
m_btnLogin->setStyleSheet( "QPushButton{"
"color:rgb(0,0,0);"
"font-size:20px;"
"font-weight:bold;"
"border:2px;"
"border-radius:5px;"
"}"
"QPushButton:hover{color:rgb(250,250,250)};"
);
/**************退出按钮****************/
m_btnExit = new QPushButton(this);
m_btnExit->setAttribute(Qt::WA_TranslucentBackground);//透明
connect( m_btnExit,SIGNAL(clicked()),this,SLOT(replyExit()) );
m_btnExit->setText( "退 出" );
m_btnExit->setGeometry( m_btnLogin->geometry().right()-106,m_btnLogin->geometry().bottom()+40,
108,25 );
m_btnExit->setStyleSheet( "QPushButton{"
"color:rgb(250,250,250);"
"font-size:15px;"
"border:2px;"
"border-radius:5px;"
"}"
"QPushButton:hover{color:rgb(10,10,10)};"
);
/**************注册按钮****************/
m_btnRegister = new QPushButton(this);
m_btnRegister->setText( "注 册" );
m_btnRegister->setAttribute(Qt::WA_TranslucentBackground);//透明
connect( m_btnRegister,SIGNAL(clicked()),this,SLOT(replyRegister()) );
m_btnRegister->setGeometry( m_btnExit->geometry().left()-128,m_btnExit->geometry().top(),
108,25 );
m_btnRegister->setStyleSheet( "QPushButton{"
"color:rgb(250,250,250);"
"font-size:15px;"
"border:2px;"
"border-radius:5px;"
"}"
"QPushButton:hover{color:rgb(10,10,10)};"
);
2 拖动界面的实现
当鼠标按下时存储当前位置,注意要用m_pntOldPos = e->globalPos();
当鼠标移动时,
this->setGeometry( this->geometry().left()+e->globalPos().x()-m_pntOldPos.x(),
this->geometry().top()+e->globalPos().y()-m_pntOldPos.y(),
this->geometry().width(),
this->geometry().height()
);
m_pntOldPos = e->globalPos();
3 网络实现
当登陆界面启动时,连接网络,WmLink网络连接类,当服务器发来信息时调用ReplyServerResult函数,判断是否连接。
g_link = new WmLink( g_strIpAdress,g_nPort );
connect( g_link,SIGNAL(NewReplySignal(quint16)),
this,SLOT(ReplyServerReslut(quint16)) );
connect( g_link,SIGNAL( ReceiveDataProgressSignal(qint64,qint64) ),
this,SLOT(ReplyReceiveDataProgressSignal(qint64,qint64) ) );//接收进度