引言 最近在做數(shù)據(jù)庫相關(guān)課程設(shè)計,所以就借此機(jī)會,,先熟悉一下Qt的一些編程,,同時了解一下C++的一些特性。其實更重要的是如何組織好相關(guān)模塊的連接,,如何規(guī)劃項目,,等等。所以就順道把過程中遇到的問題和重要的一些控件的槽和信號介紹一下,,以后忘了可以回來看,。呵呵。 以下是我用到的一些重要的函數(shù)和代碼: 一,、數(shù)據(jù)庫的連接 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");//主機(jī)名 TB.setDatabaseName("chat");//數(shù)據(jù)庫名 //TB.setPort(3306);//端口號 TB.setUserName("root");//用戶名 TB.setPassword("123");//密碼 2.數(shù)據(jù)的避免無法處理和插入漢字問題 create database chat character set GBK;//這樣建立數(shù)據(jù)庫就可以插入中文或者其它辦法
use chat;
--查看數(shù)據(jù)庫的字符集
show variables like 'character\_set\_%';
show variables like 'collation_%';
--設(shè)置數(shù)據(jù)庫字符編碼
set names 'GBK' --這樣的話在consle端口查看不會是亂碼
--drop table xxxx;
--修改系列
--增加Head屬性
alter table tb_user add Head varchar(20);
--刪除數(shù)據(jù)
delete from tb_userRS where friendID = "107865437";
--更新屬性
update tb_user set username="淼的世界" where id="642419907"; 二,、應(yīng)用程序漢字處理(黑代碼) QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));//字體處理 三、設(shè)定整個對話框的背景顏色(黑代碼) this->setAutoFillBackground(true);//1s step QPalette palette;//2s step palette.setBrush(QPalette::Background, QBrush(QPixmap("image/login.JPG"))); this->setPalette(palette);//3s step 四,、應(yīng)用程序圖標(biāo)設(shè)計 圖標(biāo)設(shè)計的時候,,我們要把.ico文件放在工程的ico目錄下,或者其它目錄也行,。然后在該圖標(biāo)目錄下建立一個后綴為.rc的文件,,內(nèi)容如下,IDI_ICON1 ICON DISCARDABLE "cool.ico" ,,就樣在程序中如此調(diào)用:this->setWindowIcon(QIcon("ico/cool.ico"));//設(shè)置程序ico圖標(biāo),注意要在.pro做設(shè)置,,OK。 五,、應(yīng)用程序enter快捷鍵事件的實現(xiàn) 1.在頭文件中定義一個快捷鍵 QShortcut *key_enter_login;//快捷鍵登錄 2.定義相應(yīng)的槽 private slots: void enter_longin(); 3.構(gòu)造函數(shù)中 key_enter_login = new QShortcut(QKeySequence(tr("Return")), this);//設(shè)置Enter快捷鍵,,但是Enter不行,用Return就行,。 connect(key_enter_login, SIGNAL(activated()), this, SLOT(enter_longin())); 六,、文字鏈接到網(wǎng)頁 構(gòu)造函數(shù)中: QLabel *m_r_acount = new QLabel(this);
m_r_acount->setText(tr("<a href=\"http://www.\">注冊賬號 ")); 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的設(shè)置 QComboBox *m_a_choice = new QComboBox(this); QStringList C_strings; C_strings <<"666666"<< "642419907" << "767938089" << "107865437" << "110120119" ; completer = new QCompleter(C_strings, this);//可以進(jìn)行匹配的哦 m_a_choice->clear(); m_a_choice->addItems(C_strings); m_a_choice->setMaxVisibleItems(7);//設(shè)置最大顯示下列項 超過要使用滾動條拖拉 m_a_choice->setEditable(true); m_a_choice->setCompleter(completer); 六,、鼠標(biāo)滑過事件 頭文件中: protected: void mouseMoveEvent(QMouseEvent *event);//鼠標(biāo)滑動 構(gòu)造函數(shù)中: //設(shè)定鼠標(biāo)跟蹤事件
this->setMouseTracking(true);
//標(biāo)簽跟蹤
qlabel->setMouseTracking(true);//還可以設(shè)置其它控件 實現(xiàn)函數(shù)中:(舉個小例子) 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();
} 六,、重寫標(biāo)簽鼠標(biāo)點擊事件的實現(xiàn)(需要重寫QLabel,構(gòu)造自己的QLabel->MyLabel) #ifndef CLICKEDLABEL_H_
#define CLICKEDLABEL_H_
#include <QtGui>
class ClickedLabel : public QLabel //繼承QLabel,重寫事件,,實現(xiàn)點擊標(biāo)簽退出
{
Q_OBJECT
public:
ClickedLabel(QWidget *parent = 0);
int MyLabelPressed;
void mousePressEvent(QMouseEvent *e);//添加鼠標(biāo)響應(yīng)事件
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定義一個這樣的標(biāo)簽,,那么在如下使用信號和槽: ClickedLabel *s_exit;//退出標(biāo)簽,這樣就可以使用clicked()信號了 connect(s_exit, SIGNAL(clicked()), this, SLOT(closed())); 七,、重寫QListWidget鼠標(biāo)右擊彈出菜單事件,。(該實現(xiàn)需要重寫QListWidget) #ifndef LISTWIDGET_H_
#define LISTWIDGET_H_
#include<QApplication>
#include<QWidget>
#include<QListWidget>
#include<QMenu>
#include<QAction>
#include "scaninfo.h"
#include "addfriend.h"
class ListWidget : public QListWidget
{
Q_OBJECT
public:
explicit ListWidget(QWidget *parent = 0);//explicit構(gòu)造函數(shù)只能被顯示調(diào)用
void contextMenuEvent ( QContextMenuEvent * event );
private:
QAction *action;//刪除選項
QAction *scan_action;//查看資料
QAction *add_friend;//添加好友
QMenu *popMenu;
};
#endif /* LISTWIDGET_H_ */ #include "ListWidget.h" #include<QMessageBox> 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()); // 菜單出現(xiàn)的位置為當(dāng)前鼠標(biāo)的位置 } } 使用的時候在你需要的類中(頭文件或者構(gòu)造函數(shù)):ListWidget u_good_Widget = new ListWidget(this);就行 七、類似于QQ那種伸縮列表的小效果,,自己可以參考 1.申明該QListWidget的相關(guān)槽 connect(u_good_Widget, SIGNAL(clicked(QModelIndex)), this, SLOT(good_Stretch(QModelIndex))); 2.槽實現(xiàn) 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;結(jié)果也出乎意料
}
} 七,、QMenu中QAction的事件定義 QMenu *cmenu = NULL;//定義并初始化右鍵
cmenu = new QMenu(this);//初始化右鍵在何處顯示
QAction *EnterGroupTalk = cmenu->addAction(QIcon("image/grouptalk.jpg"),tr("進(jìn)入群聊"));//添加右鍵菜單顯示項
connect(EnterGroupTalk,SIGNAL(triggered()),this,SLOT(on_actionEnter_GroupTalk_triggered()));//使AddRecode與其他函數(shù)關(guān)聯(lián) 八、主窗體,,標(biāo)簽之類的一些外觀設(shè)計 this->setFixedSize(this->width(), this->height());//固定大小
this->setWindowFlags(Qt::FramelessWindowHint);//去掉標(biāo)題欄語句
QFont font("ZYSong18030", 12);//設(shè)置字體
this->setFont(font);
//透明度
this->setWindowOpacity(0.8); //標(biāo)簽 QLabel s_acount_label = new QLabel(this); s_acount_label->setFont(QFont("ZYSong18030", 8));//字體小點 s_acount_label->setGeometry(155, 10, 60, 20); 九,、以下是微小的展示效果,當(dāng)然很多功能沒有實現(xiàn),慢慢完善吧,,主要是想總結(jié)一下,,不管好不好 十、(以上都不是什么很厲害的,,其實網(wǎng)絡(luò)編程應(yīng)該很重要,。我參考資料實現(xiàn)了群聊,但是對于tcp還不是很熟悉,,所以以后慢慢再學(xué)習(xí)了,自己還是不行,,所以就不貼了,覺得自己行的時候再說) |
|