Qt操作数据库2

一、数据库的连接
  1. QSqlDatabase TB = QSqlDatabase::addDatabase("QMYSQL");// becomes the new default connect
//TB.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");//使用SSL安全连接
TB.setHostName("127.0.0.1");//主机名
TB.setDatabaseName("chat");//数据库名
//TB.setPort(3306);//端口号
TB.setUserName("root");//用户名
TB.setPassword("123");//密码
   2.数据的避免无法处理和插入汉字问题
create database chat character set GBK;//这样建立数据库就可以插入中文或者其它办法 use chat;--查看数据库的字符集 show variables like 'character\_set\_%'; show variables like 'collation_%'; --设置数据库字符编码 set names 'GBK' --这样的话在consle端口查看不会是乱码 --drop table xxxx ;--修改系列--增加Head属性alter table tb_user add Head varchar(20);--删除数据delete from tb_userRS where friendID = "107865437";--更新属性update tb_user set username="淼的世界" where id="642419907";

二、应用程序汉字处理(黑代码)
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));//字体处理
三、设定整个对话框的背景颜色(黑代码)
this->setAutoFillBackground(true);//1s step
QPalette palette;//2s step
palette.setBrush(QPalette::Background, QBrush(QPixmap("image/login.JPG")));
this->setPalette(palette);//3s step
四、应用程序图标设计
图标设计的时候,我们要把.ico文件放在工程的ico目录下,或者其它目录也行。然后在该图标目录下建立一个后缀为.rc的文件,内容如下,IDI_ICON1 ICON DISCARDABLE "cool.ico" ,就样在程序中如此调用:this->setWindowIcon(QIcon("ico/cool.ico"));//设置程序ico图标,注意要在.pro做设置,OK。
五、应用程序enter快捷键事件的实现
  1. 在头文件中定义一个快捷键 QShortcut *key_enter_login;//快捷键登录
2. 定义相应的槽 private slots:
void enter_longin();
3.构造函数中
key_enter_login = new QShortcut(QKeySequence(tr("Return")), this);//设置Enter快捷键,但是Enter不行,用Return就行。
connect(key_enter_login, SIGNAL(activated()), this, SLOT(enter_longin()));
六、文字链接到网页
构造函数中
QLabel * m_r_acount = new QLabel(this);m_r_acount->setText(tr("注册帐号 "));
connect(m_r_acount, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(const QString)));
槽中 不要忘了头文件申明槽)
void MyFirstQQ::openUrl(const QString ®ister_url)
{QDesktopServices::openUrl(QUrl("http://localhost:8080/chat/register.jsp"));
}
六、QComboBox的设置
QComboBox * m_a_choice = new QComboBox(this);
QStringList C_strings;
C_strings <<"666666"<< "642419907" << "767938089" << "107865437" << "110120119" ;
completer = new QCompleter(C_strings, this);//可以进行匹配的哦
m_a_choice->clear();
m_a_choice->addItems(C_strings);
m_a_choice->setMaxVisibleItems(7);//设置最大显示下列项 超过要使用滚动条拖拉
m_a_choice->setEditable(true);
m_a_choice->setCompleter(completer);
六、鼠标滑过事件
头文件中:
protected:
     void mouseMoveEvent(QMouseEvent *event);//鼠标滑动
构造函数中:
//设定鼠标跟踪事件this->setMouseTracking(true);//标签跟踪qlabel ->setMouseTracking(true);//还可以设置其它控件
实现函数中:(举个小例子)
int x = event->x();int y = event->y();if(x > u_b_x && x < u_b_x+u_b_width && y > u_b_y && y < u_b_y+u_b_height){u_background->setPixmap(QPixmap("image/skin3.JPG"));}else if(x > u_button_x && x < u_button_x+u_button_width && y > u_button_y && y < u_button_y+u_button_height){textbox.show();}else{u_background->setPixmap(QPixmap("image/skin.JPG"));textbox.close();}
六、重写标签鼠标点击事件的实现(需要重写QLabel,构造自己的QLabel->MyLabel)
#ifndef CLICKEDLABEL_H_#define CLICKEDLABEL_H_#include class ClickedLabel : public QLabel //继承QLabel,重写事件,实现点击标签退出{ Q_OBJECTpublic: ClickedLabel(QWidget *parent = 0); int MyLabelPressed; void mousePressEvent(QMouseEvent *e);//添加鼠标响应事件 void mouseReleaseEvent(QMouseEvent *e);signals: void clicked();//点击信号};#endif /* CLICKEDLABEL_H_ */

