Chinaunix首页 | 论坛 | 博客
  • 博客访问: 400690
  • 博文数量: 168
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 13:46
文章分类

全部博文(168)

文章存档

2015年(51)

2014年(30)

2013年(87)

我的朋友

分类: Mysql/postgreSQL

2015-06-01 09:20:31

   引言  最近在做数据库相关课程设计,所以就借此机会,先熟悉一下Qt的一些编程,同时了解一下C++的一些特性。其实更重要的是如何组织好相关模块的连接,如何规划项目,等等。所以就顺道把过程中遇到的问题和重要的一些控件的槽和信号介绍一下,以后忘了可以回来看。呵呵。
   以下是我用到的一些重要的函数和代码:
一、数据库的连接
  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(""));
}
六、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_OBJECT public: 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_OBJECT public: 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);
九、以下是微小的展示效果,当然很多功能没有实现,慢慢完善吧,主要是想总结一下,不管好不好
十、(以上都不是什么很厉害的,其实网络编程应该很重要。我参考资料实现了群聊,但是对于tcp还不是很熟悉,所以以后慢慢再学习了,自己还是不行,所以就不贴了,觉得自己行的时候再说)
阅读(2049) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~