#include "ClickedLabel.h"ClickedLabel::ClickedLabel(QWidget *parent):QLabel(parent){ MyLabelPressed = 0;}void ClickedLabel::mousePressEvent ( QMouseEvent * e ){ MyLabelPressed = 1;}void ClickedLabel::mouseReleaseEvent ( QMouseEvent * e ){ if (MyLabelPressed) { emit clicked(); MyLabelPressed = 0; }}
实使用的时候,只要包括该头文件,然后用ClickedLabel定义一个这样的标签,那么在如下使用信号和槽:
ClickedLabel *s_exit;//退出标签,这样就可以使用clicked()信号了
connect(s_exit, SIGNAL(clicked()), this, SLOT(closed()));

七、重写QListWidget鼠标右击弹出菜单事件。(该实现需要重写QListWidget)
#ifndef LISTWIDGET_H_#define LISTWIDGET_H_#include#include#include#include#include#include "scaninfo.h"#include "addfriend.h"class ListWidget : public QListWidget{ Q_OBJECTpublic: explicit ListWidget(QWidget *parent = 0);//explicit构造函数只能被显示调用 void contextMenuEvent ( QContextMenuEvent * event );private: QAction *action;//删除选项 QAction *scan_action;//查看资料 QAction *add_friend;//添加好友 QMenu *popMenu;};#endif /* LISTWIDGET_H_ */

#include "ListWidget.h"
#include

ListWidget::ListWidget(QWidget *parent):
QListWidget(parent)
{
action = new QAction(QIcon("image/delete.jpg"),tr("删除好友"),this); //删除事件
scan_action = new QAction(QIcon("image/scan.jpg"),tr("查看资料"),this);
add_friend = new QAction(QIcon("image/addFriend.jpg"),tr("添加好友"),this);
popMenu = new QMenu(this);
}

void ListWidget::contextMenuEvent ( QContextMenuEvent * event )
{
if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) //如果有item则选中
{
itemAt(mapFromGlobal(QCursor::pos()))->setSelected(true);
}
else
{
popMenu->addAction(add_friend);
popMenu->removeAction(action);
popMenu->removeAction(scan_action);
popMenu->exec(QCursor::pos());
}
if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) //如果有item则添加"删除"菜单
{
popMenu->addAction(action);
popMenu->addAction(scan_action);
popMenu->removeAction(add_friend);
popMenu->exec(QCursor::pos()); // 菜单出现的位置为当前鼠标的位置
}
}
使用的时候在你需要的类中(头文件或者构造函数):ListWidget u_good_Widget = new ListWidget(this);就行

七、类似于QQ那种伸缩列表的小效果,自己可以参考
   1.申明该QListWidget的相关槽
connect(u_good_Widget, SIGNAL(clicked(QModelIndex)), this, SLOT(good_Stretch(QModelIndex)));
2.槽实现
void UserQQ::good_Stretch(QModelIndex index){if(u_flag){u_item->setIcon(QIcon(QPixmap("image/QListWidgetItemUp.png")));}else
{u_item->setIcon((QPixmap("image/QListWidgetItemDown.png")));}if(index.row()==0){for(int j = 1; j < u_hide_count+1; j++){u_good_Widget->setRowHidden(j, u_flag);}u_flag = !u_flag; //这里不要这样做u_flag = false;结果也出乎意料}}
七、QMenu中QAction的事件定义
QMenu *cmenu = NULL;//定义并初始化右键cmenu = new QMenu(this);//初始化右键在何处显示QAction *EnterGroupTalk = cmenu->addAction(QIcon("image/grouptalk.jpg"),tr("进入群聊"));//添加右键菜单显示项connect(EnterGroupTalk,SIGNAL(triggered()),this,SLOT(on_actionEnter_GroupTalk_triggered()));//使AddRecode与其他函数关联
八、主窗体,标签之类的一些外观设计
this->setFixedSize(this->width(), this->height());//固定大小this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏语句QFont font("ZYSong18030", 12);//设置字体this->setFont(font);//透明度this->setWindowOpacity(0.8);
//标签
QLabel s_acount_label = new QLabel(this);
s_acount_label->setFont(QFont("ZYSong18030", 8));//字体小点
s_acount_label->setGeometry(155, 10, 60, 20);
九、以下是微小的展示效果,当然很多功能没有实现,慢慢完善吧,主要是想总结一下,不管好不好
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值